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

Loops Java Notes

Java Coding Notes

Uploaded by

studycentral29
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Loops Java Notes

Java Coding Notes

Uploaded by

studycentral29
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Differences Between the Loops:

1. for loop: Ideal when the number of iterations is known.


 Example: Iterating over an array or a range of numbers.

2. while loop: Used when the number of iterations is


unknown, but you want to loop until a condition becomes
false.
 Example: Reading user input until a valid response is
provided.

3. do-while loop: Similar to the while loop, but ensures the


loop body executes at least once, even if the condition is
false initially.
 Example: Requiring user input at least once, regardless
of the validity of the first input.

Each loop type has its own strengths and should be chosen based
on the problem you're solving. The for loop is usually best when
you know how many times you need to iterate, while the while
and do-while loops are ideal when the loop count depends on
some runtime condition.
2. while Loop
The while loop is used when you don't know the number of
iterations in advance but want to loop based on a condition.
Structure:

while (condition) {

// Code to be executed

 The loop checks the condition first.


 If the condition is true, the loop body is
executed.
 If the condition is false, the loop exits.
Example: Print numbers from 1 to 5 using a while loop
public class WhileLoopExample {

public static void main(String[] args) {

int i = 1;

while (i <= 5) {

System.out.println(i);

i++; // Increment the loop control variable

Explanation:
 The loop starts with i = 1.
 It checks if i <= 5. If true, it prints the value of i and increments
i.
 The loop continues until i becomes greater than 5.
3. do-while Loop
The do-while loop is like the while loop but guarantees that the loop body
will execute at least once, regardless of the condition. The condition is
checked after executing the loop body.
Structure:

do {

// Code to be
executed

} while (condition);

Example: Print numbers from 1 to 5 using a


do-while loop.
public class DoWhileLoopExample {

public static void main(String[] args) {

int i = 1;

do {

System.out.println(i);

i++; // Increment the loop control


variable

} while (i <= 5);

Explanation:

 The loop starts with i = 1 and prints the value of i before checking
the condition.
 After printing, it increments i and checks if i <= 5.
 The loop continues until i becomes greater than 5.
A Short Guide to Java Loops
In Java, loops are used to execute a block of code repeatedly until a
certain condition is met. There are three primary types of loops:

1. for loop
2. while loop
3. do-while loop

1. for Loop
The for loop is typically used when the number of iterations is known
beforehand. It has the following structure:

for (initialization; condition; update) {

// Code to be executed

 Initialization: Initializes the loop control variable (e.g., int i = 0).


 Condition: Checks if the condition is true. If true, the loop body is
executed; if false, the loop terminates.
 Update: Updates the loop control variable at the end of each
iteration.

Example: Print numbers from 1 to 5


public class ForLoopExample {

public static void main(String[] args) {

for (int i = 1; i <= 5; i++) {

System.out.println(i);

Explanation:

 The loop starts with i = 1.


 It runs while i <= 5 (condition).
 After each iteration, i is incremented (i++).
 The loop prints numbers from 1 to 5.

You might also like