Logical Operators
Java
Mastering the use of conditionals is one of the 5 fundamental skills for building algorithms:
Conditionals are the only way developers can tell the computer how to make decisions in real time, much like how the human brain works.
Let's say we're building a program to help us decide what to store and we hate the color blue. We can tell the computer to avoid blue using a condition like this:
If
color is not blue, then... do something.
Else
... do nothing or exit.
The decision statements are: if-then-else
and switch
.
if... then... else
The structure of if-then-else
statements is:
1if (expression) { 2 // Then block 3} else { 4 // Else block 5}
The expression in the if statement is evaluated. If the expression is true, the then block will execute, and if false, the else block will execute.
The else part is optional. In this case, we would have an if-then statement.
1if (expression) { 2 // Then block 3}
Here's an example:
1int value = 4; 2 3if (value < 10) { 4 System.out.println("The number is less than 10"); 5} else { 6 System.out.println("The number is greater than 10"); 7}
If-then-else statements can be nested, resulting in an if-then-else if statement with the following structure:
1if (expression) { 2 // Then block 3} else if { 4 // Else block 5} else if { 6 // Else block 7} else if { 8 // Else block 9} ...
This allows the following code:
1int value = 14; 2 3if (value < 10) { 4 System.out.println("The value is a single unit"); 5} else if (value < 100) { 6 System.out.println("The value is a decade"); 7} else if (value < 1000) { 8 System.out.println("The value is a hundred"); 9} else if (value < 10000) { 10 System.out.println("The value is a thousand"); 11} else { 12 System.out.println("It's a big number"); 13}
switch
For cases where there are many branches or execution paths in an if statement, we use the switch statement. The switch statement evaluates an expression and executes the block of code that matches the expression's value.
The value of the expression must be numeric. However, starting from Java SE 7, you can use expressions that evaluate to strings.
The structure of the switch statement is:
1switch (expression) { 2 case value1: 3 block1; 4 break; 5 case value2: 6 block2; 7 break; 8 case value3: 9 block3; 10 break; 11 ... 12 default: 13 default_block; 14}
It’s important to use the break
statement. The break statement exits the switch statement, preventing the evaluation of the remaining cases. Therefore, it is mandatory at the end of each block.
A clear example of when to use the switch statement is evaluating the value of a month numerically and converting it to a string. The code would look like this:
1int iMonth = 3; 2String sMonth; 3 4switch (iMonth) { 5 case 1: 6 sMonth = "January"; 7 break; 8 case 2: 9 sMonth = "February"; 10 break; 11 case 3: 12 sMonth = "March"; 13 break; 14 case 4: 15 sMonth = "April"; 16 break; 17 case 5: 18 sMonth = "May"; 19 break; 20 case 6: 21 sMonth = "June"; 22 break; 23 case 7: 24 sMonth = "July"; 25 break; 26 case 8: 27 sMonth = "August"; 28 break; 29 case 9: 30 sMonth = "September"; 31 break; 32 case 10: 33 sMonth = "October"; 34 break; 35 case 11: 36 sMonth = "November"; 37 break; 38 case 12: 39 sMonth = "December"; 40 break; 41 default: 42 sMonth = "Invalid month"; 43} 44 45System.out.println(sMonth);
The previous example was a simple condition, but in real life, choosing what to do involves combining multiple conditions to make the final decision. For example: Here’s an algorithm that tells a computer how to decide what to wear on Valentine's Day:
If you want to represent this algorithm in Java, it would look something like this:
1if (goingOut){ 2 if (canIGetBurger){ 3 if (placeBottleWine){ 4 if (coolMix){ 5 /* do something */ 6 } 7 }else{ 8 if (blazers > 3){ 9 /* do something */ 10 }else{ 11 /* do something */ 12 } 13 } 14 }else if (shePants){ 15 /* do something */ 16 }else{ 17 /* do something */ 18 } 19}else{ 20 if (nakedSheDoor){ 21 /* do something */ 22 }else if (blazers > 3){ 23 /* do something */ 24 }else{ 25 /* do something */ 26 } 27}