Solutions for Class 10 ICSE Logix Kips Computer Applications with BlueJ Java | IT Developer <?php echo $page_title; ?>
IT Developer

Nested for Loops in Java

Chapter 10

Nested for Loops in Java

Class 10 - Logix Kips ICSE Computer Applications with BlueJ


Share with a Friend

Java Program: Difference between break & labelled break, and continue & labelled continue statement


3 (i). Distinguish between the following:

break statement and labelled break statement


3 (ii). Distinguish between the following:

continue statement and labelled continue statement


3 (i). Difference between break statement and labelled break statement

break statement

Labelled break statement

Terminates the nearest enclosing loop or switch statement.

Terminates a specific outer loop identified by a label.

Used inside for, while, do-while, or switch.

Used with a label attached to a loop.

Control exits only one loop level.

Control can exit multiple nested loops at once.

Simple syntax: break;

Syntax: break <labelName>;

Commonly used in single loops or switch cases.

Useful when working with nested loops.

break statement is used to take the control out of the loop control structure immediately enclosing it.

Labelled break statement transfers program control out of the code block whose label is specified as its target.

For example,
for (int i = 1; i <= 3; i++)
  {
     for(int j = 1; j <= 2; j++)
       {
          if (i == 2)
           {
               break;
           }
              System.out.println("Iteration value of inner loop: " + j);
      }
     System.out.println();
  }

For example,
outer:
for (int i = 1; i <= 3; i++)
   {
      for(int j = 1; j <= 2; j++)
        {
           if (i == 2)
             {
                 System.out.println("Terminating the Outer-loop");
                  break outer;
             }
       System.out.println("i =" + i +" ; j =" + j);
  }

3 (ii). Difference between continue statement and labelled continue statement

continue statement

Labelled continue statement

Skips the current iteration of the nearest loop.

Skips the current iteration of a specified outer loop.

Control moves to the next iteration of the same loop.

Control jumps to the next iteration of the labelled loop.

Used without labels.

Used with labels.

Syntax: continue;

Syntax: continue <labelName>;

Works within a single loop.

Mainly used in nested loops.

continue statement is used to skip the current iteration of a loop and move on to the next iteration.

A labelled continue statement skips the current iteration of an outer loop by using a label before the loop and including the same label in continue statement.

For example,
for (int i = 0; i <= 3; i++)
  {
     System.out.println("i =" + i);
     for(int j = 0; j < 3; j++)
       {
           if (i == 2)
             {
                continue;
             }
           System.out.print("j = " + j + " ");
       }
   System.out.println();
}

For example,
outerLoop:
for (int i = 0; i <= 3; i++)
  {
     for(int j = 0; j < 3; j++)
       {
          if (i == 2)
            {
               continue outerLoop;
               System.out.println("i =" + i +" ; j =" + j);
            }
        }
  }