How can be the output of this code be 234? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
0

How can be the output of this code be 234?

#include <iostream> using namespace std; int main() { int i=2; for(;i<=3;i++) cout<<i; //here the value of i becomes 3 and prints till 3 for(;i<5;i++)//here it should print 3 again as the condition is true. then 4. and should exit. cout<<i; return 0; } //output comes 234 why? while the output should be 2334? sorry if i am wrong m new to it.. just wamma know ! thx

18th Sep 2017, 3:12 AM
Ishu Maurya
Ishu Maurya - avatar
1 Antwort
+ 1
for(;i<=3;i++) cout<<i; //here the value of i becomes 3 and prints till 3 your range is [2,3] when "i" becomes 3 the loop condition is still true hence "i" will be incremented to 4 When it reaches the second loop, i is 4 so after the first iteration, "i" increments and become 5 and loop condition becomes false, thus loop breaks you can check for it after printing "i" after the last loop, it will be 5
18th Sep 2017, 4:00 AM
Prince Gupta
Prince Gupta - avatar