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

Loops_in_java

The document explains the three types of loops in Java: for loop, while loop, and do-while loop, detailing their syntax and functionality. It provides examples for each loop type, demonstrating how to execute code repeatedly based on conditions or predetermined iterations. Additionally, it highlights the differences in control flow and execution order among the loop types.

Uploaded by

padhyeshaunak
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)
3 views

Loops_in_java

The document explains the three types of loops in Java: for loop, while loop, and do-while loop, detailing their syntax and functionality. It provides examples for each loop type, demonstrating how to execute code repeatedly based on conditions or predetermined iterations. Additionally, it highlights the differences in control flow and execution order among the loop types.

Uploaded by

padhyeshaunak
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/ 13

Loops in java

Repetition statements are essential to control flow structures in Java. They allow you to
execute a block of code multiple times, either based on a specific condition or a
predetermined number of iterations
There are 3 types of loops in java
1) for loop

2) while loop
3) do while loop

1) for loop
Java for loop is a control flow statement that allows code to be executed repeatedly
based on a given condition. The for loop in Java provides an efficient way to iterate over a
range of values, execute code multiple times, or traverse arrays and collections.

Java for loop is divided into various parts as mentioned below:


1. Initialization Expression
2. Test Expression
3. Update Expression

Syntax
for (initialization; condition; update)
{
//Code to execute

}
Example

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

System.out.println("Iteration: " + i);

}
Explanation:

 Initialization: int i = 1 sets the starting point.


 Condition: i <= 5 ensures the loop runs while the condition is true.
 Update: i++ increments i after each iteration.
Example 2
int num = 3;

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


{
System.out.println(num + " x " + i + " = " + (num * i));
}
2) for (int i = 1; i <= 20; i += 4)

{
System.out.println(i);
}

3)
int sum = 0;

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


{
sum += i;
System.out.println("i = " + i + ", sum = " + sum);
}

System.out.println("Final sum = " + sum);


4)
int x = 0;
for (int i = 5; i > 2; i--)
{
x = x + i;

}
System.out.println(x);
5)

int countEven = 0;
int countOdd = 0;
for (int i = 1; i <= 5; i++)
{
if (i % 2 == 0)

{
countEven++;
}
else
{

countOdd++;
}
}
System.out.println("Even count: " + countEven);
System.out.println("Odd count: " + countOdd);
2) WHILE LOOP

Java while loop is a control flow statement used to execute the block of
statements repeatedly until the given condition evaluates to false. Once the
condition becomes false, the line immediately after the loop in the program is
executed.
while (test_expression)
{
// statements
update_expression;
}

 Test Expression: This condition is evaluated before each iteration. If it


returns true, then the loop executes; otherwise, the loop exits.
 Update Expression: After executing the loop body, this expression
increments/decrements the loop variable by some value.
Example 1
int sum = 0;
int i = 1;
while (i <= 5)
{
sum += i;
i++;
}

System.out.println("Sum = " + sum);

2)

int i = 1;

while (i <= 9)

if (i % 2 != 0)

System.out.println(i);

i++;

}
Final Output

13579

4)

int number = 12345;

int count = 0;

while (number > 0)

number = number / 10;

count++;

System.out.println("Number of digits: " + count);


Number of digits: 5

Do while

Java do-while loop is an Exit control loop. Unlike for or while loop, a do-while
check for the condition after executing the statements of the loop body for once
do
{

Update_expression
}

while (test_expression);
1)

int i = 1;

do {

System.out.println(i);

i++;

while (i <= 5);

2)

int count = 1;

do {

System.out.println("Hello");

count++;

} while (count <= 3);


2)

int num = 11;

do {

if (num % 2 == 0) {

System.out.println("First even number greater than 10: " + num);

break;

num++;

} while (num > 10);

You might also like