+ 3

Hey friends please explain me this code and say why its output is 1120?

#include <iostream> using namespace std; int main() { int x=1,y=2; for(x<=4;y--;x++) cout<<x<<y; return 0; } //output is 1120

28th Aug 2018, 8:35 PM
Albert Whittaker :Shaken To Code
Albert Whittaker :Shaken To Code - avatar
4 odpowiedzi
+ 3
The loop ends when y = 0 (because 0 is considered false) 1. y is decremented, prints x which is 1 followed by y which is also 1, increments x to 2 2. decrements y (the loop does not stop imediatly because first the value of y is used then it is decremented) prints x which is 2 followed by y which is 0, increments x; 3. loop ends;
28th Aug 2018, 9:04 PM
Ulisses Cruz
Ulisses Cruz - avatar
+ 2
bcz everything y is decrement ed to 1 and x is increment to 1
29th Aug 2018, 7:52 AM
Aarav Raj
Aarav Raj - avatar
0
we are checking condition only for x, so value of y won't affect the loop. but how it will yields 1120 output???
29th Aug 2018, 2:50 AM
Chaitra R
Chaitra R - avatar
0
can any 1 please explain the above code, still I m not getting
3rd Sep 2018, 12:26 PM
Chaitra R
Chaitra R - avatar