Control Statements.pdf
Control Statements.pdf
Java compiler executes the code from top to bottom. The statements in the code are executed according
to the order in which they appear. However, Java provides statements that can be used to control the
flow of Java code. Such statements are called control flow statements. It is one of the fundamental
features of Java, which provides a smooth flow of program.
if statements
switch statement
2.Loop statements
do while loop
while loop
for loop
for-each loop
3.Jump statements
break statement
continue statement
If Statement:
In Java, the "if" statement is used to evaluate a condition. The control of the program is diverted
depending upon the specific condition. The condition of the If statement gives a Boolean value, either
true or false. In Java, there are four types of if-statements given below.
1. Simple if statement
2. if-else statement
3. if-else-if ladder
4. Nested if-statement
1) Simple if statement:
It is the most basic statement among all control flow statements in Java. It evaluates a Boolean
expression and enables the program to enter a block of code if the expression evaluates to true.
Syntax of if statement
if(condition) {
Flowchart
program
int x = 10;
int y = 12;
}
}
Output:
x + y is greater than 20
2) if-else statement
The if-else statement is an extension to the if-statement, which uses another block of code, i.e., else
block. The else block is executed if the condition of the if-block is evaluated as false.
Syntax:
if(condition) {
else{
Flowchart:
program
{
public static void main(String[] args)
int x = 10;
int y = 12;
else
} Output:
x + y is greater than 20
3) 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(condition 1) {
else if(condition 2) {
}
else {
Flowchart:
program
if(name == "mahi")
System.out.println("name is mahi");
System.out.println("name is mani");
}
System.out.println("name is mohan");
else
System.out.println(name);
} Output:
vinay
4. Nested if-statement
Nested if in Java refers to having one if statement inside another if statement. If the outer
condition is true the inner conditions are checked and executed accordingly. Nested if condition
comes under decision-making statement in Java, enabling multiple branches of execution.
Normal if condition checks condition independently which means each condition works on its
own.
Whereas nested if checks conditions that depend on each other, which means one condition is
only checked if another condition is true.
Syntax
if (condition1) {
if (condition2) {
if (condition3) {
// statements;
}
If the outer condition satisfies then only the inner condition will be checked. Along with if condition, else
condition can also be executed.
Flow chart:
Program
if (number > 0)
if (number % 2 == 0)
}
else
else
Output:
switch statement
The switch statement allows us to execute a block of code among many alternatives.
Syntax:
switch (expression) {
case value1:
// code
break;
case value2:
// code
break;
...
...
default:
// default statements
The expression is evaluated once and compared with the values of each case.
If expression matches with value1, the code of case value1 are executed. Similarly, the code of case
value2 is executed if expression matches with value2
Flowchart:
program
class SwitchCase
int expression = 9;
switch(expression)
case 2:
System.out.println("Small Size");
break;
case 3:
System.out.println("Large Size");
break;
default:
System.out.println("Unknown Size");
Output
Unknown Size
Loops/Iteration statements:
1. for loop:
The for loop is used when we know the number of iterations (we know how many times we want to
repeat a task). The for statement consumes the initialization, condition, and increment/decrement in
one line thereby providing a shorter, easy-to-debug structure of looping.
Syntax:
// code to be executed
• Initialization condition: Here, we initialize the variable in use. It marks the start of a for
loop. An already declared variable can be used or a variable can be declared, local to
loop only.
• Testing Condition: It is used for testing the exit condition for a loop. It must return a
boolean value. It is also an Entry Control Loop as the condition is checked prior to the
execution of the loop statements.
• Statement execution: Once the condition is evaluated to true, the statements in the
loop body are executed.
• Increment/ Decrement: It is used for updating the variable for next iteration.
• Loop termination:When the condition becomes false, the loop terminates marking the
end of its life cycle.
program
import java.io.*;
class Numbers
Output
0 1 2 3 4 5 6 7 8 9 10
2. while Loop
A while loop is used when we want to check the condition before running the code.
Syntax:
while (condition) {
// code to be executed
Flow chart:
• While loop starts with the checking of Boolean condition. If it evaluated to true, then the loop
body statements are executed otherwise first statement following the loop is executed. For this reason
it is also called Entry control loop
• Once the condition is evaluated to true, the statements in the loop body are executed. Normally
the statements contain an update value for the variable being processed for the next iteration.
• When the condition becomes false, the loop terminates which marks the end of its life cycle.
Program
import java.io.*;
class Sample2
int i = 0;
i++;
}
}
Output
0 1 2 3 4 5 6 7 8 9 10
do-while Loop:
The do-while loop in Java ensures that the code block executes at least once before the condition is
checked.
Syntax:
do {
// code to be executed
} while (condition);
• do while loop starts with the execution of the statement(s). There is no checking of any
condition for the first time.
• After the execution of the statements, and update of the variable value, the condition is checked
for true or false value. If it is evaluated to true, next iteration of loop starts.
• When the condition becomes false, the loop terminates which marks the end of its life cycle.
• It is important to note that the do-while loop will execute its statements atleast once before any
condition is checked, and therefore is an example of exit control loop.
Flow chart
Program
import java.io.*;
class Samples
int i = 0;
do
i++;
Output
0 1 2 3 4 5 6 7 8 9 10
If we have a for loop inside the another loop, it is known as nested for loop. The inner loop executes
completely whenever outer loop executes.
The structure allows us to perform complex iterations by running a for loop for each iteration of the
outer for loop. Nested for loops are particularly useful when working with multi-dimensional data
structures like arrays or when we need to perform operations that require multiple levels of looping,
such as generating a matrix or table.
Syntax:
Flowchart:
Program:
for(int i=1;i<=3;i++)
for(int j=1;j<=3;j++)
System.out.println(i+" "+j);
}
Output:
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
The for-each loop in Java (also called the enhanced for loop) was introduced in Java 5 to simplify
iteration over arrays and collections. It is cleaner and more readable than the traditional for loop and is
commonly used when the exact index of an element is not required.
Parameters:
• var: The variable that holds the current element during each iteration.
Below is a basic example of using the for-each loop to iterate through an array and print each element.
A for-each loop directly accesses elements without needing index variables.
import java.io.*;
class ForEach
int arr[] = { 1, 2, 3, 4, 5 };
Output
12345
In this example, the for-each loop iterates over the array “arr" and prints each element. We use the
variable element "e" to access each value in the array, making the loop more concise compared to using
a traditional for loop with an index.
Jump Statements:
Jump statements are used to transfer the control of the program to the specific statements. In other
words, jump statements transfer the execution control to the other part of the program. There are two
types of jump statements in Java, i.e., break and continue.
break statement
the break statement is used to break the current flow of the program and transfer the control to the
next statement outside a loop or switch statement. However, it breaks only the inner loop in the case of
the nested loop.
The break statement cannot be used independently in the Java program, i.e., it can only be written
inside the loop or switch statement.
program
if (i == 6)
break;
System.out.println(i);
Output:
continue statement
The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues
with the next iteration in the loop.
if (i == 4)
continue;
System.out.println(i);
Output: