GFG Day 5: Java Loops

Table of contents

- Loops in Java allow you to execute a block of code multiple times, either a fixed number of times or while a condition is true. In Java, there are four types of loops, each with its own syntax and method of checking the condition, but the use case is the same.
Loops in Java
In Java there are four types of loops listed below:
for loop
for each loop
while loop
do-while loop
for loop
for loop
is used when you know exactly how many times you want to execute the block of code.
Syntax
for (initialization; condition; increment/decrement) {
// code to be executed
}
Initialization: Typically, the variable will be initialized, and the statement is executed once before the loop starts.
Condition: This expression is evaluated before each iteration, and if the condition is true, the loop executes; if it is false, the loop stops.
Increment/Decrement: It is usually to increment or decrement the variable when the statement is executed after each iteration.
The below Java program demonstrates a for loop that prints numbers from 0 to 10 in a single line.
// file name: forloop.java
public class forloop{
public static void main (String[] args) {
for(int num = 0; num <=10; num++){
System.out.println("num: " + num);
}
}
}
Output
num: 0
num: 1
num: 2
num: 3
num: 4
num: 5
num: 6
num: 7
num: 8
num: 9
num: 10
For each loop
For each
loop is the updated version of thefor loop
, and it is mainly used for arrays and collections.
Syntax
for (dataType variable : arrayOrCollection) {
// code to be executed
}
The below Java program demonstrates an enhanced for loop (for each loop) to iterate through an array and print names.
// file name: foreach.java
public class foreach{
public static void main (String[] args) {
int[] numbers = {3, 9, 5, -5};
for (int number : numbers) {
System.out.println(number);
}
}
}
Output
3
9
5
-5
while loop
- While loop is used when the number of iterations isn't known in advance. It repeats the block as long as the condition is true.
Syntax
while (condition) {
// code to be executed
}
The below Java program demonstrates a while loop that prints numbers from 0 to 10 in a single line.
// file name: whileloop.java
public class whileloop{
public static void main (String[] args) {
int i = 0;
while (i <= 10) {
System.out.print(i + " ");
i++;
}
}
}
Output
0 1 2 3 4 5 6 7 8 9 10
do-while loop
- The do-while loop ensures that the code block executes at least once before checking the condition.
Syntax
do {
// code to be executed
} while (condition);
The below Java program demonstrates a do-while loop that prints numbers from 0 to 10 in a single line.
// file name: dowhileloop.java
public class dowhileloop{
public static void main (String[] args) {
int i = 0;
do {
System.out.print(i + " ");
i++;
} while (i <= 10);
}
}
Output
0 1 2 3 4 5 6 7 8 9 10
- The difference between a
while
loop and ado-while
loop is that awhile
loop checks the condition first and only executes the code if the condition istrue
. In contrast, ado-while
loop executes the code once before checking the condition, and if the condition istrue
, it will proceed to the next iteration.
Happy Learning
Thanks For Reading! :)
SriParthu ๐๐ฅ
Subscribe to my newsletter
Read articles from sri parthu directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

sri parthu
sri parthu
Hello! I'm Sri Parthu! ๐ I'm aiming to be a DevOps & Cloud enthusiast ๐ Currently, I'm studying for a BA at Dr. Ambedkar Open University ๐ I really love changing how IT works to make it better ๐ก Let's connect and learn together! ๐๐