Loop Statements:
It
is a control flow statement.
Loop
statements are used to execute set of instruction repeatedly.
Types of loop
statement:
i.
While loop
ii.
Do while loop
iii.
For loop
iv.
Advanced for loop/ enhanced for loop / for each
loop
i. While loop:
While
loop used to execute set of instructions repeatedly.
Syntax:
While(condition)
{
Statement;
Update;
}
For
designing loop programmer should follow process
·
Initialization : this has to be done before starting
for loop
·
Condition:
used to control the loop. If the condition inside the loop is evaluated
true then the run continue. If condition evaluated false the loop stops or
terminate.
·
Update statement should be executed in every
cycle or iteration of the loop. The update statement should help the condition become false after the desire
iteration.
Example:
Class
while1
{
Public
static void main(String[] args)
{
Int
count = 1; // initialization
While(count
<= 5) // condition
{
System.out.println(“hellow
world”);
Count
++ ; // update
}
}
}
ii. Do while
It is a loop statement where condition check
after the execution of Do block.
Syntax:
do
{
Statement;
Update;
}
While(condition);
The
condition inside the while block should be Boolean type. In do while loop , do block
is first executed & then condition check at end. If the condition returns
true the control goes to do block statement get executed. This statement gets
executed still the while statement evaluated false. If the condition returns false
the loop get terminated. The minimum execution in do while loop is one time.
Example:
Class
Dowhile1
{
Public
static void main(String [] args)
{
Int
num =1 ;
do
{
System.out.println(num);
num++;
}
while(num<=10);
}
}
iii. For loop :
Syntax:
for
(initialization ; condition; update)
{
Statement
;
}
·
Variable is created & value is initialized
·
Afte the initialization If the condition is the true
control get into the loop block& instructions are executed
·
After all statement executed control goes to
update bar & variable get updated. these cycle continuous until condition
become false.
·
Ones the condition become false loop is
terminated.
Example:
Class
For1
{
Public
static void main(String [] args)
{
for(int
num =1 ; num<= 10 ; num++)
{
System.out.println(num);
}
}
}
Nested for loop:
Loop
inside another loop called nested loop. One cycle of outer loop will complete
execution of inner loop.
Example:
Class
Nestedloop
{
Public
static void main(String [] args)
{
for(int
i= 0; i<3; i++)
{
for(int
j =0 ; j< 3; j++)
{
System.out.println(“&”);
}
}
}
}
Result:
& & &
& & &
& & &
iv. Advanced for loop/ enhanced for loop / for each loop:
introduced in Java 5 to simplify iteration
over arrays and collections. It provides a more readable and concise way to
traverse elements without the need for an explicit index or iterator.
When to
Use the for-each Loop
- When
you need to traverse all elements in an array or collection.
- When
you don't require access to the index of elements.
- When
you don't need to modify the original collection during iteration.
- When
code readability and simplicity are priorities.
Limitations of the for-each
Loop
- Cannot
access the index of elements during iteration.
- Cannot
modify the collection (e.g., adding or removing elements) during
iteration. Doing so may lead to a ConcurrentModificationException.
- Cannot
iterate in reverse order.
- Not
suitable when you need to skip elements or iterate with a specific step
size.
Example:
public
class SimpleForEach {
public static void main(String[] args) {
int[] numbers = { 10, 20, 30, 40 };
for (int num : numbers) {
System.out.println(num);
}
}
}