C Programming - Control Instructions - Discussion
Discussion Forum : Control Instructions - Find Output of Program (Q.No. 13)
13.
What will be the output of the program?
#include<stdio.h>
int main()
{
int x=1, y=1;
for(; y; printf("%d %d\n", x, y))
{
y = x++ <= 5;
}
printf("\n");
return 0;
}
Discussion:
46 comments Page 2 of 5.
Lakshmi said:
1 decade ago
Thanks to panchan Raut
Sai said:
1 decade ago
Thank you Kiran.
6 < = 5 is false, then why = 0, and x increments to 1 i.e., x = 7,
In this you are saying that when it false it will take y=0 but already the why is initialized with 1 how it will take ?
6 < = 5 is false, then why = 0, and x increments to 1 i.e., x = 7,
In this you are saying that when it false it will take y=0 but already the why is initialized with 1 how it will take ?
Piyush jain said:
1 decade ago
The For statement is something like this
For(i=0 ; i<10 : printf("\n"))
{
....
}
Which is Equivalent to
i=0;
while(1)
{
if(i<10)
{
blah blah blah
}
Else break;
printf("\n");
}
For(i=0 ; i<10 : printf("\n"))
{
....
}
Which is Equivalent to
i=0;
while(1)
{
if(i<10)
{
blah blah blah
}
Else break;
printf("\n");
}
Manju said:
1 decade ago
y=x++<=5; is the statement, when x evaluates to b 2 why x value is not assigned to y ? why y is still 1?
Gauravkumar said:
1 decade ago
@Yash.
Execution of loop takes this form.
Step 1 : Initialization.
Step 2 : Condition checking.
Step 3 : Execute body of loop.
Step 4 : Increment.
Execution of loop takes this form.
Step 1 : Initialization.
Step 2 : Condition checking.
Step 3 : Execute body of loop.
Step 4 : Increment.
Ashish said:
1 decade ago
If 6<=5 the condition will become false then how it will print last one i.e. 7 0.
Krish said:
1 decade ago
I will explain this in easy manner.
We have [y=(x++<=5)] if u observe carefully it consists of
2 valuable expression with in this.
Simple funda is: T T T(1)
F F F(0)
If x<=5 is (true) then expression (x++<=5) is always (true) , that means it have the value 1 , and y will be true
always(1).
If x>=5 is (false) then expression (x++>=5) is always (false) ,that means it have the value 0 , and y will be false always(0).
That is why last step will be [7 0].
The above program can be write like this in while loop:
while(y)
{
y=x++<=5;
pf("%d %d"x,y);
}
We have [y=(x++<=5)] if u observe carefully it consists of
2 valuable expression with in this.
Simple funda is: T T T(1)
F F F(0)
If x<=5 is (true) then expression (x++<=5) is always (true) , that means it have the value 1 , and y will be true
always(1).
If x>=5 is (false) then expression (x++>=5) is always (false) ,that means it have the value 0 , and y will be false always(0).
That is why last step will be [7 0].
The above program can be write like this in while loop:
while(y)
{
y=x++<=5;
pf("%d %d"x,y);
}
Ibu said:
1 decade ago
But why the "y" value did not change? In y=x++<=5; after the first incrementation y=2++<=5. Why shouldn't the incremented value of x=2 is assigned in "y"? can anyone explain?
Jitender Chhirang said:
1 decade ago
Because here y takes the value true or false if true than it get prints 1 otherwise 0 you can change the value of y in the program and check that value of y doesn't matter it will only work on true or false of x++<=5 condition it will print 1 until this condition becomes true and value of x is incremented in every step after the use of x and the incremented value is stored at the position of previous value of x and that's why it take the value from that address and prints the value but it doesn't take value of y from that position because it only takes the answer of true false condition.
Sonam said:
1 decade ago
Why is the value of x not assigned in y?
coz, y=x++<=5.......????
what should have been the syntax to assign the value of x to y??
coz, y=x++<=5.......????
what should have been the syntax to assign the value of x to y??
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers