KnowledgeBoat Logo
|

Computer Applications

Write the output of the following program segment:

for(int a = 1; a <= 10; a++) 
{ 
if(a % 2 == 0) 
continue; 
System.out.print(a + "  "); 
}

Java

Java Iterative Stmts

5 Likes

Answer

1  3  5  7  9

Working

  1. The loop control variable a is initialized to 1 and is incremented by 1 after each iteration until it attains the value 10.
  2. The condition if (a % 2 == 0) tests whether a is an even number. If the condition is true, the continue statement transfers control to the next iteration of the loop, thereby skipping the print statement.
  3. Consequently, only the odd values 1, 3, 5, 7, and 9 are displayed by the statement System.out.print(a + " ");.

Answered By

2 Likes


Related Questions