0% found this document useful (0 votes)
2 views

Loops in Java

The document provides an overview of loops in Java, detailing three main types: for loop, while loop, and do-while loop. Each loop type is explained with syntax and examples, highlighting their unique characteristics, such as when the condition is checked and whether they run at least once. Additionally, a comparison table summarizes the features and use cases of each loop type.

Uploaded by

contactkeenan72
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Loops in Java

The document provides an overview of loops in Java, detailing three main types: for loop, while loop, and do-while loop. Each loop type is explained with syntax and examples, highlighting their unique characteristics, such as when the condition is checked and whether they run at least once. Additionally, a comparison table summarizes the features and use cases of each loop type.

Uploaded by

contactkeenan72
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Loops in Java

Annexa Academics – Grade 9 ICSE Programming Notes

➤ Introduction
Loops in Java allow a set of instructions to be executed repeatedly until a
certain condition is met. Java supports three main types of loops:
• for loop
• while loop
• do-while loop

1. for Loop

Syntax:
java
CopyEdit
for (initialization; condition; update) {
// code to repeat
}

Example: Print numbers 1 to 5


java
CopyEdit
class ForLoopExample {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
}
}

2. while Loop

Syntax:
java
CopyEdit
while (condition) {
// code to repeat
}

Example: Print numbers 1 to 5


java
CopyEdit
class WhileLoopExample {
public static void main(String[] args) {
int i = 1;
while (i <= 5) {
System.out.println(i);
i++;
}
}
}

3. do-while Loop
The do-while loop runs at least once, even if the condition is false, because the
condition is checked after the first execution.

Syntax:
java
CopyEdit
do {
// code to repeat
} while (condition);

Example: Print numbers 1 to 5


java
CopyEdit
class DoWhileExample {
public static void main(String[] args) {
int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 5);
}
}

Example When Condition is False


java
CopyEdit
class DoWhileFalse {
public static void main(String[] args) {
int i = 10;
do {
System.out.println("This will run once!");
} while (i < 5);
}
}

Even though the condition is false, the message still prints once.

Summary: Loop Comparison Table

Feature for loop while loop do-while loop

Condition
Before loop body Before loop body After loop body
checked?

Runs at least
No No Yes
once?

Known Condition-based Must run at least


Use Case
repetitions loop once

Prepared By:
Annexa Academics – Grade 9 ICSE Java Programming
No Copyright Claimed
This material has been created for educational purposes and is intended for
free public use.
The content in this document is released into the public domain by the author
and contributors.
You are free to copy, share, modify, distribute, or use this material in any form,
with or without attribution.
This resource was created with the support of AI tools and compiled by Annexa
Academics for the benefit of students and educators.

****************************************************************

You might also like