What will be the output of the following code?
int i=20;
if(i>10)
{ System.out.println( "The value of i is "+i);
i++;
if(i%2!=0)
break; }
Answers
Explanation:
We need to find the output of the given code.
Let's observe the code.
The first line of the code is
int i= 20;
Here in this line we have defined an integer i and assigned it to the value 20
Now, the value of i is 20.
if(i>20)
{
System.out.println ("The value of i is "+i")
i++
if(i%2 !=0)
break;
}
In this code, we enter the if block only when the value of i>20. The value of i is 10. So we enter the if block.
Then we have System.out.println(" The value of i is "+i")
This statement prints the value of i in the output.
Then the line i++ increments the value of i. Now the value of i will be i+1 that is 11.
Value of i is 11.
The line if(i%2 !=0)
if(i%2 !=0) break
We enter this if block only when i/2 doesn't gives us the remainder zero.
The value of i is 11. When we divide 11 by 2 we get remainder of 1. 1!=0. Hence, we enter the if block. If block contains break. Then we come out of the block.
Finally the output is
The value of i is 20
#SPJ2