Answer this question correctly to get 25 points and extra points later as a reward.
Give the output of the following program segment. Also mention the number of times
the loop is executed:
int a,b=4;
for(a=6,a<=24;a=a+3)
{
if(a%b==0)
break;
}
System.out.println(a);
Answers
Output is 12. Twice.
In the loop : for (a = 6, b=4; a< =24; a = a+6), the value of a will be incrementing as 6, 24 and upon incrementing the value to 42, the loop will terminate.
Accordingly the loop has to execute two times.
But within the loop there is a condition : if(a%b = =0) break;
This means when remainder on dividing a by b comes out to be 0 (at 24/4 = 0), the condition breaks and from the above it is clear that value of a is incremented from 6 to 24 when the loop executes second time.
Output is 12. Twice.
In the loop : for (a = 6, b=4; a< =24; a = a+6), the value of a will be incrementing as 6, 24 and upon incrementing the value to 42, the loop will terminate.
Accordingly the loop has to execute two times.
But within the loop there is a condition : if(a%b = =0) break;
This means when remainder on dividing a by b comes out to be 0 (at 24/4 = 0), the condition breaks and from the above it is clear that value of a is incremented from 6 to 24 when the loop executes second time.