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;
}
2 1
3 1
4 1
5 1
6 1
7 0
2 1
3 1
4 1
5 1
6 1
2 1
3 1
4 1
5 1
2 2
3 3
4 4
5 5
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
46 comments Page 1 of 5.

Manoj wagh said:   2 years ago
#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;
}
ChatGPT

This C program uses a for loop to iterate through a sequence of values for the variables x and y. Let's break down the code step by step:

Initialization: int x=1, y=1; initializes two integer variables, x, and y, both with a value of 1.

for loop header: for(; y; printf("%d %d\n", x, y)) is a for loop that doesn't have an explicit initialization part (for(;;)). It relies on the initialization done before the loop.

Loop condition: y is the loop condition. The loop will continue as long as y is non-zero.

Loop body: printf("%d %d\n", x, y) prints the current values of x and y to the console.

Inside the loop: y = x++ <= 5; is the statement that updates the value of y in each iteration. It uses the post-increment operator x++, and it sets y to the result of the comparison x <= 5. The loop will continue as long as y is non-zero.

The loop continues until y becomes zero.

After the loop: printf("\n"); prints a newline character, creating a line break.

The program returns 0, indicating successful execution.

Now, let's trace the execution for the first few iterations:

Iteration 1: x=1, y=1 (prints "1 1")
Iteration 2: x=2, y=1 (prints "2 1")
Iteration 3: x=3, y=1 (prints "3 1")
Iteration 4: x=4, y=1 (prints "4 1")
Iteration 5: x=5, y=1 (prints "5 1")
Iteration 6: x=6, y=0 (loop exits as y becomes 0)

So, the output of this program will be:

1 1
2 1
3 1
4 1
5 1
(6)

Abhishek Mahajan said:   1 decade ago
Look when inside the loop the expression (y = x++ <= 5) gets evaluated, then it is the value of x not the incremented value gets compared. Post increment operator increases the value after the variable's use.

SO the things will proceed like this:

1] y = x++ <= 5.
x=1 will get compared and after comparison.
y=1, x=2.
then print statement is executed for x=2 and y=1 (explained above).

2] x=2 will get compared and after comparison.
y=1, x=3.
then print statement is executed for x=3 and y=1 (explained above).

3] x=3 will get compared and after comparison.
y=1, x=4.
then print statement is executed for x=4 and y=1 (explained above).

4] x=4 will get compared and after comparison.
y=1, x=5.
then print statement is executed for x=5 and y=1 (explained above).

5] x=5 will get compared and after comparison.
y=1, x=6.
then print statement is executed for x=6 and y=1 (explained above).

6] x=6 will get compared and after comparison.
y=0, x=7.
then print statement is executed for x=7 and y=0.

Now after 6th step y=0, therefore the loop execution condition failed and loop will be exited.
(1)

Laxman said:   1 decade ago
Initialisation alredy done by y=1.... enters the loop y=x++<=5.increments x to 2 but treats x as 1 in this expression and stores x=2 in x memory location of x ..y=1<=5 true so return 1 and y value is i now repets the loop x and y values are printed as 2 1 again enteres the loop increments x to 3 but value of x ois treated as 2in this expression..y=2<=5 true..y=1 x=3 this repets till y=1 and x=5 are printed. again it enters the body of loop as condition y=1 satisfied...then y=x++<5 here the x is incremented to 6 but as its post incremented , for the expression y=x++<=5 it is treated as 5 and conditon(5<=5) true so y =1 but the value of x in memory changes to 6 .now again repeatin the loop.....(;y;printf("%d %d",x,y))..y=1 i.e condition true so value of x,y=6,1 is printed, so again enterin body of the loop..now x value is 6 and in y=x++<=5 x will be incremented to 7 in its memory location but treated as 6 in this expression ...as y=6<=5 false returns 0 so y=0... again repeatin the loop...first the value of x and y are printed as 0 7 ..then condition is cheked...y=0 loop terminates.

Rohan said:   7 years ago
int x=1, y=1;

Step 1: initialize the variable x and y to the value 1...

for(; y; printf("%d %d\n", x, y))

Step 2: (i) ; -> it means x=1 [Initialization]
(ii) y; -> condition y=1..
(iii) printf("%d %d\n", x, y) -> prints the value of x and y

y = x++ <= 5;

Step 3:
(i) First time -> y = 1 <=5 , condition true
y = 1 , x = 1+1 =2

(ii) Second time -> y = 2<= 5 , condition true
y =1 , x = 2+1 =3

(iii) Fifth time -> y = 5 < = 5, condition true
y = 1 , x= 5+1 =6


Till now
2 1
3 1
4 1
5 1
6 1


(iv) Last time -> y = 6 <=5 ,condition false
y = 0 , x= 6+1 = 7

Output:-
2 1
3 1
4 1
5 1
6 1
7 0
(27)

Kiran said:   1 decade ago
Here, x and y are inialized to 1,
In for loop, the condition statement is y = 1, it is true.
The next statements in for will executes.
y = x++ <= 5, here x++ will take the value 1 and (1 <= 5) is true x++ will becomes 2 1.e., x = 2 and y = 1 .
Then the incremented statement prints, x = 2 y = 1 it till upto x = 5 and y= 1

Here y = x++ <=5, evaluates to x = 6 (5 <= 5 it is true and x increment to 1) and
y = 1

Here 6 < = 5 is false, then y = 0, and x increments to 1 i.e., x = 7,
Then it prints x = 7 and y = 0.
Then Checks condition y = 0 which is fails, it comes out from the loop
The result is
2 1
3 1
4 1
5 1
6 1
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);
}

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.

Ravindar Nath Mishra said:   2 decades ago
Here the condition for the loop is y i.e until the y is true and in c true is assigned as 1 while false as 0 and x++ is post increment that is incremented after being used.

First Iteration x=1 y=(1<=5) which is true therefore y=1 and since x is post increment hence it is printed as 2,thus, x=2 and y=1 gets printed similarly for x=2,x=3,x=4.

On x=5 y=(5<=5) again y is true therefore y=1 and x=6 gets printed . BUT for x=6 y=(6<=5;which is false) so y=0 hence x=7 and y=0 get displayed. Since y=0 which is false thus loop gets terminated.

Panchanan rauta said:   1 decade ago
Here the execution as like below

step-1 y=x++<=5; here x is post increment '1' is compare<=5 true y=1
then x increment print x=2 y=1

step2 2<=5 x become 3 and y=1 print x=3 y=1

step3 3<=5 x become 4 and y=1 print x=4 y=1

step4 4<=5 x become 5 and y=1 print x=5 y=1

step5 5<=5 x become 6 and y=1 print x=6 y=1

step6 6<=5 x become 7 and y=0 print x=7 y=0

step7 it check y become false loop will terminate

Hence the result is
2 1
3 1
4 1
5 1
6 1
7 0

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");
}


Post your comments here:

Your comments will be displayed after verification.