3. Looping Statement
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.
While loop
Do-while loop
For
4. While Loop
Loops through a block of code as long as a specified condition is true:
Syntax:
while (condition)
{
//code to be executed
Increment / decrement statement
}
Note:
Do not forget to increase the variable used in the condition,
otherwise the loop will never end!
5. While Loop
while loop is used to iterate a part of the program several times.
while is an example of entry controlled loop.
If the number of iteration is not fixed, it is recommended to use while loop.
// Program to display numbers from 1 to 5
class Main
{
public static void main(String[] args)
{
int i = 1, n = 5;
while(i <= n)
{
System.out.println(i);
i++;
}
}
}
Outpu
t
1
2
3
4
5
7. Do-while Loop
The do/while loop is a variant of the while loop.
This loop will execute the code block once, before checking if the condition is
true, then it will repeat the loop as long as the condition is true.
Syntax:
do{
//code to be executed / loop body
//update statement
}while(condition);
8. Do-while Loop
do-while loop is executed at least once because condition is checked after loop
body.
the do-while loop is an example of an exit-controlled loop.
// Java Program to display numbers from 1 to 5
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
int i = 1, n = 5;
do
{
System.out.println(i);
i++;
} while(i <= n);
}
}
Outpu
t
1
2
3
4
5
10. For Loop
for loop is used for executing a part of the
program repeatedly.
When the number of iteration is fixed then
it is suggested to use for loop
Syntax:
for(initialization; condition; increment/
decrement)
{
//statement or code to be executed
}
11. For Loop
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.
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.
3. Increment/Decrement(Updation):
It increments or decrements the variable value.
4. Statement:
The statement of the loop is executed each time until the second condition is false.
12. For Loop
// Program to print numbers from 1 to 5
class Main
{
public static void main(String[]
args)
{
int n = 5;
for (int i = 1; i <= n; ++i)
{
System.out.println(i);
}
}
}
Outpu
t
1
2
3
4
5
14. For Each Loop in Java
For-each is another array traversing technique like for loop, while loop, do-while
loop introduced in Java5.
It starts with the keyword for like a normal for-loop.
Instead of declaring and initializing a loop counter variable, you declare a
variable that is the same type as the base type of the array, followed by a colon,
which is then followed by the array name.
In the loop body, This variable is used to access the array/collection elements
without any indexing.
It’s commonly used to iterate over an array or a Collections class (eg, ArrayList)
15. For Each Loop in Java
Syntax:
for (type var : array)
{
statements using var;
}
17. Nested Loops in Java
Every loop consists of below 3 things inside
them:
Initialization: This step refers to initializing the
value of the counter.
Condition: This condition refers to the
comparison that needs to be true for continuing
the execution of the loop.
Increment/Decrement of counter: This refers to
the operation to be performed on the counter
once one flow of loop ends.
In the case of nested loops, the above three
steps are checked for each loop inside it.
Thus with each flow of the outer loop, the inner
loop executes completely, which means if we
have m flows for the outer loop and n number
of flows for the outer loop, then this loop
18. Half Pyramid Pattern using Nested Loops
// Java Program to print a Half pyramid pattern
class Demo
{
public static void main(String[] args)
{
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
if(i>=j)
{
System.out.print('*’);
}
}
System.out.println("n");
}
}
}
20. Programs to perform (Looping Statements)
Write a program to print first n odd numbers.
Write a program to check that the given number is prime or not.
Write a program to draw given patterns.
21. Java Break Statement
Break statement is used to break loop or switch statement.
It breaks the current flow of the program at specified condition.
We can use Java break statement in all types of loops such as
for loop
while loop
do-while loop
23. Break Statement Example
class Test
{
public static void main(String[] args)
{
for (int i = 1; i <= 10; ++i)
{
if (i == 5)
{
break;
}
System.out.println(i);
}
}
}
Outpu
t
1
2
3
4
When a break statement is encountered inside a
loop, the loop is immediately terminated and the
program control resumes at the next statement
following the loop.
26. Java Continue Statement
Continue statement
breaks one iteration (in
the loop), if a specified
condition occurs, and
continues with the next
iteration in the loop.
27. Continue Statement Example
class Main
{
public static void main(String[] args)
{
for (int i = 1; i <= 10; ++i)
{
// if value of i is between 4 and 9 continue is executed
if (i > 4 && i < 9)
{
continue;
}
System.out.println(i);
}
}
}
Outp
ut
1
2
3
4
9
10
The continue statement is used in loop control
structure when you need to immediately jump to the
next iteration of the loop.