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 + " ");
}
Answer
1 3 5 7 9
Working
- 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 + " ");.
Related Questions
Evaluate the given expression when x = 4.
x *= --x + x-- + x;Convert the following switch case into if else if:
switch(x) { case 'T' : case 't' : System.out.print("Teacher"); break; default : System.out.print("Student"); }In the example given below of class Cat, identify the variable and the methods:
Cat Name meow() eat() play() Give the output of the following program segment and mention how many times the loop is executed.
int k = 100; while (k>=10) { System.out.println(k); k-=40; }