Leela Soft Core Java (J2SE) Madhusudhan
Control Flow Statements:
✓ The statements inside our source files are generally executed from top to bottom, in
the order that they appear.
✓ Control flow statements, however, break up the flow of execution by employing
decision making, looping, and branching, enabling our program to conditionally
execute particular blocks of code.
Control flow statements, supported by the Java programming language:
1. The decision-making statements (if-then, if-then-else, switch)
2. The looping statements (for, while, do-while)
3. The branching statements (break, continue, return)
1. The decision-making statements (Conditional Statements)
The Simple IF Statement
• The Simple IF statement is the most basic of all the control flow statements.
• It tells our program to execute a certain section of code only if a particular
test_condition evaluates to true.
• If this test_condition evaluates to false, control jumps to the end of the if statement.
Syntax:
if (condition) {
statement 1; //executes when condition is true
}
[Link]@[Link] Cell: 78 42 66 47 66
Leela Soft Core Java (J2SE) Madhusudhan
Example:
public class Test {
public static void main(String[] args) {
int x = 15;
int y = 12;
if (x > y) {
[Link]("x is greater than y");
}
}
}
Output: x is greater than y.
The if-else Statement
• The Java if-else statement also tests the condition. It executes the if block if condition
is true otherwise else block is executed.
Syntax:
if(condition) {
statement 1; //executes when condition is true
}
else{
statement 2; //executes when condition is false
}
Example:
public class Test {
public static void main(String[] args) {
int x = 10;
int y = 12;
if (x > y) {
[Link]("X is Big");
} else {
[Link]("Y is Big");
}
}
}
The if-else-if ladder:
• The if-else-if statement contains the if-statement followed by multiple else-if
statements.
• In other words, we can say that it is the chain of if-else statements that create a
decision tree where the program may enter in the block of code where the condition
is true. We can also define an else statement at the end of the chain.
Syntax:
if(condition1) {
[Link]@[Link] Cell: 78 42 66 47 66
Leela Soft Core Java (J2SE) Madhusudhan
statement 1; //executes when condition 1 is true
}
else if(condition2) {
statement 2; //executes when condition 2 is true
}
else {
statement 2; //executes when all the conditions are false
}
Example:
public class Test {
public static void main(String[] args) {
String city = "Noida";
if (city == "Meerut") {
[Link]("city is meerut");
} else if (city == "Noida") {
[Link]("city is noida");
} else if (city == "Agra") {
[Link]("city is agra");
} else {
[Link](city);
}
}
}
The Nested if-statement
• In nested if-statements, the if statement can contain an if or if-else statement inside
another if or else-if statement.
The switch Statement
1. The Java switch statement executes one statement from multiple conditions. It is like
if-else-if ladder statement.
2. A switch works with the byte, short, char, and int primitive data types.
Example:
public class Test {
public static void main(String[] args) {
int a = 1;
switch (a) {
case 1:
[Link]("John");
break;
case 2:
[Link]("Miller");
break;
case 3:
[Link]("Scott");
[Link]@[Link] Cell: 78 42 66 47 66
Leela Soft Core Java (J2SE) Madhusudhan
break;
default:
[Link]("Default Case");
break;
}
}
}
Points to Remember
• There can be one or N number of case values for a switch case.
• The case values must be unique. In case of duplicate value, it renders compile-time
error.
• Each case statement can have a break statement which is optional.
• The case value can have a default label which is optional.
2. Loops in Java
• Looping in programming languages is a feature which facilitates the execution of a set
of instructions/functions repeatedly while some condition evaluates to true.
• Java provides three ways for executing the loops.
• They are while loop, do-while loop and for loop.
The while Loop:
• The while Loop continually executes a block of statements while a particular
condition is true.
Syntax:
while (condition) {
statement(s)
}
Example:
public class Test {
public static void main(String[] args) {
int i = 1;
while (i < 6) {
[Link]("Value is: " + i);
i++;
}
}
}
[Link]@[Link] Cell: 78 42 66 47 66
Leela Soft Core Java (J2SE) Madhusudhan
The do-while Loop:
• The do-while evaluates its expression at the bottom of the loop instead of the top.
• Therefore, the statements within the "do" block are always executed at least once.
Syntax:
do {
statement(s)
} while (condition);
Example:
public class Test
{
public static void main(String[] args)
{
int i = 1, n = 5;
do
{
[Link](i); //1
i++; //2
}
while (i<6); //
}
}
[Link]@[Link] Cell: 78 42 66 47 66