Programming in C++ - Wikilibros, open books for an open world (2023)

Satisfied

  • 1 comparison operators
  • 2 logical operators
    • 2.2 o (||)
    • 2.3 no(!)

comparison operators[To edit|edit source]

the operators<(less than),>(but bigger than),<=(equal to less than),>=(Better than or equal to),==(and even!=(not equal) are relational operators used to compare two values. Variables can be compared to another variable or to a literal.


He<The operator checks if the first operand is less than the second operand. If the first operand is less than the second, true is returned. Otherwise, it returns false.

  • examples
E T x =5;E T j = 1;E (x < 10) //x is 5 which is less than 10 returns true{ //...Código...}E (x < 0) //x is 5 which is not less than 0 returns false{ //...Código...}E (x < j) //x is 5 and y is 1. 5 is not less than 1, returns false{ //...Código...}

He>The operator checks if the first operand is greater than the second operand. If the first operand is greater than the second operand, true is returned. Otherwise, it returns false.

  • examples
E T x =12;E T j = 1;E (x > 10) //x is 12 which is greater than 10 returns true{ //...Código...}E (x > 15) //x is 12 which is not greater than 15 will return false{ //...Código...}E (x > j) //x is 12 and y is 1. 12 is greater than 1, returns true{ //...Código...}

He<=The operator checks if the first operand is less thanoequal to the second operand. If the first operand is less than or equal to the second operand, true is returned. Otherwise, it returns false.

  • examples
E T x = 12;E T j = 12;E (x <= 12) //x is 12 which is less than or equal to 12 returns true{ //...Código...}E (x <= 5) //x is 12 which is not less than or equal to 5 returns false{ //...Código...}E(x <= j) //x is 12 and y is 12. 12 is less than or equal to 12, returns true{ //...Código...}

He>=The operator checks if the first operand is greater thanoequal to the second operand. If the first operand is greater than or equal to the second operand, true is returned. Otherwise, it returns false.

  • examples
E T x = 12;E T j = 12;E (x >= 12) //x is 12 which is greater than or equal to 12 returns true{ //...Código...}E (x >= 19) //x is 12 which is not greater than or equal to 19 will return false{ //...Código...}E (x >= j) //x is 12 and y is 12. 12 is greater than or equal to 12, returns true{ //...Código...}

He==The operator checks whether the first operand is equal to the second operand. If the first operand is equal to the second operand, true is returned. Otherwise, it returns false.

  • examples
E T x = 5;E T j = 6;E (x == 5) //x is 5, which equals 5, returns true{ //...Código...}E (x == 7) //x is 5, which is not equal to 7, returns false{ //...Código...}E (x == j) //x is 5 and y is 6. 5 is not equal to 6, returns false{ //...Código...}

He!=The operator checks that the first operand is not equal to the second operand. If the first operand is not equal to the second operand, true is returned. Otherwise, it returns false.

  • examples
E T x = 5;E T j = 6;E (x != 5) //x is 5, which equals 5, returns false{ //...Código...}E (x != 7) //x is 5, which is not equal to 7, returns true{ //...Código...}E (x != j) //x is 5 and y is 6. 5 is not equal to 6, returns true{ //...Código...}

logical operators[To edit|edit source]

the operatorsY(can also be written as&&) Yo(can also be written as||) allow the concatenation of two or more conditions. HeYThe operator checks that all conditions are true and theoThe operator checks if at least one of the conditions is true. Both operators can also be mixed, where the order they appear from left to right determines how checks are performed. Earlier versions of the C++ standard used the keywords&&Y||rather thanYYo. Both operators are consideredshort circuit. and a previousYCondition is false, subsequent conditions are not checked. If a previousoCondition is true Postconditions are not checked.

To use:
The iso646.h header file has been part of the C standard library since 1995 as a supplement to the C90 standard. Defines a set of macros that allow programmers to use bitwise C language logical operators in text form that cannot be quickly or easily typed on some international, non-QWERTY keyboards without the header file. These symbols are keywords in the ISO C++ programming language and do not require a header file. However, for consistency, the C++98 standard provides the <ciso646> header. In MS Visual Studio, which has implemented non-standard language extensions in the past, this is the only way to enable these keywords (via macros) without disabling the extensions.

Heno(can also be written as!The ) operator is used to return the inverse of one or more conditions.

  • Syntax:
condition1Ycondition2condition1ocondition2no Illness
  • examples:


If something must not be right. It is often combined with other conditions. If x>5 but not x=10, you would write:

E ((x > 5) Y no (x == 10)) // if (x greater than 5) and (not (x equals 10)){ //...Código...}

When all conditions must be true. If x must be between 10 and 20:

E (x > 10 Y x < 20) // if x is greater than 10 and x is less than 20{ //....Código...}

When at least one of the conditions must be true. If x must be equal to 5 or equal to 10 or less than 2:

E (x == 5 o x == 10 o x < 2) // if x equals 5 or x equals 10 or x is less than 2{ //...Código...}

Whether at least one condition in a group of conditions must be true. Whether x must be between 10 and 20 or between 30 and 40.

E ((x >= 10 Y x <= 20) o (x >= 30 Y x <= 40)) // >= -> greater than or equal to etc...{ //...Código...}

With more conditions it gets a little more complicated. The trick is to make sure the square brackets are in the right places to establish the intended thought order. However, when things get this complex it can often be easier to break the logic into nested if statements or put them in boolean variables, but it's still useful to be able to do things in complex boolean logic.

stick aroundX > 10and aroundX < 20they are implied like that<Operator has higher priority thanY. Firstxcompared to 10. Ifxis greater than 10,xis compared to 20, and ifxis also less than 20, the code will be executed.

S (&&)[To edit|edit source]

statement1statement2Y
TTT
TFF
FTF
FFF

The AND logical operator,Y, compares the value on the left and the value on the right. yes bothstatement1Ystatement2are true, the expression returns TRUE. Otherwise, FALSE is returned.

E ((var1 > var2) Y (var2 > var3)){ Standard::cout << var1 " is bigger than " << var2 << "Y" << var3 << Standard::fin;}

In this section, theEstatement checks ifvar1is bigger thanvar2. Then it checks whethervar2is bigger thanvar3. If so, keep telling usvar1is bigger than bothvar2Yvar3.

To use:
The logical AND operatorYis sometimes written as&&, which is not the same as the direction operator and the bitwise AND operator, which are represented by&

o (||)[To edit|edit source]

statement1statement2o
TTT
TFT
FTT
FFF

The OR logical operator is represented byo. Like the logical AND operator, it comparesstatement1Ystatement2. if there arestatement1ostatement2are true, then the expression is true. The expression is true even if both statements are true.

E ((var1 > var2) o (var1 > var3)){ Standard::cout << var1 " is bigger than " << var2 << "o" << var3 << Standard::fin;}

Let's look at the expression above with an OR operator. Yesvar1is bigger than bothvar2ovar3or both, the statements in theEexpression to be executed. Otherwise, the program continues with the rest of the code.

no (!)[To edit|edit source]

The logical operator NOT,no, returns TRUE if the statement being compared is not true. Be careful when using the NOT operator and any logical operators.

no x > 10

Logical expressions have higher precedence than normal operators. Therefore, a comparison is made to see if "not x" is greater than 10. However, this statement always returns false, no matter what "x" is. This is because logical expressions only return Boolean values ​​(1 and 0).

Top Articles
Latest Posts
Article information

Author: Aron Pacocha

Last Updated: 03/26/2023

Views: 6299

Rating: 4.8 / 5 (48 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Aron Pacocha

Birthday: 1999-08-12

Address: 3808 Moen Corner, Gorczanyport, FL 67364-2074

Phone: +393457723392

Job: Retail Consultant

Hobby: Jewelry making, Cooking, Gaming, Reading, Juggling, Cabaret, Origami

Introduction: My name is Aron Pacocha, I am a happy, tasty, innocent, proud, talented, courageous, magnificent person who loves writing and wants to share my knowledge and understanding with you.