KnowledgeBoat Logo

Output Questions for Class 10 ICSE Computer Applications

Give the output of following code and mention how many times the loop will execute?


int i;
for( i=5; i>=1; i--)
{
    if(i%2 == 1)
        continue;
    System.out.print(i+" ");
}

Java

Java Iterative Stmts

ICSE

48 Likes

Answer

Output of the above code is:

4 2

Loop executes 5 times. The below table shows the dry run of the loop:

iOutputRemark
51st Iteration — As i % 2 gives 1 so condition is true and continue statement is executed. Nothing is printed in this iteration.
442nd Iteration — i % 2 is 0 so condition is false. Print statement prints 4 (current value of i) in this iteration.
343rd Iteration — i % 2 is 1 so continue statement is executed and nothing is printed in this iteration.
24 24th Iteration — i % 2 is 0. Print statement prints 2 (current value of i) in this iteration.
14 25th Iteration — i % 2 is 1 so continue statement is executed and nothing is printed in this iteration.
04 2As condition of loop (i >= 1) becomes false so loops exits and 6th iteration does not happen.

Answered By

21 Likes