Java puzzle 25: return, break and continue

<< Java Puzzles   Green = Easy,  Blue = Normal,  Red = Hard
 P25   P26   P27   P28   P29   P30 
 By title 
>>

Questions:
1. What is the output of the following code?
2. What is the output if you replace the return; statement with the break; statement?
3. What is the output if you replace the return; statement with the continue; statement?

Java puzzle 25: The difference between return, break and continue

Level: Normal

public class MyClass
{
  static int count = 0;
  
  void print()
  {
    int[] numbers = {2, 3, 4};
    outer:for(int x: numbers)
    {
      count++;
      inner:for(int n = 0; n < 5; n++)
      {
        count++;
        return;
        // break;
        // continue;
      }
    }
  }
  public static void main(String[] args)
  {
    new MyClass().print();
    System.out.print(count);
  }
}
Author: Sar Maroof

Answer explanation

The outer loop is repeated 3 times, because there are three elements in the array.

  • return:
    When the return statement is reached, the execution of the method print is terminated.
    By using the return statement the outer loop is executed once, and the inner loop is also executed once. That is because at the first execution of the inner loop the return statement is reached.
  • break: By using the the break lable, the outer loop is executed three times, but the inner loop is terminated each time when the break statement is reached.
    So, count = 3 (outer loop) + 1 x 3 (inner loop) = 6.
  • continue: By using the continue statement the outer loop is executed 3 times and by each execution of the outer loop the inner loop is executed 5 times.
    So, count = 3 (outer loop) + 5 x 3 (inner loop) = 18.

The correct answer is:
1. 2.
2. 6.
3. 18.

If you found this difficult, you might consider reading my book Java quizmaster for beginners. It is easy to understand, organized to learn Java in 17 days and it guides you to master Java code by solving 105 quizzes and 117 assignments. This book explains each chapter properly before starting with exercises and assignments. It is available on Amazon. See inside the book here!

Please, leave your questions, feedback and suggestions in the comments below!
SarMaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.




Sar Maroof is graduated from HBO Amsterdam "higher professional education". He is a professional computer programming teacher, gives master classes and publishes technical articles. His experience with developing software started from 2001 and he has worked as a software developer for several big as well as small companies and later as a freelancer. He is the author of Java quizmaster and Build a Java application in 7 days.

Books by Sar Maroof

           

 Subscribe to Sar Maroof's RSS feeds!
 Mail this page to a friend!
 Bookmark this page!

Leave a Reply

Your email address will not be published. Required fields are marked *

Sarmaroof © 2017