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
130 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
51 Likes
Related Questions
How many times will the following loop execute? Write the output of the code:
int x=10; while (true){ System.out.println(x++ * 2); if(x%3==0) break; }Write a program in Java to find the Fibonacci series within a range entered by the user.
Sample Input:
Enter the minimum value: 10
Enter the maximum value: 20Sample Output:
13To execute a loop 10 times, which of the following is correct?
- for (int i=11;i<=30;i+=2)
- for (int i=11;i<=30;i+=3)
- for (int i=11;i<20;i++)
- for (int i=11;i<=21;i++)
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);