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
To 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++)
Which of the following are entry controlled loops?
(a) for
(b) while
(c) do..while
(d) switch
- only a
- a and b
- a and c
- c and d
Define a class to accept a number from user and check if it is an EvenPal number or not.
(The number is said to be EvenPal number when number is palindrome number (a number is palindrome if it is equal to its reverse) and sum of its digits is an even number.)
Example: 121 – is a palindrome number
Sum of the digits – 1+2+1 = 4 which is an even number