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

Iterative Constructs IN JAVA

Uploaded by

spam16543
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)
61 views5 pages

Iterative Constructs IN JAVA

Uploaded by

spam16543
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

Itera ve Constructs in Java: Class 9 ICSE Notes

Introduc on

 Itera ve constructs, also called loops, are used to execute a block of code repeatedly as long
as a given condi on is true.

 Loops reduce code redundancy and make programs efficient.

Defini on

 Itera on: Repea ng a set of instruc ons un l a specific condi on is met.

Types of Looping Statements

1. Entry Controlled Loops:

o The condi on is checked before entering the loop body.

o Examples: for loop, while loop.

2. Exit Controlled Loops:

o The condi on is checked a er execu ng the loop body.

o Example: do-while loop.

Jump Statements

 break: Terminates the loop immediately.

 con nue: Skips the current itera on and moves to the next itera on.

Syntax of Loops

1. For Loop (Entry Controlled)

for (ini aliza on; condi on; update) {

// Body of the loop

}
Example:

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

System.out.println(i);

2. While Loop (Entry Controlled)

while (condi on) {

// Body of the loop

Example:

int i = 1;

while (i <= 5) {

System.out.println(i);

i++;

3. Do-While Loop (Exit Controlled)

do {

// Body of the loop

} while (condi on);

Example:

int i = 1;

do {

System.out.println(i);

i++;

} while (i <= 5);

Interconversion of Loops

1. For to While:

// For loop

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

System.out.println(i);

}
// Equivalent While loop

int i = 1;

while (i <= 5) {

System.out.println(i);

i++;

2. While to Do-While:

// While loop

int i = 1;

while (i <= 5) {

System.out.println(i);

i++;

// Equivalent Do-While loop

int i = 1;

do {

System.out.println(i);

i++;

} while (i <= 5);

Finite and Infinite Loops

 Finite Loop: Runs a fixed number of mes.

 Infinite Loop: Runs indefinitely unless terminated by a condi on or break.

Example of Infinite Loop:

while (true) {

System.out.println("Infinite Loop");

}
Delay Loop

 A loop used to introduce a delay in the program's execu on.

 Example:

for (int i = 0; i < 100000; i++) {

// Empty loop to cause delay

Counter Variables

 Variables used to count the number of itera ons in a loop.

 Example:

int counter = 0;

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

counter++;

System.out.println("Itera on: " + counter);

Programs Illustra ng All Loops

1. For Loop Example:

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

System.out.println("For Loop Itera on: " + i);

2. While Loop Example:

int i = 1;

while (i <= 5) {

System.out.println("While Loop Itera on: " + i);

i++;

3. Do-While Loop Example:

java

Copy code

int i = 1;
do {

System.out.println("Do-While Loop Itera on: " + i);

i++;

} while (i <= 5);

Key Points

1. Use break to exit a loop prematurely.

2. Use con nue to skip the rest of the loop body for the current itera on.

3. Entry controlled loops are preferred when the number of itera ons is known.

4. Exit controlled loops are preferred when the loop must execute at least once.

You might also like