Why there are different output for the following two same program in C++

arpitsri

Honorable
Dec 2, 2013
6
0
10,510
0
I mean that the following codes should have the same output, but a change of line is giving a whole different output. I'm using Turbo C++ compiler, I know that these codes are outdated but in my exams only these are going to come.

Code 1
#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
int i,j;
for(i=1;i<=5;i++)
{for(j=1;j<i;j++)
cout<<"*";
cout<<"\n";
}
getch();}

and this one-

Code-2
#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
int i,j;
for(i=1;i<=5;i++)
{for(j=1;j<i;j++)
cout<<"*"<<"\n";
}
getch();}


Output 1-
*
**
***
****

Output 2-
*
*
*
*
*
*
*
*
*
*

 

Cons29

Distinguished
Feb 10, 2010
342
0
19,010
25
they are not the same

CODE1, the inner for loop will only print the *, i am not 100% sure here but since you dont have { } in the inner for loop, only the line before it will be executed (the * print), the "\n" will execute after the inner loop is done

code2, again no { } in the inner for loop, so it will only execute 1 line after it, but this time, the print is * together with the newline (\n), hence after printin 1 *, new line will be printed after it.


for(i=1;i<=5;i++)
{for(j=1;j<i;j++)
cout<<"*"; <--- this will execute for the inner loop
cout<<"\n"; <--- this will execute for the outer loop (using the i variable), so only a few "new line"
}

for(i=1;i<=5;i++)
{for(j=1;j<i;j++)
cout<<"*"<<"\n"; <---- * + newLine will execute, so just 1 * per line
}

hope its clear somehow
 

Cons29

Distinguished
Feb 10, 2010
342
0
19,010
25
they are not the same

CODE1, the inner for loop will only print the *, i am not 100% sure here but since you dont have { } in the inner for loop, only the line before it will be executed (the * print), the "\n" will execute after the inner loop is done

code2, again no { } in the inner for loop, so it will only execute 1 line after it, but this time, the print is * together with the newline (\n), hence after printin 1 *, new line will be printed after it.


for(i=1;i<=5;i++)
{for(j=1;j<i;j++)
cout<<"*"; <--- this will execute for the inner loop
cout<<"\n"; <--- this will execute for the outer loop (using the i variable), so only a few "new line"
}

for(i=1;i<=5;i++)
{for(j=1;j<i;j++)
cout<<"*"<<"\n"; <---- * + newLine will execute, so just 1 * per line
}

hope its clear somehow
 

Ijack

Distinguished
You should be 100% sure as your explanation is correct. A C++ "for" statement is followed by either a single statement or by a number of statements grouped with {}. in each of the OP's programs only the statement immediately following the inner "for" is part of the loop.
 
Thread starter Similar threads Forum Replies Date
P Apps General Discussion 2
A Apps General Discussion 5
clintox Apps General Discussion 3
T Apps General Discussion 1
R Apps General Discussion 2
O Apps General Discussion 1
I Apps General Discussion 2
Jmurph01 Apps General Discussion 1
Zaporro Apps General Discussion 1
K Apps General Discussion 4
K Apps General Discussion 1
Q Apps General Discussion 2
G Apps General Discussion 2
S Apps General Discussion 1
shattered space Apps General Discussion 6
U Apps General Discussion 9
D Apps General Discussion 1
jefe323 Apps General Discussion 10
B Apps General Discussion 4
L Apps General Discussion 14

ASK THE COMMUNITY