QuesAns Print Page – CSKC
Sign In

Search

siteicon
Q&A ICSE2017-02D #7226

Question:

Give the output of the following program segment and also mention the number of times the loop is executed:
int a,b;
for (a = 6, b = 4; a <= 24; a = a + 6)
  {
    if (a%b ==0)
    break;
  }
System.out.println(a);

Answer:

Loop initialization: a = 6, b = 4
First iteration:
Condition check: a <= 24 --- 6 <= 24 ---- true Loop is executed for the first time Loop execution: a % b = 6 % 4 = 2 != 0 Hence, break is not executed Loop increment operator: a = 1 + 6 --- a = 6 + 6 = 12 Second iteration: Condition check: a <= 24 --- 12 <= 24 ---- true Loop is executed for the second time Loop execution: a % b = 12 % 4 = 0 = 0 Hence, break is executed System.out.println(a); --- 12 Output is 12 and loop is executed two times