Control Flows
In programming, executing a specific code section is often desirable based on a condition known only during the program execution. Java offers control flow statements to address such programming requirements. It enables us to create a program that can make a decision.
We will cover the following different types of control flow statements:
- if statement
- if-else statement
- Nested if-else statements
- Chaining if-else statement
- switch statement
› if statement¶
The if statement is the simplest decision-making statement. It is used to decide whether a block of code will be executed or not based on the condition result.
| Syntax | |
|---|---|
Note that the condition can be any of the following that results in a single boolean value:
| condition | Example |
|---|---|
| boolean data values | true false |
| relational operators | 10 >= 20 , 3 < 90 |
| logical operators | true && false, true ll false, !false |
| boolean expression | !(10 >= 20 ll 3 < 90) |
Suppose the condition results in true boolean value, the code below the if statement will be executed. However, if the condition results in a false value, then the entire if statement code will be ignored.
| Sample Code | |
|---|---|
Type & Run the Sample Code 👩💻
⚡For long lines of code, please click the
Edit this program in JDoodle.comlink
One of the common syntax error with if-statement is placing the ; right after the condition.
Subtle Mistake
The semicolon ; separates the condition with its code block { } and makes them isolated code segments. As you can see from the above example, "Hello" gets printed out regardless of false condition.
› if-else statement¶
The if-else statement is helpful when we want to have two choices where only one of them will be executed based on the result of a condition.
| Syntax | |
|---|---|
The if-else statement executes if code block when condition is true. If the condition is false, the else code block will be executed.
In the following sample code, we have a condition homeworkDone && roomCleaned which evaluates to true. Hence, code inside the body of if-block is executed.
| Sample Code | |
|---|---|
Type & Run the Sample Codes 👩💻
⚡For long lines of code, please click the
Edit this program in JDoodle.comlink
› Nested if-else statements¶
It is also possible to have if or if-else statements inside another if-else statement. It's called a nested statements. It is equivalent to having options inside an option. Remember that inside any code block marked by { and }, you can create any number of if or if-else statements.
Even though a nested if-else statement seems intimidating, if you follow the execution flow from top to down fashion, it is surprisingly easy to understand.
› Chained if-else statement¶
It is if-else statements chained together with multiple conditions. Each condition gets evaluated from top to bottom until one of them is true. As soon as one of the conditions evaluates to true, the corresponding block will be executed, and the rest will be ignored.
Please see the following example execution for execution flow:
Chained if-else executions
You can use this flow diagram to understand any chained if-else statement execution flow:
flow diagram
Try this code sample on your test case:
Type & Run the Sample Code 👩💻
⚡For long lines of code, please click the
Edit this program in JDoodle.comlink
› switch Statement¶
The switch statement is a straightforward replacement for the chaining if-else statement. However, it has some limitations, and it is error-prone. Therefore, I recommend you guys use a chaining if-else statement instead.
The expression is evaluated once and compared with the values of each case.
- If the expression matches with
value1, the code of the case value1 is executed. Similarly, the code of casevalue2is executed if the expression matches with value2. - If there is no match, the code of the
defaultcase is executed.
Use this flow chart to understand the execution flow:
flow diagram
Try this code sample:
Type & Run the Sample Code 👩💻
⚡For long lines of code, please click the
Edit this program in JDoodle.comlink