Java Programming - Operators and Assignments - Discussion
Discussion Forum : Operators and Assignments - Finding the output (Q.No. 8)
8.
What will be the output of the program?
class Test
{
public static void main(String [] args)
{
int x= 0;
int y= 0;
for (int z = 0; z < 5; z++)
{
if (( ++x > 2 ) || (++y > 2))
{
x++;
}
}
System.out.println(x + " " + y);
}
}
Answer: Option
Explanation:
The first two iterations of the for loop both x and y are incremented. On the third iteration x is incremented, and for the first time becomes greater than 2. The short circuit or operator || keeps y from ever being incremented again and x is incremented twice on each of the last three iterations.
Discussion:
20 comments Page 2 of 2.
AKASH said:
1 decade ago
Just explain output of this type of code through steps.
Arham said:
3 years ago
Thank you for explaining @Arun.
Jayaram.... said:
1 decade ago
Hi Thanks, Mr.Maheshthakuri
Susanta pradhan said:
1 decade ago
Please explain in for loop.
Purushottam said:
9 years ago
Thanks @Maheshthakuri.
Malathi said:
8 years ago
Thanks @Maheshthakuri.
Neha said:
7 years ago
Thanks @Maheshthakuri.
Mayur Patil said:
6 years ago
Thanks @Maheshthakuri.
Sanjay kumar said:
5 years ago
Thanks @Maheshthakuri.
Priyanka said:
8 years ago
Thank You @Arun.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers