+ 9

Can anyone explain the output ?

//~~ first code ~~ #include <iostream> using namespace std; int main() { int i=0; for(;i<=4;i++) i*=6; cout<<i; return 0; } //output 7 // ~~ second code ~~ #include <iostream> using namespace std; int main() { int i=0; for(;i<=4;i++); i*=6; cout<<i; return 0; } //output 30 //in the second code( i )will be 5 from the for loop and then multiply by 6 , so the output is 30 , am I right ?

6th Jun 2017, 2:54 PM
Enas Emad
Enas Emad - avatar
3 Answers
+ 3
You are correct. In the first code i starts off as 0 then (0 <= 4) => true 0 * 6 = 0 ++i, i becomes 1 (1 <= 4) => true 1 * 6 = 6 ++i, i becomes 7 (7 <= 4) => false Done
6th Jun 2017, 3:03 PM
Dennis
Dennis - avatar
+ 9
Ahh , Now it is clear ,thank you so much @Dennis.
6th Jun 2017, 3:11 PM
Enas Emad
Enas Emad - avatar
+ 7
here I was thinking that [i] would stop incrementing at 4. Edit: Clearly it does not. Adds another for good measure ;l https://code.sololearn.com/cp043va9GuW5/?ref=app
6th Jun 2017, 3:07 PM
jay
jay - avatar