for loop output and execution count – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Exam Questions-ICSE2016-03H #JAVA#4252    siteicon   siteicon  

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

i) So output would be
5
10

ii) Execution count would be
3

 


Share

sunmitra| Created: 31-Mar-2018 | Updated: 16-Apr-2019|ICSE2016









Back