Java2all.com  

Looping statements





Share This Topic -

Java’s repetition (iteration) statements are for, while, and do-while. These statements create what we commonly call loops.

As you probably know, a loop repeatedly executes the same set of instructions until a termination condition is met.

1. for :

  • The for loop repeats a set of statements a certain number of times until a condition is matched.
  • It is commonly used for simple iteration. The for loop appears as shown below.

Syntax :

for (initialization; condition; expression)
{
        Set of statements;
}
  • In the first part a variable is initialized to a value.
  • The second part consists of a test condition that returns only a Boolean value. The last part is an expression, evaluated every time the loop is executed.
  • The following example depicts the usage of the for loop.
 class for1
{
        public static void main(String args[])
        {
                int i;
                for (i=0;i<10;i++)
                {
                        System.out.println("\nExample of for loop ");
                }
        }
}

Output :
Example of for loop 
Example of for loop 
Example of for loop 
Example of for loop 
Example of for loop 
Example of for loop 
Example of for loop 
Example of for loop 
Example of for loop 
Example of for loop 

2. while :

  • The while loop executes a set of code repeatedly until the condition returns false.
  • The syntax for the while loop is given below:

Syntax :

while (condition)
{
      body(statements) of the loop
}
  • Where <condition> is the condition to be tested. If the condition returns true then the statements inside the <body of the loop> are executed.
  • Else, the loop is not executed.
 class while1
{
        public static void main(String args[])
        {
                int i=1;
                while(i<=10)
                {
                        System.out.println("\n" + i);
                        i++;
                }
        }
}

Output :
1
2
3
4
5
6
7
8
9
10

3. do while :

  • The do while loop is similar to the while loop except that the condition to be evaluated is given at the end.
  • Hence the loop is executed at least once even when the condition is false.
  •  The syntax for the do while loop is as follows:

Syntax :

do
{
      body of the loop
} while (condition);
 
In do while loop semicolon(;) is compulsory after while.
 
NOTE :  for and while loops are entry control loop because here conditions are checked at entry time of loop but do while loop is exit control loop because the condition is checked at exit time.
 class dowhile1
{
        public static void main(String args[])
        {
                int i = 1;
                int sum = 0;
                do
                {
                        sum = sum + i;
                        i++;
                }while (i<=10);
                System.out.println("\n\n\tThe sum of 1 to 10 is .. " + sum);
        }
}

Output :
The sum of 1 to 10 is .. 55
 
Now we are going to learn something more about loop, especially about for loop.
 
=> ?We can declaring Loop Control Variables Inside the for Loop like below.
 class ForTick 
{
    public static void main(String args[]) 
    {
         for(int n=10; n>0; n--)                 // here, n is declared inside of the for loop    
              System.out.println("tick " + n);
    }
}

Output :
tick 10
tick 9
tick 8
tick 7
tick 6
tick 5
tick 4
tick 3
tick 2
tick 1
 
NOTE : When you declare a variable inside a for loop, there is one important point to remember: the scope of that variable ends when the for statement does.
 
=> We can use comma operator inside for loop to declare more than one variable inside loop.
class Sample 
{
       public static void main(String args[]) 
       {
            int a, b;
            
            for(a=1,b = 4;a < b;a++,b--) 
            {
                System.out.println("a = " + a);
                System.out.println("b = " + b);
            }
       }
}

Output :

a =  1
b =  4
a =  2
b =  3
 
As you can see we declare two variables in for loop with the help of comma operator and use those variables inside loop as we did in above program.
 
 NOTE : The actual syntax of for loop is " for( ; ; ) ". It means we can use or access every for loop which contain two semicolon (;) inside round braces of for loop.
 
EX.
 
for( ; ; )
{
    // ...
}

Nested loops : 

Like all other programming languages, Java allows loops to be nested. That is, one loop may be inside another.
 
It can help you to scan and print multi dimensional array as well as in so many other application or program.
 
EX. 
 class Nested 
{
        public static void main(String args[]) 
        {
                int i, j;
                for(i=0; i<10; i++) 
                {
                        for(j=i; j<10; j++)
                        {
                             System.out.print(".");
                        }
                System.out.println();
                }
        }
}

Output :
 
..........
.........
........
.......
......
.....
....
...
..
.