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
1 Resposta
+ 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