You are on page 1of 8

Iterative constructs (Loops) in Java

Iterative construct (loop) means repeating execution of a set of instructions till given
condition evaluates true. The process of executing a set of instructions once is called an
iteration.
The condition, which basically controls the loop, is referred to as loop control and the
variable used in condition to control the loop is known as loop control variable.

Components (segments) of a loop: A loop has following components-

1. Initialization: It is used to assign initial value to the loop control variable before
starting the loop.
2. Test condition: It is Boolean expression, its value (true / false) determines whether
the body of the loop is to be executed or the loop is to be terminated.
3. Body of the loop: It is the set of instructions that is executed repeatedly, grouped
together between a pair of curly brackets.
4. Update expression: It is used to update or change the value of loop control variable
at the end of each iteration.

Java provides following three loop structures:


for loop, while loop and do – while loop
These loops can be classified as –
 Entry-controlled loop: In this type of loop the test-condition is evaluated before
the start of the loop body. There are two entry-controlled loops- for loop, while
loop.
 Exit-controlled loop: In this type of loop the test-condition is evaluated at the end
of the loop body. So the body of an exit-controlled loop is always executed at least
once. There is an exit-controlled loop- do – while loop.

for loop :- It is an entry-controlled loop used to perform a fixed number of iterations.


False
Syntax:

for (initialization ; test-condition ; update expression)


True
{
Body of the loop;
}

Example:
for(int i=1; i<=5 ; i++)
{ System.out.println(“Number is ”+i);
}
Computer Applications Notes Volume I (Class IX) for ICSE examinations 2022 by Hem Sir Page 30
Note: In case of single statement in the body of the loop, the curly brackets can be ignored.
Working / dry-run of above loop-

So output will be as following-


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

Variations in for loop: A for loop can have multiple variations that make it more flexible
and efficient. Such as-
Multiple initialization and update expressions
for ( int i=1, j=5 ; i<=5 ; i++, j--)
System.out.println(i + “, ” +j);
Output:
1, 5
2, 4
3, 3
4, 2
5, 1

Optional expressions (We can skip any or all expressions depending upon requirement)
int i=2;
for( ; i<=20 ; )
{
System.out.println( i );
i += 2;
}

Computer Applications Notes Volume I (Class IX) for ICSE examinations 2022 by Hem Sir Page 31
Infinite loop (Never ending loop)
int i=2;
for( ; ; )
{
System.out.println( i );
i += 2;
}

Empty loop (A loop without any statement in its body)


for ( int i=1; i<=9999999; i++)
{
}

Note: We can declare variable inside the loop initialization, but this variable has a
limited scope and can be available only within the loop.

while loop:- It is also an entry-controlled loop used where the number of iterations are
not fixed.
Syntax:

Initialization; False

while ( test-condition )
True
{
Body of the loop;

Update expression;
}

Example Output

int i=1; 1
while (i<=5) 2
{ 3
System.out.println(i); 4
i++; 5
}

Computer Applications Notes Volume I (Class IX) for ICSE examinations 2022 by Hem Sir Page 32
Variations in the while loop: The while loop also supports a number of variations. Such
as-
Example 1.
while(true) //Infinite loop
{
System.out.println(“Hello”);
}

Example 2.
int i=0;
while(++i <=5) // Test expression with updation
{
System.out.println(i);
}

Example 3.
int i=0;
while(++i <=5) ; // Empty loop

The do - while loop:- It is an exit-controlled loop used where the number of iterations
are not fixed.

Syntax:

Initialization;

do
{
Body of the loop;

Update expression;
}
while(test – condition);
True

False

Computer Applications Notes Volume I (Class IX) for ICSE examinations 2022 by Hem Sir Page 33
Example Output

int i=1; 1
do 2
{ 3
System.out.println(i); 4
i++; 5
}
while (i<=5);

Variations in the do-while loop: The do-while loop also supports a number of variations.
Such as-
Example 1.
do
{
System.out.println(“Hello”);
}
while(true); //Infinite loop

Example 2.
int i=1;
do
{
System.out.println(i);
}
while(++i <=5) ; // Test expression with updation

Example 3.
int i=1;
do
{
}
while(++i <=5) ; // Empty loop

Jump Statements:
Java supports three jump statements:
 break
 continue
 return
Computer Applications Notes Volume I (Class IX) for ICSE examinations 2022 by Hem Sir Page 34
The break statement terminates the current loop or switch statement and takes the
program control out of the loop or switch structure.
For example : To print squares of first 10 natural numbers but stop when square is
divisible by 5.
Output
int i, sq=0; 1
for(i=1; i<=10 ; i++) 4
{ 9
sq = i * i ; 16
if(sq%5 == 0)
break;
System.out.println(sq);
}

The continue statement skips the rest of the statements for the current iteration of the
loop, and transfer the control for the next iteration of the loop.
For example : To print squares of first 10 natural numbers but skip when square is
divisible by 5.
Output
int i, sq=0; 1
for(i=1; i<=10 ; i++) 4
{ 9
sq = i * i ; 16
if(sq%5 == 0) 36
continue; 49
System.out.println(sq); 64
81
}

The return statement terminates the current method and transfers control to the code that
invoked (call) it. The return statement is covered in method chapter.

Note: Using above concepts and your book, try to make many programs for your practice
to improve programming logic. Some examples are-

Example 1. Program to display all factors of a given number.


import java.util.Scanner;
class loop1
{

Computer Applications Notes Volume I (Class IX) for ICSE examinations 2022 by Hem Sir Page 35
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("ENTER YOUR NUMBER");
int x =sc.nextInt();
int i;
for(i=1;i<=x;i++)
{
if(x%i==0)
{
System.out.println(i);
}
}
}
}

Example 2. Program to display a given number is Prime or not.


import java.util.Scanner;
class loop2
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("ENTER YOUR NUMBER");
int x =sc.nextInt();
int i, k=0;
for(i=2;i<=x/2;i++)
{
if(x%i==0)
{
k++;
break;
}
}
if (k==0)
System.out.println(x + " is a Prime no.”);
else
System.out.println(x +" is not a Prime no.”);
}
}

Computer Applications Notes Volume I (Class IX) for ICSE examinations 2022 by Hem Sir Page 36
Example 3. Program to display a given number is palindrome or not.
import java.util.Scanner;
class loop3
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("ENTER YOUR NUMBER");
int x =sc.nextInt();
int n = x;
int i, r=0;
while(x!=0)
{
r = r * 10 + (x%10);
x = x / 10;
}
if (r==n)
System.out.println(n+ " is a Palindrome no.”);
else
System.out.println(n +" is not a Palindrome no.”);
}
}

Computer Applications Notes Volume I (Class IX) for ICSE examinations 2022 by Hem Sir Page 37

You might also like