Any book on C++, will have a chapter on looping.
But generally to give you a basic understanding of for loops and nested for loops:-
The for loop has 3 parts:
1) Declare and initialize a variable : Ex: int i = 0;
2) Test the variable : For any condition you want. Ex: i< 4
3) Perform some operation: Usually the variable is incremented or decremented. Ex: i++;
1)The for loop (not nested) can be used to for displaying values, testing an array of values etc.
Ex :
int main()
{
int a[4] = {1, 2, 3,4 };
for (int i=0; i < 4; i++)
{
cout << a[i] << endl;
}
}
output:
1
2
3
4
2) But when you are dealing with 2 - d array, That is, which consists of both row and column, the nested for loop can be used.
The example you have illustrated itself is a very good example. But still, To print 5 rows and 6 columns of "*", the for loop would be:
main()
{
int row,col;
for (row=1; row<=5; row++)
{
for (col=1; col<=6; col++)
{
printf ("*", col);
}
printf ("\n");
}
getch();
output:
******
******
******
******
******
Here, the variable "row" in the outer loop is initialized to 0
row = 1 This is Row 1
Then the loop goes into inner loop, where the loop is run until the condition col<=6 is reached -
col = 1;
print *
col is incremented and checked if it is less than /equal to 6,
if yes print *
This is done until col = 6, thus displaying 6 columns. Then the loop moves to the outer loop again, increments row, checks if the value is less than 5 and the process continues until row = 5. Then stops.
Try this same example with different values for row and column and run your program in debug. You will understand what is going on exactly.