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
123 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
48 Likes
Related Questions
Give the output of the following program segment. How many times is the loop executed?
for(x=10; x>20;x++) System.out.println(x); System.out.println(x*2);Define a class to accept a number and check whether it is a SUPERSPY number or not. A number is called SUPERSPY if the sum of the digits equals the number of the digits.
Example1:
Input: 1021 output: SUPERSPY number [SUM OF THE DIGITS = 1+0+2+1 = 4, NUMBER OF DIGITS = 4 ]Example2:
Input: 125 output: Not an SUPERSPY number [1+2+5 is not equal to 3]