Monday, July 22, 2024

Loop statements(for loop, while loop, do while loop)

 

Loop statements:

Loop statements are used to execute a group of statements repeatedly

//                          System.out.println("stmt-1");

//                           System.out.println("stmt-2");

//                           System.out.println("stmt-3");

 

//                           System.out.println("stmt-1");

//                           System.out.println("stmt-2");

//                           System.out.println("stmt-3");

 

//                           System.out.println("stmt-1");

//                           System.out.println("stmt-2");

//                           System.out.println("stmt-3");

copy paste the same stmts , it increases no of lines of code.

 

without rewriting , I want to execute group of stmts ---> Loop stmts

       1 time

      2 times

      3 times

.....  n no of times  ---->  Loop stmts

Instead of copying and pasting the same statements multiple times, you can use loops to execute these statements multiple times efficiently.

 

Types of Loop Statements in Java:

1. For Loop

2. While Loop

3. Do while Loop

1. For Loop

The for loop is used when you know in advance how many times you want to execute a statement or a block of statements.

Syntax:

  for(initialization; condition; increment/Decrement operator)

{                 1                   2             4

               stmt-1;

               stmt-2;

       3

 

}

1. it performs initialisation step-1

2.it checks condition -in step -2

    if cond is true, it goes inside the loop, executes all statements inside loop (step-3)

    and goes to step-4  performs increment/decrement -- again it goes to step-2

     if cond (step-2) is true, again it goes inside the loop, executes statements inside loop (step-3)

    and goes to step-4  performs increment/decrement ---- again it goes to step-2

    if cond (step-2) is true, again it goes inside the loop, executes statements inside loop (step-3)

    and goes to step-4  performs increment/decrement ---- again it goes to step-2

   

    if cond is false, control comes out of loop and   goes after out of loop, executes statements after loop

    Explanation:

  1. Initialization: This step is executed only once at the beginning of the loop. It typically initializes one or more loop counters.
  2. Condition: Before every iteration of the loop, this condition is evaluated. If the condition is true, the loop statements are executed. If the condition is false, the loop terminates, and control passes to the statement following the loop.
  3. Statements: The block of code inside the loop that gets executed as long as the condition is true.
  4. Increment/Decrement: This step is executed after each iteration of the loop. It updates the loop counter.

 

ex:

package LoopBasics;

public class ForLoop1 {

               public static void main(String[] args)

               {

//                           System.out.println("stmt-1");

//                           System.out.println("stmt-2");

//                           System.out.println("stmt-3");

                               

//                           System.out.println("stmt-1");

//                           System.out.println("stmt-2");

//                           System.out.println("stmt-3");

//                          

//                           System.out.println("stmt-1");

//                           System.out.println("stmt-2");

//                           System.out.println("stmt-3");

//                          

                                             // execute above stmts  for 2 times

                               

                              int  i;

                              for(i=1;i<=3;i++) // i = 1 = 1 +1  =2 =  2+1 =3 = 3+1 =4

                              {//     1 <= 3 true, ctrl goes inside for loop

                              //      2<=3 true ,  ctrl goes inside for loop

                                             //  3 <=3 true,  ctrl goes inside for loop

                                             //  4<=3  false, ctrl goes after for loop

                                              System.out.println("stmt-1");

                                              System.out.println("stmt-2");

                                              System.out.println("stmt-3"); //   ctrl   - performs incrementing

                              }

                             

                              System.out.println("after for loop");

               }

}

 

o/p:

 

ex:// display numbers 1 to 3

1

2

3

package LoopBasics;

public class ForLoop2 {

               public static void main(String[] args) {

                              // display numbers 1 to 3

                              //   1

//                               2

//                               3

                             

                              int i;

                              for(i=1;i<=3;i++)//i=1=  1 +1 = 2= 2 +1 =3 =  3+1 =4

                              {//     1<=3 true - ctrl goes inside for loop

                              //      2<=3 , true ,ctrl goes inside for loop

                                             //   3<=3, true ,ctrl goes inside for loop

                                             //    4<=3 false,           out side of for loop

                                            

                                             System.out.println(i);

                                             //                  1  //  ctrl -incrementing

                                             ///                 2

                                             //                  3

                              }

                              System.out.println("out of for loop");

 

               }

}

 

o/p:

i=1

i=2

i=3

out of for loop

ex: display numbers   1 to 5?

package LoopBasics;

public class ForLoop2 {

               public static void main(String[] args) {

                              // display numbers 1 to 5

                              //   1

//                               2

//                              

                              //  5

                             

                              int i;

                              for(i=1;i<=5;i++)//i=1=  1 +1 = 2= 2 +1 =3 =  3+1 =4

                              {//     1<=3 true - ctrl goes inside for loop

                              //      2<=3 , true ,ctrl goes inside for loop

                                             //   3<=3, true ,ctrl goes inside for loop

                                             //    4<=3 false,           out side of for loop

                                            

                                             System.out.println(i);

                                             //                  1  //  ctrl -incrementing

                                             ///                 2

                                             //                  3

                              }

                              System.out.println("out of for loop");

 

               }

}

 

HW display numbers  1 to 10

Explain  "for loop" with flow diagram

Display numbers 2 to 5

 

package LoopBasics;

public class ForLoop2To5 {

               public static void main(String[] args) {

                              // Display numbers 2 to 5

                              //                           2

                              //                           3

                              //                           4

                              //                           5

                              for(int i=2; i<=5; i++)// i =2, 2+1 =3,  3+1 = 4, 4+1 =5, 5+1 = 6

                              { //         2<=5 true ,  it enters for loop

                              //           3<=5  true, it enters for loop

                                             //       4<=5  , true ,it enters for loop

                                             //       5<=5 true, it enters for loop

                                             //        6<=5 false, ctrl goes after for loop

                                             System.out.println(i);

                                             //                 2

                                             //                 3

                                             //                 4

                                             //                 5

                              } // end of for loop

                             

                              System.out.println("out of for loop");

               }

}

 

o/p:

i=2

i=3

i=4

i=5

out of for loop

 

ex:  // Display numbers  3,2,1

3

2

1

                             

 

package LoopBasics;

public class ForLoop3To1 {

               public static void main(String[] args) {

//                           ex:  // Display numbers  3,2,1

//                                          3

//                                          2

//                                          1

                             

                              for(int i=3;i<=1;i++)// i=3

                              {//         3<=1  false, ctrl goes after for loop

                                             System.out.println(i);

                              }                            

                              System.out.println("out of for loop");

               }

}

 

o/p:

out of for loop

o/p for below program:

 

package LoopBasics;

public class ForLoop3To1 {

               public static void main(String[] args) {

                              // Note :  1 2 3  , for (int i=1;i<=3;i++)

                             

//                           ex:  // Display numbers  3,2,1

//                                          3

//                                          2

//                                          1

                             

                              for(int i=3;i>=1;i++)// i=3, 3+1 = 4, 4+1 =5, 5+1 =6,  7 , 8, 9

                              {//         3>=1  true, it enters for loop

                                             //      4>=1 true,it enters for loop

                                             //      5>=1 true,it enters for loop

                                             //      6>=1  true

                                             //.................  conidtion is always true, it goes to infinite looop

                                             ////          // cond is always true - will never become false -- loop -  it goes to infinite loop

                                             System.out.println(i);

                                             //                  3

                                             //                  4

                                             //                  5

                              }

                             

                              System.out.println("out of for loop");

               }

}

 

o/p:

3

4

5

6

7

....inifnite looop  - as condi is always true

 

ex:

package LoopBasics;

public class ForLoop3To1 {

               public static void main(String[] args) {

                              // Note :  1 2 3  , for (int i=1;i<=3;i++)

                             

//                           ex:  // Display numbers  3,2,1

//                                          3

//                                          2

//                                          1

                             

                              for(int i=3;i>=1;i--)// i= 3, 3-1 = 2,2-1 = 1, 1-1 = 0

                              {//         3>=1  true, it enters for loop

                                             //      2>=1  true,it enters for loop

                                             //      1>=1, true, it enters for loop

                                             //      0>=1 false, ctrl goes after for loop

                                             //.................  conidtion is always true, it goes to infinite looop

                                             ////          // cond is always true - will never become false -- loop -  it goes to infinite loop

                                             System.out.println(i);

                                             //                  3

                                             //                  2

                                             //                  1

                              }

                             

                              System.out.println("out of for loop");

               }

}

o/p:

---

3

2

1

out of for loop

Note :

1. To display numbers in ascending order, you typically use a for loop with an incrementing counter.

( in ascending order  1,2,3)

for (int i = 1; i <= 3; i++) {

    System.out.println(i);

}

2. To display numbers in descending order, you need to modify the condition and use a decrement operator.

while displaying numbers in reverse order - 3,2,1. Use

   i=3;i>=1;i--

1. change codition   '<=' -->  to  '>='

2.  use always  Decrement operator  --  don’t use increment operator

 

Key Points:

1.     Change Condition:

    • For ascending order, use <= in the condition.
    • For descending order, use >= in the condition.

2.     Decrement Operator:

  • Always use the decrement operator (i--) for counting down.
  • Avoid using the increment operator (i++) when counting down.

 

ex: display numbers 5 ,4,3,2,1

 

package LoopBasics;

public class ForLoop5To1 {

               public static void main(String[] args) {

                              // Display numbers 5,4 3,2,1

                             

                              for(int i=5;i>=1;i--)

                              {

                                             System.out.println(i);

                              }

                             

                              System.out.println("out of for loop");

               }

}

o/p:

5

4

3

2

1

out of for loop

HW  display numbers 5 to 2

HW  display numbers 10  to 5

 

We can  write  i--,  --i  or i=i-1  in for loop

 and also   we can write i = i+2

   or i= i+3;..etc

 

ex:  write o/p for below program?

package LoopBasics;

public class ForLoop1 {

               public static void main(String[] args)

               {

                              for(int i=1;i<=3;i= i+2)// i= 1, 1 + 2 = 3, 3 +2 = 5

                              {//        1<=3 true, enter for loop

                                             //     3<=3 true,   enter for loop

                                             //     5<=3 false, ctrl goes after for loop

                                             System.out.println(i);//

                                             //                  1

                                             //                   3

                              }

                             

                              System.out.println("out of for loop");

               }

}

o/p:

1

3

out of for loop

 

While Loop:

The while loop repeatedly executes a block of statements as long as a specified condition is true.

Syntax:

// Initialization;

while (condition) {

    // Statement 1;

    // Statement 2;

    // Increment/Decrement;

}

 

package LoopBasics;

public class whileLoop1 {

               public static void main(String[] args)

               {

                              int i=1;

                              while(i<=3)

                              {  //  1<=3 true , for loop

                                             // 2<=3 true, for loop

                                             // 3<=3 , true , for loop

                                             //  4<=3 false,   ctrl goes after while loop

                                             System.out.println("stmt-1");// // //

                                             //                    stmt-1

                                             //                    stmt-1

                                             //                    stmt-1

                                            

                                             i++;// i = 1 +1 = 2 , 2+1 = 3, 3+1 =4

                              } // end of while loop

                              System.out.println("after while loop");

 

               }

}

o/p:

stmt-1

stmt-1

stmt-1

after while loop

 

ex:   //  display number 1 to 3   using while loop

package LoopBasics;

public class whileLoop1To3 {

               public static void main(String[] args)

               {

                              //  display number 1 to 3 for(int i=1;i<=3;i++)

              

                              int i= 1;

                              while(i<=3)

                              {   // 1 <=3 , true, enter for loop

                                             // 2<=3 , true ,  enter for loop

                                             // 3<=3 , true ,enter for loop

                                             //  4<=3  false, ctrl goes after while loop

                                             System.out.println(i);

                                             //                  1

                                             //                  2

                                             //                   3

                                            

                                             i++;// i = 1 +1 =2, 2+1 = 3, 3+1 =4

                              }

                              System.out.println("after while loop");//

               }

}

 

o/p:

1

2

3

after while loop

 

HW display number 1 to 5 using while loop

 

ex: //  display number 2 to 5  using while loop ( incrementing numbers use <= )

               package LoopBasics;

public class whileLoop2To5 {

               public static void main(String[] args)

               {

                             

  //  display number 2 to 5 using while loop   incrementing numbers use <=

                              //                     Decrementing numbers , use >=

               //  for(int i=2;i<=5;i++)

                                                           

                              int i=2;

                             

                              while(i<=5)

                              {//    2<=5 , true , enters while loop

                                             // 3<=5  , true ,  enters while loop

                                             // 4<=5  , true , enters while loop

                                             // 5<=5, true , enters while loop

                                             // 6<=5  false,  ctrl goes after while loop

                                             System.out.println(i);

                                             //                 2

                                             //                 3

                                             //                 4

                                             //                 5

                                             i++;// i =2 , 2+1 = 3, 3+1 = 4, 4+1 =5, 5+1= 6

                              }

                             

               }

}

o/p:

2

3

4

5

HW Write o/p for below program?

package LoopBasics;

public class WhileLoop1 {

               public static void main(String[] args) {

                             

 

                              int i=3;

                              while(i<=5)

                              {   //

                                             //

                                             // 

                                             //

                                            

                                              System.out.println(i);// 

                                              i++;// i =

                              }

                             

                              System.out.println("after while loop");//

               }

}

o/p:

 

ex: Write o/p for below program?

package LoopBasics;

public class whileLoop2To5 {

               public static void main(String[] args)

               {

                              int i=3;

                              while(i>=5)

                              {   // 3>=5 false, ctrl goes after while loop

                                             System.out.println(i);// 

                              }

                              System.out.println("after while loop");//

 

               }

}

 

o/p:

after while loop

 

HW Display numbers 3 to 1 using while loop

HW Display  numbers 5 to 2  using while loop

 

 //  display number 3 to 1      reverse order 3,2,,, use >=, -- decrement operator

                              //                in normal order  1,2,3,.. , use <= ,  and ++ increment operator

       

 

 

Display the o/p for below program?

package LoopBasics;

public class whileLoop2To5 {

               public static void main(String[] args)

               {

                              //  display 3 to 1

                              int i=3; //   while reversing,  we have to use decrement

                              while(i>=1)

                              { //   3>=1 true, enters while loop

                                             // 3>=1,  true,  enters while loop

                                             // 3>=1,true,  enters while loop

                                             //                                            / /   3>=3 -True  it goes to infinite loop  as condi is always true  and i=3 always (we are not incrementing/ decrementing i val)

                                             System.out.println("i="+i); //

                                             //                  i= 3

                                             //                  i =3

                                             //                  i =3

                                             //...................infinite loop as i value is always = 3

                                            

                                            

                              }

                              System.out.println("after while loop");

                              //Note :

                             

                              //while writing loop stmts, 

                              //write (be careful about) proper  increment/ decrement operators- else it goes to infinite loop

                              // Dont forget to write ++i, --i; stmts inside while loop

               }

}

o/p:

i =3

 i=3

i=3

...

...

... infinite loop

 

HW  Display o/p for below program?

package LoopBasics;

public class WhileLoop1 {

               public static void main(String[] args) {

                               

               //  display 3 to 1

                                               int i=3; //   while reversing,  we have to use decrement

                                               while(i>=1)

                                               { //  

                                                              //

                                                              //

//                                                           / /   6>=3 -True  it goes to infinite loop  as condi is alwyas true  and i=3 always (we are not incrementing/ decrementing i val)

                                                             System.out.println("i="+i); //

                                                             

                                                             i++;//  i=

                                               }

                                            

                                             System.out.println("after while loop");

                                            

//                                          Note :

//                                                         while writing loop stmts,  write (be careful about) proper  increment/ decrement operators- else it goes to infinite loop

               }

}

o/p:

 

 

HW Write o/p for below program?

package LoopBasics;

public class WhileLoop1 {

               public static void main(String[] args) {

                               

               //  display 3 to 1

                                               int i=3; //   while reversing,  we have to use decrement

                                               while(i>=1)

                                               { //   

                                                              //

                                                              //

//                                                           / /   6>=3 -True  it goes to infinite loop  as condi is always true.

                                                             System.out.println("i="+i); //

                                                             

                                                             i++;//  i=

                                               }

                                            

                                             System.out.println("after while loop");

                                            

//                                          Note :

//                                                         while writing loop stmts,  write (be careful about) proper  increment/ decrement operators- else it goes to infinite loop

               }

}

o/p:

3

4

5

.....inifnte loop

ex: Display the o/p for below program?

package LoopBasics;

public class whileLoop1 {

               public static void main(String[] args)

               {

               //  display 3 to 1

                                int i=3; //   while reversing,  we have to use decrement

                               

                                while(i>=1)

                                {   // 3>=1  true, enters while loop

                                               // 2>=1 , true, enters while loop

                                               // 1>=1  ,true, enters while loop

                                               // 0>=1, false  ,   ctrl goes after while looop

                                               // 0>=1 False-  does not enter while loop and comes out of while loop and comes after while loops

                                              System.out.println("i="+i); // 3  2            1

                                              //                    i =3

                                              //                                                                                      i =2

                                              //                     i=1

                                              

                                              i--;//  i=3, 3-1 =2, 2-1 =1,  1-1 = 0

                                }

                             

                              System.out.println("after while loop");

                             

//                           Note :

//                                          while writing loop stmts,  write (be careful about) proper  increment/ decrement operators- else it goes to infinite loop

 

               }

}

 

o/p:

i=3

i=2

i=1

after while loop

HW Display o/p for below program?

package LoopBasics;

public class WhileLoop1 {

               public static void main(String[] args) {

                               

               //  display 3 to 1

                                               int i= -1 ; //   while reversing,  we have to use decrement

                                               System.out.println(-1>=1);//  false

                                               while(i>=1)  // 

                                               { // -

                                                             

                                                             System.out.println("i="+i); //

                                                             

                                                             i--;//  i=

                                               }

                                            

                                             System.out.println("after while loop");

                                            

//                                          Note :

//                                                         while writing loop stmts,  write (be careful about) proper  increment/ decrement operators- else it goes to infinite loop

               }

}

o/p:

false

after while loop

HW Display numbers 5 to 1 using while loop

HW Display numbers 10 to 1 using while loop

HW Display numbers 5 to 2 using while loop

HW Display numbers 10 to 3 using while loop

HW Display numbers 10 to -2 using while loop

Display the o/p for below program?

package LoopBasics;

public class WhileLoop1 {

               public static void main(String[] args) {

                             

                              int i=1;

                             

                              while(i<=3)

                              {   //

                                             //

                                             //

                                             System.out.println(i); //

                                             i = i+2;// i=

                              }

                             

                              System.out.println("after while loop");

               }

}

o/p:

1

3

after while loop

Do while loop:

do is a predefined keyword in Java language used to write do-while loop statements.      

Syntax:

do {

    stmt-1;

    increment / Decrement operator;

}

while (condition);  // Note: Don't forget to write a semicolon (;) at the end of the do-while loop statement

This loop ensures that stmt-1 is executed at least once before the condition is checked.

 

ex:  o/p for below program

package LoopBasics;

public class DoWhileLoop1 {

               public static void main(String[] args)

               {

                             

                              int i=1;

                              do

                              {

                                             System.out.println("stmt1-");//  these stmts gte executed at least once

                                             //                 stmt-1

                                             //                                                                        stmt-1

                                             //                                                                        stmt-1

                                             i++;// i=1 , 1+1 =2 , 2+1 = 3, 3+1 =  4

                              }

                              while(i<=3);//  at end we are checking condi

                              //    2<=3  true, ctrl goes inside do block

                              //    3<=3 true, ctrl goes inside do block

                              //    4<=3 false , ctrl goes after do while loop

                             

                              System.out.println("after do while loop");

               }

}

 

o/p:

stmt1-

stmt1-

stmt1-

after do while loop

 

ex2:

package LoopBasics;

public class doWhileLoop1 {

               public static void main(String[] args) {

                              int i=1;

                             

                              do

                              {

                                             System.out.println("i="+ i);// i=  1

                                             //   do block smts gets executed at least once even if cond is false                                                           

                                             i++;// i =

                              }

                              while(i>=3);

                              //    2>=3 False - it wont go do block

               //     - does not enter do block  and goes after  do while loop

                              System.out.println("after do while loop");            

 

               }

}

o/p:

i=1

after do while loop

Break, continue stmt:

                     in Switch cond

switch()

{

case a:

       stmt1;

       break; //  can be  used to come out of switch block

 

}

------------------

 

                              //  break:  is predefined k/w in java lang

// It can be used to exit from a loop statement (for loop, while loop, do-while loop) or a switch block

                              Or it  can be used to come out of loop stmt i.e for loop, while loop, do -while loop, Switch block also.

                             

write o/p for below program?

package LoopBasics;

public class break1 {

               public static void main(String[] args) {

                             

                              //  for

                              int i;

                              for(i=1;i<=3;i++) // i= 1 , 1+1 =2

                              {    // 1<=3, true , enters for loop

                                             //   2<=3, true , enters for loop

                                             System.out.println("i="+ i);//

                                             //                  i= 1

                                             //                   i= 2

                                             if(i==2)

                                             { //  1==2 false, ctrl goes after if block

                                                            // 2 == 2 , true , enters if block  

                                                            break;//  -->  come out of for loop

//                                                         System.out.println("after break ");//  Unreachable code

                                                            // Note:  dont write any stmts after break k/w

                                             }             

                                            

                                             System.out.println("after break ");

                                            

                              } // end of for loop

                              System.out.println("after for loop");

 

               }

}

o/p:

i=1

after break

i=2

after for loop

 

write o/p for below program?

package LoopBasics;

public class break1 {

               public static void main(String[] args) {

                             

                              int i;

                             

                              for(i=1;i<=3;i++) // i =1

                              {              //   1<=3 true, enters for loop 

                                              System.out.println("i="+ i);//

                                              //                   i =1

                                              

                                              if(i==1)

                                              { // 1==1 true , enters if block

                                                             break;//   come out of for loop

//                                                         System.out.println("after break ");//  Unreachable code

                                                             // Note:  dont write any stmts after break k/w

                                              }                             

                                              System.out.println("after break ");

                              }// end of for loop

                             

                              System.out.println("after for loop");       

 

               }

}

 

o/p:

i=1

after for loop

 

break in while loop:

package LoopBasics;

public class breakInsideWhileLoop {

               public static void main(String[] args) {

                              int i=1;

                             

                              while(i<=3)  //

                              {  // 1<=3 , true , enters while loop

                                             //2<=3, true, netrs while loop

                                             System.out.println("Enters loop");

                                             //                   Enters loop

                                             //          Enters loop

                                            

                                             if(i==2)

                                             { //1 ==2 , false,  ctrl goes after if block

                                                            // 2 == 2 true,   ctrl goes inside if block

                                                           

                                                            break;// come out of  while loop

                                             }

                                            

                                             i++;// i = 1 +1 =2

                              } //  end of while loop

                                            

                                             System.out.println("after while loop");

              

               }

}

o/p:

Enters loop

Enters loop

after while loop

HW   what is the o/p for below program?

                              int i=1;

                              while(i<=5)

                              {                                           

                                             if(i==3)

                                             {

                                                            System.out.println("i="+ i);

                                                            break;

                                             }

                                             System.out.println("after if i="+i);

                                             i++;

                              } //  end of while loop

                                            

                                             System.out.println("after while loop");

 

HW Write break stmt  inside  "do while" loop?

Note:

break can be used in switch, for , while , do while loop

Switch()

{

case val:

          stmt1;

         break;

case 2 :

         stmt2;

        break;

 

}

 

for()

{

    break;

 

}

 

while()

{

   break;

}

 

do

 

{

   break;

}

while(cond);

 

continue: 

// continue: is a predefined keyword in Java language

// It can be used to skip the current iteration of a loop (for loop, while loop, do-while loop) and proceed to the next iteration

 

package LoopBasics;

public class continue1 {

               public static void main(String[] args) {

                              //for loop 

                             

                              for(int i=1;i<=3;i++) // i = 1, 1+1 = 2 , 2+1 =3, 3+1 =4

                              {     //    1<=3 true, for loop 

                                             //      2<=3 true , enters for loop

                                             //      3<=3 true, for loop

                                             //      4<=3 false, ctrl goes after for loop

                                             if(i==2)

                                             { // 1 ==2 false, ctrl goes after if block

                                                            // 2 == 2 true, ctrl inside if block 

                                                            // 3== 2  false, after if block

                                                            //using continue statement 

                                                            continue;//it will skip the rest  of statements

                                                           

                                             }  // end of if block

                                             System.out.println(i); //

                                             //                     1

                                             //                       3

                              }  // end of for loop

                             

                              System.out.println("after loop");

               }

}

o/p:

1

3

after loop

 

package LoopBasics;

public class continue1 {

               public static void main(String[] args) {

                             

                              int i;

                             

                              for(i=1;i<=3;i++) // i= 1 , 2 , 2+1 = 3 , 3+1 =4

                              {              //  1<=3  true , enters for loop

                                             //  2<= 3 true ,  for loop

                                             //  3<=3  trye, for loop

                                             //   4<=3  false, ctrl goes after for loop

                                             System.out.println("i="+ i);// i=

                                             //                   i=1

                                             //                    i=2

                                             //                    i=3

                                            

                                             if(i==1)

                                             { // 1 == 1 true 

                                                            // 2 ==1 false,   after if block

                                                            // 3 ==1 false,  after if block

//                                                         break;//   come out of loop // last executeable stmts inside loop

                                                             continue;

//                                                         System.out.println("after break ");// error :  Unreachable code

                                                            // Note:  dont write any stmts after break k/w

                                                            //   Dont  write any stmts after continue  k/w

                                             }             

                                            

                                             System.out.println("after break ");

                              } // end of for loop

                              System.out.println("after for loop");

               }

}

o/p:

i=1

i=2

after break

i=3

after break

after for loop

 

Nested If, for:

 write o/p for below program?

                              int a= 50;

                              int b=20;

                              int c=30;

                             

                              if(a>15) // outer if block

                              { // 50 >15 true, enters if block

                                             System.out.println("a> 15");// a>15

                                             if(b>10)  // inner if block              

                                                            // Nested If:   writing if condi inside if stmt

                                             { // 20 >10 , true , enters if block

                                                            System.out.println("b>10");// b>10

                                             }

                              }

 

o/p:

a> 15

b>10

 

Nested For loop:

Def:

A nested for loop is a loop inside another loop. The inner loop completes all its iterations for each iteration of the outer loop.

For() // outer loop

{

  For() // inner for loop

{

 

}

}

Note:

  • Once the inner for loop completes, control goes back to the outer for loop.
  • After executing the inner for loop completely, control goes to the outer for loop.
  • If the condition is false in the outer for loop, control goes after the outer for loop.
  • If the condition is false in the inner for loop, control goes after the inner for loop.

·        for each iteration of the outer loop, the inner loop runs completely. When the inner loop completes, control goes back to the outer loop to proceed with the next iteration.

 

 

 

package LoopBasics;

public class NestedForLoop {

               public static void main(String[] args) {

                              // TODO Auto-generated method stub

                              // nested for loop:  

                              for(int i=1;i<=3;i++) // outer for loop i=  1 , 1+1 =2, 2+1 =3, 3+1 =4

                              { //        1<=3 true, enters for loop   

                                             //      2<=3 true,  enters for loop   

                                             //      3<=3  true , enters for loop

                                             //      4<=3  false,  come out of outer for loop

                                             System.out.println("inside 1st for loop");// inside 1st for loop

                                            

                                             for(int j=1;j<=2;j++) //  inner  for loop  j= 1 , 1+1 =2, 2+1 =3

                                             { //       1 <=2 , true , enters inner for loop

                                                            //      2<=2 true ,enters inner for loop

                                                            //      3<=2 false,  after inner for loop

                                                            System.out.println("i="+ i + ",j="+j);

                                                            //                                                                        i  =1    ,j =1

                                                            //                   i=1     , j=2               

                                                            //                   i =2    , j=1                   

                                                            //                   i =2    , j =2

                                                            //                    i=3  ,,  j=1

                                                            //                    i=3   j =2

                                                            //                                                       

                                             }//  end of inner loop

                                             System.out.println("after inner loop");//               gets executed   

                                             // ctrl will go outer for loop

                              } // end of outer loop

                              System.out.println("after outer loop");

 

 

               }

}

 

o/p:

inside 1st for loop

i=1,j=1

i=1,j=2

after inner loop

inside 1st for loop

i=2,j=1

i=2,j=2

after inner loop

inside 1st for loop

i=3,j=1

i=3,j=2

after inner loop

after outer loop

 

write o/p for below program?

 

package LoopBasics;

public class NestedForLoop2 {

               public static void main(String[] args) {

                             

                              for(int i=2;i<=4;i++) // outer loop i=

                              {

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

                                             {                                                          

                                                            System.out.println("i="+ i + ",j="+j);//

                                            

                                             }//  end of inner loop

                              }

                              System.out.println("after outer loop");

               }

}

o/p:

i=2,j=1

i=2,j=2

i=2,j=3

i=3,j=1

i=3,j=2

i=3,j=3

i=4,j=1

i=4,j=2

i=4,j=3

after outer loop

HW  write o/p for below program:

                             

                              // nested for :  writing for loop inside for loop

                             

                              for(int i=1;i<=2;i++) // outer loop i=

                              {

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

                                             {                                                          

                                                            System.out.println("i="+ i + ",j="+j);//

                                            

                                             }//  end of inner loop

                              }

                              System.out.println("after outer loop");

 

o/p:

Revision:

  cond stmts-  if(),  if else, if .. else if()   else if() , Switch

 Loop :

      for loop

       while

      do while

     break, continue

No comments:

Post a Comment

git commands MCQ

 Here are some multiple-choice questions (MCQs) on Git commands relevant for Selenium: 1. Which Git command is used to clone a remote reposi...