Session 2 |
Logic control structures
A boolean is a primitive data type used in boolean expressions of logic control structures.
Boolean Expressions
Usually a condition is a boolean expression - a combination of literals, operators, variable names, and parentheses used to evaluate variables and values. Boolean expressions often operate on the primitive data types (e.g. numbers, chars) using relational operators:
Operator | Example | Equivalent |
== | b1== b2 | returns true if both the left side and right side are equal |
!= | b1!= b2 | returns true if left side is not equal to the right side of operator |
> | b1> b2 | returns true if left side is greater than right |
< | b1< b2 | returns true if left side is less than right side |
>= | b1>= b2 | returns true if left side is greater than or equal to right side |
<= | b1<= b2 | returns true if left side is less than or equal to right side |
equals | b1.equals (b2) | to compare two strings |
public class RelationalDemo { public static void main(String args[]) { int num1 = 10; int num2 = 50; if (num1 == num2) { System.out.println("num1 and num2 are equal"); } if( num1 != num2 ){ System.out.println("num1 and num2 are not equal"); } } }
Two-way decision
An outline of a two-way decision structure:
// statements that are executed before the decision if ( condition ){ // true branch } else{ // false branch } // statements that are executed after the the if-structure
Here are some details:
- The condition is a boolean expression which compares values; the result of the evaluation is either true or false.
- The else separates the true branch from the false branch.
- The true branch can be a block and the false branch can be a block; a block consists of several statements inside a pair of braces
{}
. - When a block is chosen for execution, the statements in it are executed one by one.
- The statements after the false branch (after line 8) are always executed.
Note that a condition in the if statement can be a single expression or a combination of expressions - the latter simplifies and shortens the code. Java provides three logical operators that combine boolean expressions:
Operator | Example | Explanation |
&& | b1&& b2 | returns true if both b1 and b2 are true else it would return false |
|| | b1|| b2 | returns false if both b1 and b2 are false else it would return true |
! | ! b1 | returns the opposite of b1, that means it would be true if b1 is false and it would return false if b1 is true |
// statements that are executed before the decision if ( a == 6 && b == 7 ){ System.out.println(a*b); } else { System.out.println(a/b); }
Three-way decision
// statements that are executed before the decision if ( condition 1 ){ // true branch } else if ( condition 2 ){ // alternative branch } else{ // false branch } // statements that are executed after the if-structure
PS! Only one of these blocks will get executed, and then Java jumps out of the conditional structure. Plan tests properly to avoid overlapping or invalid behaviour of the program.
Nested decisions
Switch operator
The three- or multi-way conditional structures can be substituted by operator switch
:
switch ( expression ) { case value1: //statements; break; case value2: //statements; break; … default: //statements if none of the cases has been executed; }
PS! Be careful with the keyword break
. If we forget to add it, then Java will not jump out of the conditional structure and continue checking the following condition(s).
An example (part of the code):
int weekDay = 4; switch (weekDay) { case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; case 3: System.out.println("Wednesday"); break; case 4: System.out.println("Thursday"); break; case 5: System.out.println("Friday"); break; case 6: System.out.println("Saturday"); break; case 7: System.out.println("Sunday"); break; default: System.out.println("Invalid number"); }
Session 2 |