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

CL-9 CH-9 (Iterative Constructs in Java)

Uploaded by

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

CL-9 CH-9 (Iterative Constructs in Java)

Uploaded by

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

CLASS- 9

Chapter- 9
Iterative Constructs in Java
Java is a widely-used programming language known for its versatility and
ability to handle complex tasks. One of the fundamental concepts in Java
programming is the use of iterative constructs that allows us to repeat a set
of instructions multiple times. In this section, we will explore the three
main iterative constructs in Java: the "for" loop, the "while" loop, and the
"do-while" loop. We will provide detailed explanations, along with full
Java programs, comments, and output examples, to help you understand
how to use these constructs effectively in your coding endeavours.
1. Java for Loop
The "for" loop in Java is used when you know in advance how many times
you want to repeat a set of instructions. It consists of three parts:
initialization, condition, and increment/decrement. Let's break down each
part with a simple example:
ForLoopExample.java
1. public class ForLoopExample
2. {
3. public static void main(String[] args)
{
4. // Initialization: Start from 1
5. // Condition: Continue while i is less than or equal to 5
6. // Increment: Increase i by 1 in each iteration
7. for (int i = 1; i <= 5; i++)
{
8. System.out.println("Iteration " + i);
}
}
}
Output:

Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5

2. Java while Loop


The "while" loop is used when you don't know in advance how many
times you want to repeat a set of instructions but have a condition that
should be met for the loop to continue. Here's an example:
WhileLoopExample.java
1. public class WhileLoopExample
2. {
3. public static void main(String[] args) \
{
4. int count = 1;
5. // Continue looping as long as count is less than or equal
to 5
6. while (count <= 5)
{
7. System.out.println("Iteration " + count);
8. count++; // Increment count by 1
9. }
}
}
Output:

Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5

3. Java do-while Loop


The "do-while" loop is similar to the "while" loop, but it guarantees that
the loop body will execute at least once, even if the condition is initially
false. Here's an example:
DoWhileLoopExample.java
1. public class DoWhileLoopExample
2. {
3. public static void main(String[] args)
{
4. int number = 5;
5. // Execute the loop body at least once
6. do
{
7. System.out.println("Number is " + number);
8. number--;
9. }
while (number > 0);
}
}
Output:

Number is 5
Number is 4
Number is 3
Number is 2
Number is 1

Comparing the Three Iterative Constructs


Now that we have seen examples of each type of loop, let's compare them
based on their use cases:
o for loop: Use when we know the number of iterations in advance
or want to iterate over a range of values.
o while loop: Use when we want to repeat a block of code based on
a condition, and you don't necessarily know the number of
iterations beforehand.
o do-while loop: Use when we want to ensure that a block of code
executes at least once, regardless of the initial condition.
Choosing the right type of loop depends on your specific programming
needs and the logic we want to implement.
Common Loop Control Statements
In addition to the basic loop constructs, Java provides control statements
that allow you to control the flow of loops more precisely:
Java break statement: It is used to exit a loop prematurely based on a
certain condition. Here's an example:
BreakStatementExample.java
1. public class BreakStatementExample
2. {
3. public static void main(String[] args)
{
4. for (int i = 1; i <= 10; i++)
{
5. if (i == 5)
{
6. break; // Exit the loop when i is 5
7. }
8. System.out.println("Iteration " + i);
9. }
}
}

Output:

Iteration 1
Iteration 2
Iteration 3
Iteration 4

Java continue statement: It is used to skip the rest of the current iteration
and proceed to the next iteration of the loop. Here's an example:
ContinueStatementExample.java
1. public class ContinueStatementExample
2. {
3. public static void main(String[] args)
{
4. for (int i = 1; i <= 5; i++)
{
5. if (i == 3)
{
6. continue; // Skip iteration when i is 3
7. }
8. System.out.println("Iteration " + i);
9. }
}
}
Output:

Iteration 1
Iteration 2
Iteration 4
Iteration 5

In Summary, Iterative constructs, including the "for," "while," and


"do-while" loops, are essential tools in Java programming for
repeating code blocks. By understanding when and how to use
these constructs, along with control statements like "break" and
"continue," we can write efficient and flexible code to solve a wide
range of problems. Remember that choosing the right loop type and
control statements depends on the specific requirements of
program. Practice using these constructs in different scenarios to
become a proficient Java programmer.

You might also like