Java Programming
Connect4Techs
Looping Statement
A "looping statement" is a programming instruction that lets you
execute a block of code repeatedly as long as a certain condition is met.
It is a tool that helps you automate repetitive tasks and can be used to
iterate over arrays, collections, and other data structures. Loops are an
important programming construct that makes code more efficient and
concise, and can help solve complex problems.
For example, if you want to show a message 100 times, then rather than
typing the same code 100 times, you can use a loop.
Types of Loops
In Java, there are three types of loops:
1. For loop
2. For-each loop
3. While loop
4. Do..while loop
➔ For Loop :
Java for loop is used to run a block of code for a certain
number of times. The syntax of for loop is:
for (initialization; condition; increment/Decrement) {
// body of the loop
}
Java Programming
Here,
1. The initialization initializes and/or declares variables and executes
only once.
2. The condition is evaluated. If the condition is true , the body of
the for loop is executed.
3. The increment/Decrement updates the value of
initialExpression.
4. The condition is evaluated again. The process continues until the
condition is false .
Flowchart of Java for loop
Java Programming
Example : Display a Text Five Times
// Program to print a text 5 times
class Main {
public static void main(String[] args) {
int n = 5;
// for loop
for (int i = 1; i <= n; ++i) {
System.out.println("Topperworld");
}
}
}
Output
Topperworld
Topperworld
Topperworld
Topperworld
Topperworld
➔ For-each Loop :
In Java, the for-each loop is used to iterate
through elements of arrays and collections (like ArrayList). It is also
known as the enhanced for loop.
The syntax of the Java for-each loop is:
for(dataType item : array) {
...
}
Java Programming
Here,
• array - an array or a collection
• item - each item of array/collection is assigned to this variable
• dataType - the data type of the array/collection
Example : Print Array Elements
// print array elements
class Main {
public static void main(String[] args) {
// create an array
int[] numbers = {31, 9, 5, -15};
// for each loop
for (int number: numbers) {
System.out.println(number);
}
}
}
Output
31
9
5
-15
Here, we have used the for-each loop to print each element of
the numbers array one by one.
• In the first iteration, element will be 31.
• In the second iteration, element will be 9.
• In the third iteration, element will be 5.
• In the fourth iteration, element will be -15.
Java Programming
➔ While Loop :
Java while loop is used to run a specific code
until a certain condition is met. The syntax of the while loop is:
while (condition) {
// body of loop
}
Here,
1. A while loop evaluates the condition inside the parenthesis () .
2. If the condition evaluates to true , the code inside the while loop is
executed.
3. The condition is evaluated again.
4. This process continues until the condition is false .
5. When the condition evaluates to false , the loop stops.
Flowchart of while loop
Java Programming
Example : Display Numbers from 1 to 5
// Program to display numbers from 1 to 5
class Main {
public static void main(String[] args) {
// declare variables
int i = 1, n = 5;
// while loop from 1 to 5
while(i <= n) {
System.out.println(i);
i++;
}
}
}
Output
1
2
3
4
5
➔ Do…While loop :
The do…while loop is similar to while loop.
However, the body of do…while loop is executed once before the test expression
is checked.
The syntax of the do…while loop is:
do {
// body of loop
} while(condition);
Java Programming
Here,
1. The body of the loop is executed at first. Then the condition is
evaluated.
2. If the condition evaluates to true , the body of the loop inside the
do statement is executed again.
3. The condition is evaluated once again.
4. If the condition evaluates to true , the body of the loop inside the
do statement is executed again.
5. This process continues until the condition evaluates to false . Then
the loop stops.
Flowchart of Java do while loop
Java Programming
Example : Display Numbers from 1 to 5
// Java Program to display numbers from 1 to 5
import java.util.Scanner;
// Program to find the sum of natural numbers from 1 to 100.
class Main {
public static void main(String[] args) {
int i = 1, n = 5;
// do...while loop from 1 to 5
do {
System.out.println(i);
i++;
} while(i <= n);
}
}
Output
1
2
3
4
5