Image

Java - Core Java - Looping Statement

Looping Statement

while, do-while and for control looping and are sometimes classified as iteration statements. A statement repeats until the controlling Boolean expression evaluates to false.

The Boolean-expression is evaluated once at the beginning of the loop that is check the condition at the starting time of loop and again before each further iteration of the statement

while loop

initialization;
while(condition)
{
     //statement
       updation(increment/decrement);
}
WAP to print from 10 to 1
class Whileloop
{
public static void main(String args[])
{
        int i=10;             
        while(i<0)
        {
            System.out.println(i);
            i--;
        }   
 }
 }
O/P
D:\javap>javac AJTD1.java
D:\javap>java Whileloop
10
9
8
7
6
5
4
3
2
1

do while loop

When you want to enter inside of loop at least one time without checking any condition then you should go for do while loop here the condition will come when you will go for exit from loop so that it is also called as exit point loop

initialization;
do
{
     // statement
  updation(increment/decrement);
} while(condition);
Ex: WAP to count number by do while
class DoWhileDemo {
     public static void main(String[] args){
          int count = 1;
          do {
               System.out.println("Count is: " + count);
               count++;
          } while (count <= 11);
     }
}
O/P
D:\javap>javac AJTD1.java

D:\javap>java DoWhileDemo
Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5
Count is: 6
Count is: 7
Count is: 8
Count is: 9
Count is: 10
Count is: 11    

The sole difference between while and do-while is that the statement of the do-while always executes at least once, even if the expression evaluates to false the first time. In a while, if the conditional is false the first time the statement never executes. In practice, do-while is less common than while.

for loop

A for loop performs initialization before the first iteration. Then it performs conditional testing and, at the end of each iteration, some form of “stepping” The form of the for loop is:

for(initialization; condition; updation)
{
     //statement
}
Ex: WAP to print 10 times * using for
class Forloop
{
public static void main(String args[])
{
        int i,j;
        for(i=1;i<=1;i++)

                System.out.println(" ");
        
         for(j=i;j<=10;j++)
                System.out.println("*");
 }
 }

class Forloop
{
public static void main(String args[])
{
        int i,j;
        for(i=1;i<=1;i++)

                System.out.println(" ");
        
         for(j=i;j<=10;j++)
                System.out.println("*");
 }
 }
O/P
D:\javap>javac AJTD1.java

D:\javap>java Forloop

*
*
*
*
*
*
*
*
*

for each loop

For-each is another array traversing technique like for loop, while loop, do-while loop introduced in Java 5 version. It starts with the keyword for like a normal for-loop.

We can declare a variable that is the same type as the base type of the array, followed by a colon (:), which is followed by the array name. In the loop body, you can use the loop variable you created rather than using an indexed array element. It’s commonly used to iterate over an array or a Collections class (eg, Vector, ArrayList)

Syntax:
for (type var : array) 
{ 
    statements using var;
}
 
Program to find maximum score using for each loop
class ForEach     
{
    public static void main(String[] arg)
    {
        {
            int[] marks = { 125, 132, 95, 116, 110 };             
            int highest_marks = maximum(marks);
            System.out.println("Student highest score is " + highest_marks);
        }
    }
    public static int maximum(int[] numbers)
    { 
        int max = numbers[0];        
        // for each loop
        for (int num : numbers) 
        {
            if (num > max)
            {
                max = num;
            }
        }
    return max;
    }
}
Output
Student highest score is 132