1.

22. What will be the output of thefollowing programint a, b=4;for(a=6; a

Answer»

Required Answer:-

Correct Co‎de:

int a, b=4;

for(a=6;a<=24;a+=6)    {

  if(a%b==0)

    break;

}

System.out.println(a);

Output:

12

Explanation:

  • The given loop iterates in the range 6 to 24.
  • Here, initial value of a is 6. After each iteration, its value is INCREMENTED by 6.
  • So, when a is 6, a%b==0 is FALSE and so, the break statement will not be executed.
  • After the FIRST iteration, a becomes 12 i.e., its value is incremented by 6. So, a%b==0 becomes true as 12 is divisible by 4. So, the break statement is executed. Loop is terminated at this point.
  • Final value of a becomes 12 which is displayed on the screen.

See the ATTACHMENT for output.

•••♪



Discussion

No Comment Found