1.4 Control Flow
Control flow in Java refers to the order in which statements are executed in a program. It allows you to control the flow of execution based on certain conditions. Understanding control flow is crucial for writing dynamic and responsive Java applications.
Key Concepts
1. Conditional Statements
Conditional statements allow you to execute different blocks of code based on whether a condition is true or false. The primary conditional statements in Java are if
, if-else
, and switch
.
1.1 if Statement
The if
statement executes a block of code if a specified condition is true.
int x = 10; if (x > 5) { System.out.println("x is greater than 5"); }
1.2 if-else Statement
The if-else
statement executes one block of code if a condition is true and another block if the condition is false.
int x = 10; if (x > 15) { System.out.println("x is greater than 15"); } else { System.out.println("x is 15 or less"); }
1.3 switch Statement
The switch
statement allows you to select one of many code blocks to be executed based on the value of a variable.
int day = 3; switch (day) { case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; case 3: System.out.println("Wednesday"); break; default: System.out.println("Other day"); }
2. Loops
Loops allow you to execute a block of code repeatedly. Java supports three types of loops: for
, while
, and do-while
.
2.1 for Loop
The for
loop is used to execute a block of code a specific number of times.
for (int i = 0; i < 5; i++) { System.out.println("Iteration: " + i); }
2.2 while Loop
The while
loop executes a block of code as long as a specified condition is true.
int i = 0; while (i < 5) { System.out.println("Iteration: " + i); i++; }
2.3 do-while Loop
The do-while
loop is similar to the while
loop, but it guarantees that the code block will be executed at least once.
int i = 0; do { System.out.println("Iteration: " + i); i++; } while (i < 5);
3. Jump Statements
Jump statements allow you to alter the normal flow of control in a loop or switch statement. The primary jump statements in Java are break
and continue
.
3.1 break Statement
The break
statement terminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch.
for (int i = 0; i < 10; i++) { if (i == 5) { break; } System.out.println("Iteration: " + i); }
3.2 continue Statement
The continue
statement skips the current iteration of the loop and continues with the next iteration.
for (int i = 0; i < 10; i++) { if (i % 2 == 0) { continue; } System.out.println("Odd iteration: " + i); }
Examples and Analogies
Think of control flow as a road map for your program. Conditional statements are like road signs that direct the program based on certain conditions. Loops are like roundabouts that keep the program going in circles until a condition is met. Jump statements are like shortcuts that allow the program to bypass certain parts of the road map.
By mastering control flow, you can create more dynamic and responsive Java applications that adapt to different conditions and scenarios.