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.

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)

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)

Andy said:   6 years ago
What is ; for after the bracket, before y?

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

Kaira said:   6 years ago
Agree @Renuka!

Please, anyone, explain it.
(2)

Manoja V. said:   8 years ago
Thanks for the explanation @Kiran.
(1)

Vignesh said:   8 years ago
Thanks @Kiran.
(1)

Nandini said:   9 years ago
Thanks @Kiran.
(1)

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)

Krithik said:   10 years ago
Thanks everyone for giving the time to explain this.

Renuka said:   1 decade ago
Can anyone please explain what is "for(; y; printf("%d %d\n", x, y))" ?


Post your comments here:

Your comments will be displayed after verification.