Different results in for loops.
I was wondering why the following two pieces of code give different results. The first one prints "theChar" a certain amount of times in a vertical line while the second creates a matrix.
The difference is the brackets. But why does it execute the way it does?
Code:
#include <iostream>
int main()
{
using namespace std;
int rows, columns;
char theChar;
cout << "How many rows? ";
cin >> rows;
cout << "How many columns? ";
cin >> columns;
cout << "What character do you prefer? ";
cin >> theChar;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
cout << theChar;
cout << endl;
}
}
return 0;
}
Code:
#include <iostream>
int main()
{
using namespace std;
int rows, columns;
char theChar;
cout << "How many rows? ";
cin >> rows;
cout << "How many columns? ";
cin >> columns;
cout << "What character do you prefer? ";
cin >> theChar;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
cout << theChar;
cout << endl;
}
return 0;
}