U -1 Looping Statements
U -1 Looping Statements
U -1 Looping Statements
In programming, sometimes we need to execute the block of code repeatedly while some condition
evaluates to true.
However, loop statements are used to execute the set of instructions in a repeated order.
The execution of the set of instructions depends upon a particular condition.
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. While all the ways provide similar basic functionality,
they differ in their syntax and condition checking time.
In Java, we have three types of loops that execute similarly. However, there are differences in their syntax and
condition checking time.
1. for loop
2. while loop
3. do-while loop
However, if we don't know the number of iterations in advance, it is recommended to use a while loop.
Unlike for loop, the initialization and increment/decrement doesn't take place inside the loop statement in
while loop.
It is also known as the entry-controlled loop since the condition is checked at the start of the loop. If the
condition is true, then the loop body will be executed; otherwise, the statements after the loop will be
executed.
Syntax :
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.
import java.io.*;
class GFG {
public static void main (String[] args) {
int i=0;
while (i<=10)
{
System.out.println(i);
i++;
}
}
}
Output
0
1
2
3
4
5
6
7
8
9
10
Java do-while loop
do while loop is similar to while loop with only difference that it checks for condition after executing the
statements, and therefore is an example of Exit Control Loop.
The do-while loop checks the condition at the end of the loop after executing the loop statements.
When the number of iteration is not known and we have to execute the loop at least once, we can use do-
while loop.
It is also known as the exit-controlled loop since the condition is not checked in advance. The syntax of the
do-while loop is given below.
Syntax:
do
{
statements..
}
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.
import java.io.*;
class GFG {
public static void main (String[] args) {
int i=0;
do
{
System.out.println(i);
i++;
}while(i<=10);
}
}
Output
0
1
2
3
4
5
6
7
8
9
10
Java for loop
In Java, for loop is similar to C and C++.
for loop provides a concise way of writing the loop structure.
It enables us to initialize the loop variable, check the condition, and increment/decrement in a single line of
code, thereby providing a shorter, easy to debug structure of looping.
We use the for loop only when we exactly know the number of times, we want to execute the block of
code,
Syntax:
for (initialization condition; testing condition; increment/decrement)
{
statement(s)
}
It consists of four parts:
1. Initialization: It is the initial condition which is executed once when the loop starts. Here, we can initialize
the variable, or we can use an already initialized variable. It is an optional condition.
2. Condition: It is the second condition which is executed each time to test the condition of the loop. It
continues execution until the condition is false. It must return boolean value either true or false. It is an
optional condition.
3. Increment/Decrement: It increments or decrements the variable value. It is an optional condition.
4. Statement: The statement of the loop is executed each time until the second condition is false.
import java.io.*;
class GFG {
public static void main (String[] args) {
for (int i=0;i<=10;i++)
{
System.out.println(i);
}
}
}
Output
0
1
2
3
4
5
6
7
8
9
10
12
23
44
56
78