Categories: Java

Iteration Statements(Loops)

  • The iteration statements allow a set of instructions to be performed repeatedly until a certain condition is fulfilled.
  • The iteration statements are also called loops or looping statements.
  • Java provides three kinds of loops: for loop, while loop, and do-while loop.
  • All three loop constructs of Java repeat a set of statements as long as a specified condition remains true.
  • This specified condition is generally referred to as a loop control.
  • For all three loop statements, a true condition is the one that returns a boolean true value and the false condition is the one that returns a boolean false value.
Parts of a Loop:
  1. Initialization expression: it gives the loop variable its first value. It is executed only once.
  2. Test expression: it is an expression whose truth value decides whether the loop body will be executed or not.
  3. Update Expression: The update expression changes the value of the loop variable. The update expression is executed; at the end of the loop, after the loop-body is executed.
  4. The Body-of-the-Loop: The statements that are executed repeatedly form the body of the loop.

Note: In an entry-controlled loop, first the test expression is evaluated and if it is non-zero, the body of the loop is executed; if the test expression evaluates to be zero, the loop is terminated. In an exit-controlled loop, the body of the loop is executed first, and then the test expression is evaluated. If it evaluates to be zero, the loop is terminated, otherwise repeated.

for loop

The for loop is the easiest to understand of the java loops.

All its loop-control elements are gathered in one place, while in the other loop construction of java, they are scattered about the program.

Syntax:

The general form (syntax) of the for loop statement is:

for(initialization-expression ; test-expression; update-expression )
{
  //body of the loop
}
Data-flow diagram of for loop
Example: Sum of 20 natural numbers

class HelloWorld {
    public static void main(String[] args) {
        int sum=0;
        System.out.println("20 Natural Numbers and their sum :\n"); 
        for(int i=1;i<=20;i++)
        {
            System.out.println(i+"\n");
            sum+=i;
        }
        System.out.println("\nSUM: "+sum);
        sum=0;
    }
}
Output:
20 Natural Numbers and their sum :
1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

SUM: 210
Example 2: pattern programming

public class ForDemo2
{  
public static void main(String[] args) 
{  
for(int i=1;i<=5;i++)
{  
for(int j=i;j>=1;j--)
{  
    System.out.print("* ");  
}  
System.out.println(); 
}  
}  
}  
 
Output:
* 
* * 
* * * 
* * * * 
* * * * * 
while loop:

The second loop available in Java is the while loop.

It is an entry-controlled loop.

Syntax
initialization;
while(condition)
  //loop body
  updation;
Data-flow diagram of while loop:
Example: Calculating factorial

public class ForDemo2
{  
public static void main(String[] args) 
{  
int num=6,fact=1;
int n=num;
while(n!=0){
    fact*=n;
    --n;
}
System.out.println("The factorial of "+num+" is "+fact); 
}  
}  

 
Output:
The factorial of 6 is 720
The do-while loop:

Unlike the for and while loops, the do-while is an exit-controlled loop i.e, it evaluates its test expression at the bottom of the loop after executing its loop-body statements. This means that a do-while loop always executes at least once.

Syntax:
initialization;
do{
   //loop body
  updation;
}while(condition);
  
Data flow diagram of do-while loop
Example:

class demo {
 public static void main(String args[])
 {
  int x = 21, sum = 0;

  do {
   sum += x;
   x--;
  }

  while (x > 10);

  System.out.println("Summation: " + sum);
 }
}
Output:
Summation: 176

Note: also read about the The switch vs if-else

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

2 days ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

5 days ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

3 weeks ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

1 month ago

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

1 month ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

2 months ago