Analyse 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 execute
for(int m=5 ; m<=20 ;m+=5)
{
if (m%3= = 0)
break;
else
if (m%5= = 0) System.out.println(m) continue;
}
Please give me a perfect answer with understandable steps
Answers
Hey Mate!
Here's the solution for your question:
In the first turn, m = 5, which is lesser than 20. So, the condition returns true and hence the loop is executed.
In the 1st iteration of the loop, the if condition returns false, since m % 3, i.e. 5 % 3 ≠ 0. Hence, the false block statements are executed. In the false block, the if condition returns true since 5 % 5 is equal to 0. So, m i.e. 5 is displayed and the loop is continued.
In the second turn, m is still <20, so the 2nd iteration takes place.
In the 2nd iteration, the if condition returns true, since 6 % 3 = 0. So the true block statement gets executed. In the true block, the break statement terminates the loop and hence the control moves out from the loop without further execution.
So, the answers to the questions are as follows:
i ) 5
ii ) 2 times
Hope it Helps!
In the first turn, m is 5, which is lesser than 20. So, the condition returns true and hence the loop is executed.
In the 1st iteration of the loop, the if condition returns false, since m % 3, i.e. 5 % 3 ≠ 0. Hence, the false block statements are executed. In the false block, the if condition returns true since 5 % 5, is equal to 0. So, m i.e. 5 is displayed and the loop is continued.
In the 2nd iteration, the if condition again returns false, since 10 % 3 ≠ 0. So the false block statement gets executed. In the false block, the if condition returns true as 10 % 5 = 0. So 10 is displayed.
In the third iteration, the if condition returns true, since 15 % 3 is equal to 0. So, the true block statement is executed. In the true block, the break statement terminates the loop and hence the control moves out from the loop without further execution.
i ) 5
10
ii ) 3 times
i) The output of the given code segment is 5 10.
Iteration 1 Execution :
- The initial value of m is 5.
- The first condition checks the remainder on division with 3. (5%3=2). Thus, the if condition is not satisfied, so move to else if.
- The number 5 is divisible by 5, so "5 is printed in the output console".
- The value of m updates as m = m+5 = 5+5 = 10
Iteration 2 Execution :
- The value of m in this iteration is updated to 10.
- The first "if" condition is not satisfied (10%3=1). So move to else if.
- The number 10 is divisible by 5 i.e. else condition satisfied. So,"10 is printed in the output console".
- The value of m updates as m = m+5 = 10+5 = 15
Iteration 3 Execution :
- The value of m in this iteration is updated to 15.
- The first "if" condition is satisfied (15%3=0) as 15 is divisible by 3.
- So, the break statement inside the "if" block is executed and the control breaks out of the loop and no further iterations are made.
ii) The body of the loop runs 3 times.
- In iteration 1 with m = 5, the else part is executed of the loop
- In iteration 2 with m = 10, the else part is executed of the loop
- In iteration 3 with m = 15, the if part is executed of the loop
- After this, the break statement executes and the control goes out of the loop
- Hence, the number of times the loop body executes is the number of iterations i.e.3.
Hence, the output of the given snippet is "5 10" and the loop body is executed 3 times.
To learn more about Code Output, visit
https://brainly.in/question/642691
#SPJ6