Ad Code

Responsive Advertisement

Conditional Statements (Loops)

 

Loops

If the same condition wants to be executed multiple times, loops can be used instead of typing the statement repeatedly. For instance, if user wants to print a number 5 times, instead of mentioning the output statement 5 times, it can be mentioned within a loop.

Advantages of loops

·      Consumes less storage

·      Reduced time complexity

·      Minimization of space complexity



Types of loops

In Java, loops are categorized into 4 types as for loop, while loop, do- while loop and for- each loop.

(i)                      For loop

The keyword used here is ‘for’ and the general syntax to use this loop is :

for (Initialization; Condition; Increment/ Decrement)

{

//Code to be executed;

}

This loop executes until the condition within the ‘for’ statement holds false.

Eg) Write a Java program that outputs the numbers for 11 to 15 using for loop

                      class loop{

                      public static void main(String args[]){

                       for(int i=11;i<=15;i++)
                       {
                      System.out.println(i);
                       }

                       }

                        }

Explanation

Iteration 1:

i=11

11< 15 : True

11 is printed

i= 11+1= 12

Iteration 2:

i=12

12< 15 : True

12 is printed

i= 12+1= 13

Iteration 3:

i=13

13< 15 : True

13 is printed

i= 13+1= 14

Iteration 4:

i=14

14< 15 : True

14 is printed

i= 14+1= 15 

Iteration 5:

i=15

15= 15 : True

15 is printed

i= 15+1= 16

Iteration 6:

i=16

16< 15 : False

The for loop end since the condition is false.

Output


(ii)                      While loop

‘While’ keyword is used for executing ‘while’ loop statements. It is similar to a ‘for’ loop, except in ‘while’ the condition is only given within the ‘while’ statement. In case of ‘for’ loops, initialization and condition is given within a statement. The general syntax for while loop is,

while(condition)

{

//code to be executed

}

Eg) Write a Java code to print even numbers within 1 to 10 using while loop

                      class loop {
                       public static void main(String args[]) {
                       int i = 0;
                       while (i < 9) {
                       i = i + 2;
                      System.out.println(i);
                      }
                      }
                     }

Explanation

Iteration 1:

i=0;

0< 9: True

I= 2;

2 is printed

Iteration 2:

i=2;

2< 9: True

I= 4;

4 is printed

Iteration 3:

i=4;

4< 9: True

I= 6;

6 is printed

Iteration 4:

i=6;

6< 9: True

I= 8;

8 is printed

Iteration 5:

i=8;

8< 9: True

I= 10;

10 is printed

Iteration 6:

i=10;

10< 9: False

Since the condition fails, the loop execution terminates.

Output


(iii)                   Do while loop

In this loop statement, the code gets executed once and then only the condition is checked. In case of while statements, the code gets executed only after the statement holds true. This is the major difference between ‘while’ and ‘do-while’ loops. ‘Do-while’ loops can be used in applications which seeks executing the statement atleast once eventhough the condition is false.

Consider a vending machine which must be programmed to ask the customer what they want to buy regardless of whether he is really going to buy. In such cases, a do-while loop can be used. The syntax of the do-while loop is,

do

{

//statement

}while (condition)

 

Eg) Write a Java program using do-while loop to print all even numbers within 1 to 10.

                      class loop {
                      public static void main(String args[]) {
                      int i=0;
                      do{
                       i=i+2;
                       System.out.println(i);
                      }while(i<=8);
                      }
                      }

 

Explanation

Iteration 1:

i=0;

i=0+2= 2;

2 is printed

 

Iteration 2:

2<8 : True

i=2;

i=2+2= 4;

4 is printed

 

Iteration 3:

4<8 : True

i=4;

i=4+2= 6;

6 is printed

 

Iteration 4:

6<8 : True

i=6;

i=6+2= 8;

8 is printed

 

Iteration 5:

8= 8 : True

i=8;

i=8+2= 10;

10 is printed

 

Iteration 6:

10< 8 : False

The condition fails and hence the loop stops its execution

Output


(iv)                 For-each loop

This loop is specifically designed for printing arrays. To know more about arrays, you can CHECK HERE. In case of ‘for’ loop, a condition is checked and further the statement is executed. In case of ‘for-each’ loop, no such condition is checked. A new variable has to be assigned and the statement is executed.

Eg) Print the elements of an array using for-each loop statement

             class loop {
                        public static void main(String args[]) {
                         
//Declaring an array
                         int a[] = {11, 29, 19, 20};
                       
 //Printing the array using for-each loop
                         System.out.println("The array elements are:");
                         for (int k : a) {
                          System.out.println(k);
                         }
                         }
                         }

Output


Disadvantages of for-each loop

For-each loop offers speed execution but the elements are printed only in forward direction. There is no option for printing the elements in reverse order.


PREVIOUS

Post a Comment

1 Comments