Problem Statement - for loop output and execution count
Analyze the given program segment and answer the following questions :
(i) Write the output of the program segment
(ii) 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;
}
Solution
TC++ #4252
Analysis:
for(int m=5; m<=20; m+=5)
{
if(m%3 == 0)
break;
else
if(m%5 == 0)
System.out.println(m);
continue;
}
The loop entry will happen with m=5
m%3 == 0 will be false so it will go to else
m%5==0 will be true so m will be print as 5 and a newline will come.
loop will go to next increment section as continue is there
m+=5 will make m=10
m%3 == 0 will be false so it will go to else
m%5==0 will be true so m will be print as 10 and a newline will come.
loop will go to next increment section as continue is there
m+=5 will make m=15
m%3 == 0 will be true now so loop will break. But this is 3rd execution of loop