Nested use of a For loop
Nested syntax for the FOR loop:
for (int i =0;i<10;i++) {
for (int j=0;j<5;j++) {
}
}
Usually used to print a plane, or a control plane, or a printed form.
Summary: Outer loop control number of rows, inner loop control number of columns
Tips: Code examples are as follows
#include <stdio.h>
int main (int argc, const char * argv[]) {
/*
* * * * *
* * * *
* * *
* *
*
Outer Loop Loop 5 times (control line) inner loop total number of rows-row number (control column)
*/
/*
for (int i=0; i<5; i++) {
for (int j=0; j<5-i; J + +) {
printf ("*");
}
Finish line, print line break
printf ("\ n");
}
*/
/*
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
Requires two loops, one to control the number of columns above 5 rows (number of columns = line number), one to control the number of columns below 4 rows (number of columns = Total rows-line number)
*/
/*
for (int i =0; i<9; i++) {
if (i<5) {//This is the case of the previous 5 rows
for (int j=0; j<=i; J + +) {
printf ("*");
}
}else{//4 rows in the following case
for (int k=0; k<9-i; k++) {
printf ("*");
}
}
printf ("\ n");
}
*/
/*
1
2 2
3 3 3
4 4 4 4
5 5 5) 5 5
Outer loop Control 5 times (control line) Inner loop control line number (control column)
*/
/*
for (int i=0;i<5;i++) {
for (int j=0; j<=i; J + +) {
printf ("%d", i+1);
}
printf ("\ n");
}
*/
/*
1
1 2
1 2 3
1 2 3 4
1 2 3) 4 5
Outer loop Control 5 times (control line) Inner loop control line number (control column)
The printed content is column number +1
*/
for (int i=0; i<5; i++) {
for (int j=0; j<=i; J + +) {
printf ("%d", j+1);
}
printf ("\ n");
}
return 0;
}
C-language loop structure for (ii)