We just went over operators, mostly arithmetic. But there are other operators we can use to compare values of variables as well! Enter the relational operators:
Relational Operators
Operator
Meaning
Usage
<
Less than
a < b
>
Greater than
a > b
⇐
Less or equal to
a ⇐ b
>=
Greater or equal to
a >= b
!=
Not equal to
a != b
==
Equal to
a == b
!
Negation/Not
!a
Remember how we talked about boolean data types, and how they can only be one of two values (which ones?).
Well… when do we actually ever SEE boolean data in our code? What is it used for?
We may want to record the result of a comparison between two different pieces of data (say, two integers, two floats, etc). This is called a condition!
Conditions
(evaluate to a value of TRUE or FALSE, and are stored in bool variables)
The value of the bool variable condition1 is true, because 5 is less than 10.
The value of the bool variable condition2 is false, because 20 is not equal to 30.
The value of the bool variable condition3 is true, because 45.8f is greater than or equal to 45.8f.
The value of the bool variable condition4 is false, because we are assigning the opposite value of condition3 to condition 4, due to the negation operator.
WARNING
The ’=’ is the ASSIGNMENT OPERATOR, used to assign a value to a variable.
The ’==’ is the EQUALITY OPERATOR, used to check for equality between two values.
Guess what? These are two COMPLETELY DIFFERENT operations, and you WILL get them mixed up.
Know the difference!
Logical Operators
Operator
Meaning
Usage
Description
&&
AND
a && b
(a && b) is true only if both a and b are true.
||
OR
a || b
(a || b) is true if either a or b are true.
We can combine conditions using the logical operators! The truth table below can help with understanding how the conditions above work:
The value of the bool variable condition5 is false:
5 is less than 10 → this is true
20 is equal to 30 → this is false
Since one of these is false, the entire thing is false, meaning the value assigned to condition5 is false
The value of the bool variable condition6 is true:
5 is less than 10 → this is true
20 is equal to 30 → this is false
Since one of these is true, the entire thing is true, meaning the value assigned to condition6 is true
If Statement
(for only running code if a condition is true)
if(/*condition*/){ //your code here! //this runs if the condition is true}
If Statement, Examples
int x = 5;if(x < 20){ x = 100;}
Result
Once this code is done, the integer variable x will have the value 100. When the condition is evaluated, x has a value of 5. Because the condition (x < 20) is true, the code inside the curly braces will be executed, meaning x will be assigned a value of 100.
Else Statement
(for running code when a condition is false, only pairs with an If statement)
if(/*condition*/){ //your code here! //this runs if the condition is true}else{ //your code here! //this runs if the condition is false}
Else Statement, Examples
bool myCondition = (10 <= 0);if(myCondition){ printf("IF IF IF IF IF");}else{ printf("ELSE ELSE ELSE ELSE");}
Output/Result
ELSE ELSE ELSE ELSE
The value of myCondition is false, because 10 is not less than or equal to 0. The code inside the first set of curly braces won’t run. Since the else statement is paired with the if statement and myCondition was false, the code in the second set of curly braces will run.
bool anotherCondition = ((0 == 0) && (1000 > -1000));if(anotherCondition){ printf("IF IF IF IF IF");}else{ printf("ELSE ELSE ELSE ELSE");}
Output/Result
IF IF IF IF IF
The value of anotherCondition is true, because 0 is equal to 0 AND 1000 is greater than -1000. The code inside the first set of curly braces will run. Since the else statement is paired with the if statement and anotherCondition was true, the code in the second set of curly braces will not run.
Incorrect ways to use Else
Remember, the code below is NOT correct
We didn’t pair the else statement below with an if statement
//code, not importantint x = 5;else{ //Wrong! We didn't pair this with an If statement}
Remember, the code below is NOT correct
Since there is code between the if/else statements, we didn’t pair the else statement below with an if statement
if(/*condition*/){ //code}int x = 5; //You can't put these here! Now the if/else statements aren't paired!else{ //Wrong! We didn't pair this with an If statement}
Else If
We can actually combine the If statement and the Else statement into a single thing, called an else if statement. This allows us to expand our check beyond just a yes/no answer to the first if statement:
if(/* condition 1 */){ //your code here! //this runs if condition 1 is true}else if(/* condition 2 */){ //your code here //this runs if condition 1 is false, and condition 2 is true}else{ //your code here! //this runs if all previous conditions (1 and 2) are false}
Else If, Examples
if(false){ printf("IF IF IF IF IF");}else if(176 == (175+1)){ printf("MIDDLE!");}else{ printf("ELSE ELSE ELSE ELSE");}
Output/Result
MIDDLE
The first condition fails because it is always false.
The second condition will evaluate to true, because 176 is equal to (175+1).
Nested If Statements
You can also put if statements inside each other, like so:
int x = 42;if(x > 40){ printf("Whoa, over 40!"); if(x > 50) { printf("Wow, greater than 50!"); } else { printf("Ah, still shy of 50..."); }}
Output/Result
Whoa, over 40!Ah, still shy of 50...
The outer condition is true because x is greater than 40, so the code inside the block will be executed
The first printf call will run.
The second condition is false, since x is less than 50.
The else statement paired with the inner if statement will run, executing the second printf call.
The language shouldn’t limit the number of if statements that can be nested inside each other. However, nesting too many statements will quickly turn your code into an unreadable mess!
Nested if statements and if/else will be very useful, but it may not be immediately clear WHY they’re useful. As we run into more complicated examples, they’ll be used for more complex combinations of conditions that would be difficult to handle otherwise.