Flow Control refers to the order in which individual statements, instructions, or function calls are executed in a programming language. Java provides several structures to control the flow of a program. These include decision-making constructs, loops, and branching statements that enable programmers to control how the code executes based on various conditions.
We'll cover each flow control concept, from basic to advanced, with examples.
In the sequential flow, the program executes statements one by one, in the order in which they appear. This is the most basic form of flow control, where the execution flows sequentially from top to bottom.
xxxxxxxxxx
public class SequentialExample {
public static void main(String[] args) {
int x = 10; // Step 1
System.out.println("Value of x: " + x); // Step 2
x = x + 5; // Step 3
System.out.println("Updated value of x: " + x); // Step 4
}
}
Value of x: 10
Updated value of x: 15
Here, each line executes in sequence, with no branching or loops.
Decision-making statements in programming are constructs that allow a program to execute different sets of instructions based on certain conditions. Here's a concise definition:
Decision-making statements are programming constructs that enable conditional execution of code based on the evaluation of specified criteria. They allow programs to make choices and follow different paths of execution depending on whether certain conditions are true or false.
The most common types of decision-making statements include:
The if
statement allows the program to make a decision based on a condition. If the condition is true, the code inside the if
block is executed; otherwise, it is skipped.
Syntax:
if (condition) {
// Code to execute if condition is true
}
xxxxxxxxxx
public class IfExample {
public static void main(String[] args) {
int number = 10;
if (number > 5) {
System.out.println("Number is greater than 5");
}
}
}
Number is greater than 5
The if-else
statement provides an alternative path if the condition in the if
statement is false.
Syntax:
if (condition) {
// Code to execute if condition is true
} else {
// Code to execute if condition is false
}
xxxxxxxxxx
public class IfElseExample {
public static void main(String[] args) {
int number = 3;
if (number > 5) {
System.out.println("Number is greater than 5");
} else {
System.out.println("Number is not greater than 5");
}
}
}
Number is not greater than 5
The if-else if ladder is used when there are multiple conditions to check, one after the other.
Syntax:
if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition2 is true
} else {
// Code to execute if none of the conditions are true
}
xxxxxxxxxx
public class IfElseIfExample {
public static void main(String[] args) {
int number = 10;
if (number < 5) {
System.out.println("Number is less than 5");
} else if (number == 10) {
System.out.println("Number is equal to 10");
} else {
System.out.println("Number is greater than 5 and not equal to 10");
}
}
}
Number is equal to 10
The switch
statement allows you to execute different code blocks based on the value of a variable or expression. It is often used as a more readable alternative to multiple if-else if
statements.
Syntax:
switch (expression) {
case value1:
// Code to execute if expression == value1
break;
case value2:
// Code to execute if expression == value2
break;
default:
// Code to execute if none of the cases match
}
xxxxxxxxxx
public class SwitchExample {
public static void main(String[] args) {
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");
}
}
}
Wednesday
The switch
statement checks the value of the day
variable and matches it to the appropriate case. If no case matches, the default
block is executed.