A Control Flow statement in Java determines the order in which individual statements, instructions, or function calls are executed or evaluated. These statements enable decision-making, looping, and branching in programs, thus controlling the flow of execution.
Control flow statements are essential for building logic in a program, allowing you to execute code conditionally, repeatedly, or in a specific sequence.
Types of Control Flow Statements in Java:
- Conditional Statements (Decision-making):
These statements allow the program to make decisions and execute certain blocks of code based on conditions.
2. if
Statement: Executes a block of code if a condition is true
.
java
if (condition) {
// Code to be executed if the condition is true
}
3. if-else
Statement: Executes one block of code if a condition is true
, and another block if it is false
.
java
if (condition) {
// Code if true
} else {
// Code if false
}
4. switch
Statement: Tests a variable against a list of values and executes the corresponding block of code.
java
switch (expression) {
case value1:
// Code for value1
break;
case value2:
// Code for value2
break;
default:
// Code if no match
}
- Looping Statements (Iteration):
Looping statements allow code to be executed repeatedly based on a condition or counter.
2. for
Loop: Repeats a block of code a specific number of times.
java
for (int i = 0; i < 5; i++) {
// Loop body
}
3. while
Loop: Repeats a block of code while a condition is true
.
java
while (condition) {
// Loop body
}
4. do-while
Loop: Similar to the while
loop, but the block of code is executed at least once, as the condition is checked after the loop body.
java
do {
// Loop body
} while (condition);
- Branching Statements:
These statements allow the program to jump to different parts of the code.
2. break
: Exits the loop or switch statement immediately.
java
break; // Exits the loop or switch
3. continue
: Skips the current iteration of a loop and proceeds with the next iteration.
java
continue; // Skips the rest of the loop body
4. return
: Exits from the current method and optionally returns a value.
java
return value; // Exits a method and returns a value
Example:
Using if-else
and for
Loop:
public class Example { public static void main(String[] args) { int num = 5;
// Conditional statement
if (num > 0) {
System.out.println("Positive number");
} else {
System.out.println("Non-positive number");
}
// Looping statement
for (int i = 0; i < num; i++) {
System.out.println("Count: " + i);
}
}
}
Why Control Flow Statements are Important:
- They enable decision-making within the program.
- They allow code to be executed multiple times or selectively based on conditions.
- They make the program flexible, adaptable, and efficient by controlling which parts of the code are executed and in what order.
Key Points:
- Conditional statements (
if-else
,switch
) allow decision-making in programs. - Looping statements (
for
,while
,do-while
) enable code repetition. - Branching statements (
break
,continue
,return
) control the flow by jumping between different parts of the code.
In conclusion, control flow statements are essential building blocks of Java programs, helping manage the sequence of execution, repetition, and decision-making within a program. They allow developers to create dynamic, responsive, and efficient code.