Analyze the given program segment and answer the following questions:

  1. Write the output of the program segment.
  2. How many times does the body of the loop gets executed ?

for(int m = 5; m <= 20; m += 5)
{    if(m % 3 == 0)
        break;
    else
        if(m % 5 == 0)
            System.out.println(m);
    continue;
}

Views 10

Answer:


5
10

Loop executes 3 times.

Below dry run of the loop explains its working.

m Output Remarks
5 5 1st Iteration
10 5
10
2nd Iteration
15 5
10
3rd Iteration — As m % 3 becomes true, break statement exists the loop.

Related Articles:

This section is dedicated exclusively to Questions & Answers. For an in-depth exploration of Java Programming Language, click the links and dive deeper into this subject.

Join Our telegram group to ask Questions

Click below button to join our groups.