coderz.py

Keep Coding Keep Cheering!

Iteration Statements(Loops)

method overloading and method overriding
  • 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
iteration
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:
iteration
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

Leave a Comment

Your email address will not be published. Required fields are marked *

Advertisement