Session 2 |
Logical control structures
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 branches join together again
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.
Boolean Expressions
Usually the 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 | Meaning |
A == B | is A equal to B ? |
A < B | is A less than B ? |
A <= B | is A less than or equal to B ? |
A > B | is A Greater than B ? |
A >= B | is A Greater than or equal to B ? |
A != B | is A not equal to B ? |
We will learn a bit later how to evaluate Strings.
Combination of Expressions
The condition in the if statement can be a single expression or a combination of expressions. Java provides three logical operators that combine boolean expressions:
Operator | Name | Example | Evaluates to true if and only if… |
&& | and | a && b | a AND b are both true |
|| | or | a||b | a OR b (or both) are true |
! | not | !a | a is NOT true |
Example:
// 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 ){ // nested true branch } else{ // nested false branch } // statements that are executed after the branches join together again
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 |