Computer Applications
Give the output of the following program segment and mention how many times the loop will execute:
int k;
for ( k = 5 ; k < = 20 ; k + = 7 )
if ( k% 6==0 )
continue;
System.out.println(k);
Java
Java Iterative Stmts
92 Likes
Answer
Output of the program is 26 and the loop executes 3 times.
As there are no curly braces after the for
loop so only the statement immediately following the loop that is the if
statement is part of the for
loop. Again if
has no curly braces so just the continue
statement is part of if. The statement System.out.println(k)
is outside the loop. The below table shows each iteration of for loop in detail:
k | Remarks |
---|---|
5 | 1st Iteration |
12 | 2nd Iteration |
19 | 3rd Iteration |
26 | As the loop condition k<=20 becomes false, the loop stops iterating after 3 iterations. |
Answered By
35 Likes