0% found this document useful (0 votes)
21 views5 pages

Lab06 LoopsForWhile

The document explains looping statements in Java, including for, while, and do-while loops, each with its specific use cases and syntax. It provides examples of how to implement these loops in programs, demonstrating their functionality through sample code. Additionally, it compares the three types of loops based on their execution conditions and guarantees.

Uploaded by

jainul.spec
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)
21 views5 pages

Lab06 LoopsForWhile

The document explains looping statements in Java, including for, while, and do-while loops, each with its specific use cases and syntax. It provides examples of how to implement these loops in programs, demonstrating their functionality through sample code. Additionally, it compares the three types of loops based on their execution conditions and guarantees.

Uploaded by

jainul.spec
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/ 5

Looping Statements (for, while, do..

while)

Looping Statements in Java

Looping statements in Java allow executing a block of code multiple times until a certain
condition is met.

There are three types of loops:

1. for loop – Used when the number of iterations is known.


2. while loop – Used when the number of iterations is not known but depends on a
condition.
3. do-while loop – Similar to the while loop but ensures the loop body executes at least
once.

for Loop in Java

The for loop is used when we know how many times we need to execute the loop.

The syntax is:

for(initialization; condition; update) {


// Code to be executed
}
 Initialization: Initializes the loop variable.
 Condition: Checked before every iteration; the loop stops if false.
 Update: Modifies the loop variable in each iteration.

Program using for Loop


class ForLoopExample {
public static void main(String[] args) {
System.out.println("Printing numbers from 1 to 5 using for
loop:");
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
}
}

Output:

Printing numbers from 1 to 5 using for loop:


1
2
3
4
5
Looping Statements (for, while, do..while)

Similar Program Definitions:

1. Write a program using a for loop to print the first 10 even


numbers.
2. Develop a program that uses a for loop to calculate the
factorial of a given number.
3. Create a program that utilizes a for loop to iterate over an
array of strings and print each string.

while Loop in Java

The while loop is used when the number of iterations is not known but depends on a
condition.

The syntax is:

while(condition) {
// Code to be executed
}

 The loop executes only if the condition is true.


 If the condition is false initially, the loop does not execute.

Program using while Loop


class WhileLoopExample {
public static void main(String[] args) {
int num = 1;
System.out.println("Printing numbers from 1 to 5 using while
loop:");
while (num <= 5) {
System.out.println(num);
num++;
}
}
}

Output:
Printing numbers from 1 to 5 using while loop:
1
2
3
4
5

public class WhileLoopExample {


Looping Statements (for, while, do..while)

public static void main(String[] args) {


int i = 1;
int sum = 0;
while (i <= 5) {
sum += i;
i++;
}
System.out.println("Sum: " + sum);
}
}

Explanation: The while loop starts with i equal to 1 and sum equal to 0.
It adds i to sum, increments i by 1, and repeats this process as long as i is
less than or equal to 5.

Similar Program Definitions:

1. Implement a program using a while loop to find the first


number greater than 100 that is divisible by 7.
2. Write a program that uses a while loop to reverse the digits
of a given number.
3. Create a program that utilizes a while loop to read integers
from user input until a negative number is entered, then
calculates the average of the entered numbers.

do-while Loop in Java

The do-while loop is similar to the while loop, but the condition is checked after executing
the loop body at least once. The syntax is:

do {
// Code to be executed
} while(condition);

Ensures the loop runs at least once, even if the condition is false initially.
Program using do-while Loop
class DoWhileExample {
public static void main(String[] args) {
int num = 1;
System.out.println("Printing numbers from 1 to 5 using do-while
loop:");
do {
System.out.println(num);
num++;
Looping Statements (for, while, do..while)

} while (num <= 5);


}
}

Output:
Printing numbers from 1 to 5 using do-while loop:
1
2
3
4
5

import java.util.Scanner;

public class DoWhileLoopExample {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int choice;
do {
System.out.println("Menu:");
System.out.println("1. Option 1");
System.out.println("2. Option 2");
System.out.println("3. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("You selected Option 1.");
break;
case 2:
System.out.println("You selected Option 2.");
break;
case 3:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice. Please try
again.");
}
} while (choice != 3);
scanner.close();
}
}
Looping Statements (for, while, do..while)

Output:
Menu: Menu: Menu:
1. Option 1 1. Option 1 1. Option 1
2. Option 2 2. Option 2 2. Option 2
3. Exit 3. Exit 3. Exit
Enter your Enter your Enter your
choice: 1 choice: 2 choice: 3
You selected You selected Exiting...
Option 1. Option 2.

Explanation: The do-while loop displays a menu and processes the


user's choice. The loop ensures that the menu is shown at least once
and continues to display it until the user selects the option to
exit (choice 3).

Similar Program Definitions:

1. Develop a program using a do-while loop to repeatedly prompt


the user to guess a randomly generated number between 1 and 50
until they guess correctly.
2. Write a program that uses a do-while loop to read characters
from user input until the user enters the character 'q'.
3. Create a program that utilizes a do-while loop to display a
countdown from 10 to 1 and then prints "Liftoff!".

Comparison of Loops
Feature for loop while loop do-while loop
Use case When the number of When the condition Ensures at least one
iterations is known determines execution execution
Condition Before each iteration Before each iteration After each iteration
Check
Execution May execute 0 times if May execute 0 times if Always executes at
Guarantee condition is false condition is false least once
initially initially
Best Used For Counting loops Running until a When at least one
condition is met execution is needed

You might also like