This is one of the big ones when it comes to programming. I use these ALL THE TIME in my code. It’s actually hard to find code without them. It wouldn’t hurt to read through the entirety of this page multiple times to really grasp how each of these elements fit together.
Page Summary:
- What are Boolean Expressions?
- What are Relational Operators?
- ==, !=, >, <, >=, <=
- What are Boolean/Logical Operators?
- AND(&&), OR(||) and NOT(!)
- What is the Conditional Operator? (?)
- What are Truth Tables?
- What are If-Else statements in Programming?
- What are Switch statements in Programming?
What are Boolean Expressions?
Boolean Expressions are expressions that evaluate to either TRUE or FALSE.
The most common types of boolean expressions are those that use relational operators. The general syntax of a conditional statement (which is a type of boolean expression) is the following:
<expression> <relational operator> <expression>
Note: The two expressions above MUST match in type
What are Relational Operators?
The 6 Relational Operators that we are going to cover here:
- Equal To (==)
- Not Equal To (!=)
- Greater Than (>)
- Greater Than OR Equal To (>=)
- Less Than (<)
- Less Than OR Equal To (<=)
For sake of simplicity, we are only going to be comparing numerical values, as comparing other data types (such as Strings, Dates, Arrays, etc.) create complexity. Data types that are not numerically based require other functions/methods for accurate expressions, such as String.equals(anotherString). I will include a few simple examples of these, but nothing too crazy.
Let’s take a look at some examples, first look at this code:
char jay;
int a,b;
jay = 'j';
x = 12;
y = 4;
Now let’s take some expressions, and try to solve them:
x + y > 20;
jay == 'k';
jay == 'j';
x >= 12;
y < 4;
x != 3;
y == 4;
Before looking at the answers below, try to solve them on your own.
Which lines evaluate to TRUE and which lines evaluate to FALSE?
- Line 1 is FALSE, because (x + y) equals 16, and is NOT greater than 20
- Line 2 is FALSE, because jay equals ‘j’ NOT equal to ‘k’
- Line 3 is TRUE, because jay equals ‘j’ which is equal to ‘j’
- Line 4 is TRUE, because x equals 12, which is greater than or equal to 12
- Line 5 is FALSE, because y equals 4, which is NOT less than 4
- Line 6 is TRUE, because x equals 12, which is NOT equal to 3
- Line 7 is TRUE, because y equals 4, which is equal to 4
What are Boolean/Logical Operators?
Boolean/Logical Operators are operators that take boolean expressions as operands. Here are some common boolean operators, for example:
AND also referred to as &&
OR also referred to as ||
NOT also referred to as !
The first two take two boolean expressions as operands, and the last takes only a single boolean expression as an operand.
Let EXP1 and EXP2 be boolean expressions. Here is the syntax for complex boolean expressions put together with the boolean operators mentioned above:
(EXP1) && (EXP2)
(EXP1) || (EXP2)
!(EXP1)
Now imagine instead of just EXP1 and EXP2, these were actual expressions. Let’s take some examples:
int x = 5;
int y = 10;
boolean test = FALSE;
test = (x + y) > 20;
test = (x > 1) && (y > 2)
test = (x <= 4) && (y > 2)
test = (x > 3) || (y > 2)
test = (x > 6) || (y > 2)
test = !(x > 3)
Again, before looking at the answers below, try to figure it out on your own.
Which lines does the variable test equal TRUE, and which lines does the variable test equal FALSE?
- Line 5 : FALSE, this is because (x + y) is 15, which is NOT greater than 20
- Line 6 : TRUE, this is because (x > 1) is TRUE AND (y > 2) is TRUE
- (x > 1) is TRUE
- (y > 2) is TRUE
- TRUE && TRUE == TRUE
- Line 7 : FALSE, this is because both expressions, (x <= 4) and (y > 2), must be TRUE.
- (x <= 4) is FALSE
- (y>2) is TRUE
- FALSE && TRUE == FALSE
- Line 8 : TRUE, this is because at least one of the expressions are TRUE
- (x > 3) is TRUE
- (y > 2) is TRUE
- TRUE || TRUE == TRUE
- Line 9 : TRUE, this is because at least one of the expressions are TRUE
- (x > 6) is FALSE
- (y > 2) is TRUE
- FALSE || TRUE == TRUE
- Line 10 : FALSE, even though (x > 3) is TRUE, the NOT(!) operator negates it, therefore it is FALSE
- (x > 3) is TRUE
- !(x > 3) is FALSE
- !TRUE == FALSE
What is the Conditional Operator? (?)
No, I didn’t accidentally include an extra question mark in that title, that’s the actual operator itself.
?
This handy operator can be used in some languages as a nifty way to decide an if-else statement (coming later). Basically, it’s structured this way:
variable x = (expression) ? value if expression is FALSE : value if expression is FALSE
Let’s take some coding examples:
int x = 0;
int y = 4;
int z = 0;
z = (x == 0) ? 4 : 2;
z = (y > 5) ? 3 : 5;
z = (x + y >= 3) ? 1 : 6;
Before looking at the answers below, try to evaluate these yourself.
What is the value of z on lines 5 through 7?
- Line 5 : z equals 4, because (x == 0) evaluates to TRUE, therefore the 4 is saved to z
- Line 6 : z equals 5, because (y > 5) evaluates to FALSE, therefore the 5 is saved to z
- Line 7: z equals 1, because (x + y) is 4, and 4 is greater than or equal to 3, which evaluates to TRUE, therefore the 1 is saved to z
What are Truth Tables?
Truth tables are great tools to visualize how Boolean/Logical operators work, as well as individual expressions. Let’s take a super simple table and go through some examples:
| && | TRUE | FALSE |
| TRUE | TRUE | FALSE |
| FALSE | FALSE | FALSE |
Let’s go through each combination from this Truth Table to try and visualize what it’s doing. We’ll start from the top left, then make our way to the right, and then wrap around:
- TRUE && TRUE == TRUE
- TRUE && FALSE == FALSE
- FALSE && TRUE == FALSE
- FALSE && FALSE == FALSE
Basically, anything except TRUE && TRUE will equal false when it comes to the AND (&&) operator. Let’s take a look at another table:
| || | TRUE | FALSE |
| TRUE | TRUE | TRUE |
| FALSE | TRUE | FALSE |
Basically, anything that has TRUE on it will evaluate to true:
- TRUE || TRUE == TRUE
- TRUE || FALSE == TRUE
- FALSE || TRUE == TRUE
- FALSE || FALSE == FALSE
There are many applications for truth tables, but those will be covered in other sections later on. Possibly a Discrete Mathematics location. These are just to give a baseline approach to what’s going on between these expressions.
Okay, so all of these expressions are operators are just SO MUCH FUN, but where do we use them?
What are If-Else Statements in Programming?
If-Else statements are great for decision making in programming. Some programmers even say that all Artificial Intelligence is based off of just a bunch of If-Else statements. However, those programmers’ jobs were probably replaced by AI.
Anyways, let’s take a look at the fundamental structure of an If-Else construct:
if(<boolean expression) {
statement1;
}
else {
statement2;
}
statement3;
The above code is executed as follows:
- Check if the boolean expression is TRUE
- If the boolean expression is TRUE, then we will execute statement1
- If the boolean expression is FALSE, then we will execute statement2
- statement3 gets executed no matter what, since it is not inside either the if or the else block.
Let’s do another example, with real code this time:
int x = 5;
int y = 10;
if(x + y <= 30) {
x = 20;
y = 5;
}
else {
x = 0;
}
Okay, so let’s evaluate the expression first. (x+y) equals 15, which is less than or equal to 30, therefore the expression evaluates to TRUE.
From there, we execute the if block, which is x getting set to 20 and y getting set to 5. We do NOT execute the else block, so x is never set to 0.
What if I else’d another if?
Wow, that sentence hurts to read, but we are now going to introduce…
if-else-if-else structures.
These are very similar to the previous If-Else structure, but with a bonus, let’s take a look:
if(<boolean expression 1>){
statement1;
}
else if (<boolean expression 2>){
statement2;
}
else {
statement3;
}
Basically, if boolean expression 1 is TRUE, then statement1 gets executed. However, if boolean expression 1 is FALSE, it doesn’t execute statement1 and moves on to check boolean expression 2. If boolean expression 2 is TRUE, then statement2 gets executed. However, if boolean expression 2 is FALSE(and boolean expression 1 is FALSE, as we’ve stated), then statement2 does NOT get executed, and we move on to the final else, where statement3 gets executed.
You can include as many else-if’s as you want, but if you find yourself with more than 3, than it might be a good idea to re-evaluate your code or to use a switch stucture.
Another point to make is that you do not have to include an else statement or a final else statement, take a look at the following example:
if(<boolean expression>){
statement1;
}
if(<boolean expression 1>){
statement2;
}
else if (<boolean expression 2>){
statement3;
}
There are definitely times in programming where you might only want to do one thing / one block and not else somewhere else.
The last note I’m going to make about if statements, is that they can be nested.
Nested If-Else Statements are in a lot of code, and sometimes can get complicated, but let’s try to do a basic example. See the following code:
if(<boolean expression 1>) {
statement1;
if(<boolean expression 2>) {
statement2;
}
else {
statement3;
}
}
else if(<boolean expression 3>) {
statement4;
}
else {
statement5;
}
You’re probably already starting to notice where white space and indentation play an important part in organizing your code.
You’ll notice that the nested if statement is pushed outward farther than the others. This is to indicate to the reader that it is ‘deeper’ than the other if statements.
Let’s try to break this down.
If boolean expression 1 is TRUE, statement1 gets executed AND statement4 is NOT executed AND statement5 is NOT executed. This then leads to the inner if statement, which checks boolean expression 2, if boolean expression 2 is TRUE, statement2 gets executed, if boolean expression 2 is FALSE, statement3 gets executed.
However, if boolean expression 1 is FALSE, then the entire nested if block containing statement2 and statement3 gets skipped.
From there, we check if boolean expression 3 is TRUE, if boolean expression 3 is TRUE, then statement4 is executed.
If boolean expression 3 is FALSE, then everything is skipped and only statement5 gets executed.
What are Switch statements in Programming?
As much as I wish these structures were related to a Nintendo Switch, they’re not.
A programming Switch statement is much like an if-else-if-else structure, but with a few key differences, let’s take a look:
int x = 2;
switch(x){
case 0:
statement1;
break;
case 1:
statement2;
break;
case 2:
statement3;
break;
default:
statement4;
}
This is doing exactly what it describes, it switches to the correct case to execute when x is a certain value meeting that case’s value.
So if x is 0, then statement1 is executed
if x is 1, then statement2 is executed
if x is 2, then statement3 is executed
if x doesn’t meet any of the other cases (if x is not 0,1, or 2) then statement4 is executed.
You’re probably wondering what the break is. This is to exit out of the switch structure, because more than likely we are done testing. default doesn’t need a break because we are already at the end of the switch structure.
Thanks for Reading!…but please read it again
This is all I have so far on conditionals and structures. Please re-read through this page multiple times, you will have a much easier time programming. I’m not saying that to sound conceited, but these structures, expressions, and statements are used everywhere in the world of programming. It’s basically like having socks when running. Sure, you can run without them but it’ll be uncomfortable and everyone will say your shoes (code) smells.