Java Course
Lets see the flow controls available in Java.
- if-else Branching: Allows conditional execution based on whether a condition is true or false.
- switch Statements: Used for multiple discrete values, executing code based on the value of an expression.
- while Loop: Repeats code as long as a condition is true.
- do-while Loop: Guarantees at least one execution of the code block and then continues based on the condition.
- for Loop: Provides a compact way to iterate over a range of values with initialization, condition, and update expressions.
- break and continue: Control the flow within loops and switch statements, allowing early exits or skipping iterations.
I. if-else Branching
The if-else statement allows you to execute different code blocks based on a condition. It is one of the fundamental constructs for decision-making in Java.
Basic Syntax
if (condition) { // Code to execute if condition is true } else { // Code to execute if condition is false }
Example
int number = 10; if (number > 0) { System.out.println("The number is positive."); } else if (number < 0) { System.out.println("The number is negative."); } else { System.out.println("The number is zero."); }
Explanation: The if block checks if the number is greater than zero. If true, it prints "The number is positive." If not, it checks if the number is less than zero. If this condition is true, it prints "The number is negative." If neither condition is true, the else block executes, printing "The number is zero."
II. switch Statements
The switch statement is a control mechanism that allows a variable to be tested for equality against a list of values. It's often used when you have multiple discrete values.
Basic Syntax
switch (expression) { case value1: // Code to execute if expression equals value1 break; case value2: // Code to execute if expression equals value2 break; // Additional cases... default: // Code to execute if expression doesn't match any case }
Example
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("Invalid day"); }
Explanation: The switch statement evaluates the day variable. It matches the value against the case labels. For day = 3, it matches case 3 and prints "Wednesday." If no match is found, the default block executes.
III. Loops and Iterators
Loops allow you to execute a block of code repeatedly. Java provides several types of loops for different scenarios.
while Loop
The while loop repeatedly executes a block of code as long as a specified condition is true.
Basic Syntax
while (condition) { // Code to execute while condition is true }
Example
int count = 0; while (count < 5) { System.out.println("Count is: " + count); count++; }
Explanation: The while loop continues to execute the code block as long as count is less than 5. It prints the value of count and increments it in each iteration.
do-while Loop
The do-while loop is similar to the while loop, but it guarantees that the code block executes at least once, regardless of the condition.
Basic Syntax
do { // Code to execute } while (condition);
Example
int count = 0; do { System.out.println("Count is: " + count); count++; } while (count < 5);
Explanation: The do-while loop executes the code block once before checking the condition. It continues to execute as long as count is less than 5.
for Loop
The for loop provides a concise way to iterate over a range of values. It's typically used when the number of iterations is known beforehand.
Basic Syntax
for (initialization; condition; update) { // Code to execute }
Example
for (int i = 0; i < 5; i++) { System.out.println("i is: " + i); }
Explanation: The for loop initializes i to 0, checks if i is less than 5, and then increments i by 1 after each iteration. The loop prints the value of i in each iteration.
IV. Using break and continue
Both break and continue statements control the flow of loops.
break Statement
The break statement exits from the innermost loop or switch statement.
Example
for (int i = 0; i < 10; i++) { if (i == 5) { break; } System.out.println("i is: " + i); }
Explanation: The loop prints the value of i until i equals 5. When i is 5, the break statement terminates the loop.
continue Statement
The continue statement skips the current iteration of a loop and proceeds with the next iteration.
Example
for (int i = 0; i < 10; i++) { if (i % 2 == 0) { continue; // Skip the rest of the loop body for even numbers } System.out.println("i is: " + i); }
Explanation: The loop prints only odd numbers. When i is even, the continue statement skips the remaining code in the loop body and proceeds to the next iteration.