Give and explain the output of the following? int main () { int i, n = 4; for (i = 0; i++<n; i++) cout<<i<<"i"; } | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Give and explain the output of the following? int main () { int i, n = 4; for (i = 0; i++<n; i++) cout<<i<<"i"; }

20th Jun 2016, 3:12 PM
Rashad
Rashad - avatar
9 Answers
+ 5
i is set to zero in the beginning , the condition is checked first (i++<n) and while checking the codition i is "incremented" by 1.. so now the value of i is "1"...the loop runs for the first time,and 1i is displayed in the console and i is incremented again after the end of loop..now i="2",loop runs again and "i" gets incremented again while checking for the condition (i++<n) ,now i="3" and 3i is displayed the console and as the loop ends ,"i" gets incremented again but now i is 4 which means i++ should be 5..and 5 is not less than 4 so loop will not run again..program ends here after returning zero
20th Jun 2016, 6:22 PM
Kapil
+ 2
1i3i You can try to run the code in the Code Playground and see how it works :)
20th Jun 2016, 3:49 PM
James Flanders
+ 2
n=4 i i <=n i++(i=i+1) 0 true 1 1 true 2 2 true 3 3 true 4 4 true 5 false It prints value of i on the screen till the condition is satisfied i.e. 0 1 2 3 4
28th Jun 2016, 10:29 AM
Dhaval Rathod
Dhaval Rathod - avatar
0
I didn't get u
20th Jun 2016, 3:17 PM
Rashad
Rashad - avatar
- 1
"first u learn english"@sukhvinder
20th Jun 2016, 4:38 PM
Rashad
Rashad - avatar
- 1
the output will be 1 i 3 i
21st Jun 2016, 8:59 AM
Tushar Chetan
Tushar Chetan - avatar
- 2
Помогите, гайд по c++
20th Jun 2016, 4:44 PM
Bokmari Game
Bokmari Game - avatar
- 2
it is i
22nd Jun 2016, 6:55 AM
sophie
sophie - avatar
- 5
I am not understand
20th Jun 2016, 4:34 PM
sukhvinder
sukhvinder - avatar