for
: Repeats a block of code a specific number of times.
while
: Executes a block of code as long as a specified condition is true.
do-while
: Executes a block of code once, then repeats it as long as the condition is true.
for-each
(Enhanced For Loop): Iterates over elements in an array or collection.
for
loop is used to repeat a block of code a specific number of times.
for
loop is useful when the number of iterations is known beforehand (i.e. we know exactly how many times we need to repeat a task), like when working with arrays or running a piece of code a specific number of times.
for (initialization; condition; increment/decrement)
{
// statements (code to execute)
}
public class ForLoopExample
{
public static void main(String[] args)
{
// Print numbers from 1 to 5 using a for loop
for (int i = 1; i <= 5; i++)
{
System.out.println("Number: " + i);
}
}
}
Number: 1 Number: 2 Number: 3 Number: 4 Number: 5
for
loop, then curly braces {}
are optional.
for(initialization; condition; increment/decrement)
statement;
public class ForLoopExample
{
public static void main(String[] args)
{
for (int i = 1; i <= 5; i++)
System.out.println("Number: " + i);
}
}
while
loop is used to repeat a block of code as long as a specific condition is true
.
while
loop is useful when we don’t know how many times we need to repeat the task, and the loop will continue as long as the condition holds true
.
while (condition)
{
// statements (code to execute)
}
true
, the loop continues to execute the while
block statements.
true
.
false
, the loop will not run even once.
true
, then the loop will run infinite times.
for
loop and instead use a while
loop.
public class WhileLoopExample
{
public static void main(String[] args)
{
int no = 2; // Start from the smallest even number
while (no <= 17)
{
System.out.println("Even Number: " + no);
no = no + 2; // Skip directly to the next even number
}
}
}
Even Number: 2 Even Number: 4 Even Number: 6 Even Number: 8 Even Number: 10 Even Number: 12 Even Number: 14 Even Number: 16
while
loop, then curly braces {}
are optional.
while(condition)
statement;
do-while
loop is used to repeat a block of code at least once and then repeatedly as long as the condition is true
.
do-while
loop is useful when we want the code to run at least once, even if the condition is false
initially.
do
{
// statements (code to execute)
} while (condition);
true
, the loop continues to execute the statements. If it's false
, the loop terminates.
do-while
loop guarantees that the code inside the loop will run at least once.
true
, it continues to run. If false
, it exits.
do-while
loop.
import java.util.Scanner;
public class DoWhileExample
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
int number;
// Prompting user for a positive number
do
{
System.out.print("Enter a positive number: ");
number = scanner.nextInt();
} while (number <= 0);
System.out.println("You entered a valid positive number: " + number);
}
}
Here we have used Scanner
class for User Input, Click Here to read Scanner
Class deeply with Program.
Enter a positive number: -20 Enter a positive number: 0 Enter a positive number: 5 You entered a valid positive number: 5
for-each
loop (also called Enhanced For Loop) in Java is used to iterate over elements in an array or collection without needing an index variable.
for (dataType variable : collection)
{
// statements (code to execute)
}
int
, String
).
List
, Set
etc.) we want to iterate over.
for-each
loop (enhanced for loop) in Java is primarily used with arrays and collections, but it can also be used with any other Iterable objects also.
public class EnhancedForLoopExample
{
public static void main(String[] args)
{
String[] fruits = {"Apple", "Banana", "Cherry"};
// Using Enhanced For Loop (For-each loop)
for (String fruit : fruits)
{
System.out.println(fruit);
}
}
}
Apple Banana Cherry
ArrayIndexOutOfBoundsException
by iterating directly over elements.
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
We’re here to make our tutorials better based on your thoughts and suggestions.