Friday, September 3, 2021

# 18. nested loops

 whenever we know the number of iterations in advance means we know how many times we need to execute the program then we go for loop

 whenever we don't know the number of iterations in advance, we don't know how many times we need to execute the program then we go for while loop 

I want to execute my loop body at least once irrespective of the condition then we can go for do-while

do and then check the condition

what is nested for loop?

A for loop inside a for loop is called nested for loop

what is nested while loop?

A while loop inside a while loop is called nested while loop

A loop inside another loop is called nested loop


for (init section; condition; inc/dec section)

{

;;;;;;;;;;;

    for (init section; condition; inc/dec section)

     {

        ;;;;;;;;;;;

       }

}


Example:

public class LoopsDemo{

public static void main(String[] args)  {

        for (int i=0; i<3; i++)        // outer loop

        {

            System.out.println("OL: " + i);

            for(int j=0; j<=3; j++)        // inner loop

            {

                System.out.println("IL: "+j);

            }

        }

   }

}

// once the control is coming to the inner loop, this inner loop is executed till the condition is false

OUTPUT:

OL: 0

IL: 0

IL: 1

IL: 2

IL: 3

OL: 1

IL: 0

IL: 1

IL: 2

IL: 3

OL: 2

IL: 0

IL: 1

IL: 2

IL: 3

OL: 3

IL: 0

IL: 1

IL: 2

IL: 3


we saw break and continue in switch case.

break:

a break is a keyword or a statement which we can use inside the switch cases and which we can use inside the looping mechanism that can be while, do-while, nested, and for loop

continue:

based on some condition if we want to skip the current iteration then we go for continue 


public class LoopsDemo{

public static void main(String[] args)  {

        for (int i=0; i<=5; i++)        // outer loop

        {

             System.out.println(i);

              if (i==2) {

              break;        // directly it comes out of for loop

               }

          }

           System.out.println("OOFL");

    }

}

OUTPUT:

0

1

2

OOFL


continue:

public class LoopsDemo{

public static void main(String[] args)  {

        for (int i=0; i<=5; i++)        

        {

            

              if (i==2) {

              continue;        // skips current iteration    

               }

             System.out.println(i);

    }

        System.out.println("OOFL");

  }

}

OUTPUT:

0

1

3

4

5

OOFL


break:

public class LoopsDemo{

public static void main(String[] args)  {

        for (int i=0; i<=5; i++)        

        {

            

              if (i==2) {

                break ;  

               }

             System.out.println(i);

    }

        System.out.println("OOFL");

  }

}

OUTPUT:

0

1

2

OOFL


PART 1 completed


part1  java basics

part2  oops

part3  selenium

part4

frameworks






















No comments:

Post a Comment

# 16. control statements

 Control statements: In java, by using control statements we can tell the flow of execution. in which order the statements should execute. d...