c++ - difference between a cout and 2 couts -


as title says, what's difference between cout , 2 couts?

#include <iostream> using namespace std; int main() {     for(int i=0; != 6; = + 2)         cout<<"&"<<"h"; } 

this produce output : &h&h&h

but if change above cout

cout<<"&";cout<<"h"; 

the output changes completely, shows : &&&h instead. how work actually?

the solution here bracket loop such both statments cout<<"&"; , cout<<"h" in loop. way loop written first 1 included.

as written code becomes this.

#include <iostream> using namespace std; int main() {     for(int i=0; != 6; = + 2)         cout<<"&";     cout<<"h"; // <---this line outside loop } 

this happens because first line following loop statement scoped loop default. output here print & 3 times, loop terminate , h print.

corrected version:

#include <iostream> using namespace std; int main() {     for(int i=0; != 6; = + 2){         cout<<"&";cout<<"h";     } } 

the output here print &h 3 times , terminate loop.


Comments