2(iv)
Write the output of the following program segment:
for(int a = 1; a <= 10; a++)
{
if(a % 2 == 0)
continue;
System.out.print(a + " ");
}Show SolutionHide Solution↓
1 3 5 7 9
- The loop control variable
ais initialized to 1 and is incremented by 1 after each iteration until it attains the value 10. - The condition
if (a % 2 == 0)tests whetherais an even number. If the condition is true, thecontinuestatement transfers control to the next iteration of the loop, thereby skipping the print statement. - Consequently, only the odd values 1, 3, 5, 7, and 9 are displayed by the statement
System.out.print(a + " ");.