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.
Arun said:
8 years ago
Guys, When we use || logic operator, second condition will be executed only when the first condition is false.
AKASH said:
1 decade ago
Just explain output of this type of code through steps.
Ravi said:
9 years ago
How can you people understand this programs?
I completed BCA but I can't understand this programs.
For example I write small program here :
class Fact
{
public static void main(String [] args)
{
int n,c,fact=1;
S.o.p("enter a number to find fact");
Scanner in=new Scanner(System.in);
n=in.nextInt();
if(n<0)
s.o.p("enter number should be not a negative");
else
{
for(c=1;c<=n;c++)
fact=fact*c;
s.o.p('factorial of given number :"+fact);
}
}
}
Could anyone please explain this program briefly one by one line.
I completed BCA but I can't understand this programs.
For example I write small program here :
class Fact
{
public static void main(String [] args)
{
int n,c,fact=1;
S.o.p("enter a number to find fact");
Scanner in=new Scanner(System.in);
n=in.nextInt();
if(n<0)
s.o.p("enter number should be not a negative");
else
{
for(c=1;c<=n;c++)
fact=fact*c;
s.o.p('factorial of given number :"+fact);
}
}
}
Could anyone please explain this program briefly one by one line.
Purushottam said:
9 years ago
Thanks @Maheshthakuri.
Summa said:
1 decade ago
I thought "||" means "or" which means that, their can be both condition true, or one is true, the loop start to begin. In that case I think "D" is correct.
Manoj said:
1 decade ago
I too think option D is correct. Because the value of 'y' keep increasing.
Problem with option B: If value of y will incremented up to 2 then it will increment up to end.
Problem with option B: If value of y will incremented up to 2 then it will increment up to end.
MurAli said:
1 decade ago
1----> x--1, y--1 false.
2----> x--2, y--2 false.
3----> x--3, y--3 true.
x--4, y--3.
4---> x--5, y--4 true.
x--6, y--4.
5----> x--7, y--5 true.
x--8, y--5.
There fore (x, y) = (8, 5) option D is correct.
2----> x--2, y--2 false.
3----> x--3, y--3 true.
x--4, y--3.
4---> x--5, y--4 true.
x--6, y--4.
5----> x--7, y--5 true.
x--8, y--5.
There fore (x, y) = (8, 5) option D is correct.
Mohamed Jaleel Nazir said:
1 decade ago
for(int z=0;0<5;z++)->if((1>2)||(1>2))
for(int z=1;1<5;z++)->if((2>2)||(2>2))
for(int z=2;2<5;z++)->if((3>2)||(2>2))
// 'if' true then get into braces
x++; // x=4
for(int z=3;3<5;z++)->if((5>2)||(2>2))
// 'if' true then get into braces
x++; // x=6
for(int z=4;4<5;z++)->if((7>2)||(2>2))
// 'if' true then get into braces
x++; // x=8
for(int z=5;5<5;z++) -> condition false then get out of loop.
Now x=8.
Then x=8 y=2.
for(int z=1;1<5;z++)->if((2>2)||(2>2))
for(int z=2;2<5;z++)->if((3>2)||(2>2))
// 'if' true then get into braces
x++; // x=4
for(int z=3;3<5;z++)->if((5>2)||(2>2))
// 'if' true then get into braces
x++; // x=6
for(int z=4;4<5;z++)->if((7>2)||(2>2))
// 'if' true then get into braces
x++; // x=8
for(int z=5;5<5;z++) -> condition false then get out of loop.
Now x=8.
Then x=8 y=2.
Jayaram.... said:
1 decade ago
Hi Thanks, Mr.Maheshthakuri
Susanta pradhan said:
1 decade ago
Please explain in for loop.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers