22. What will be the output of the
following program
int a, b=4;
for(a=6; a<=24; at=6)
{
If(a%b==0)
break;
}} System.out.println(a);
Answers
Answered by
7
Required Answer:-
Correct Code:
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.
•••♪
Attachments:
Similar questions