# c语言 循环嵌套循环_C编程语言中的嵌套循环

c语言 循环嵌套循环

### 循环嵌套 (Nesting of Loops)

A loop inside another loop is called nesting of loops. There can be any number of loops inside one another with any of the three combinations depending on the complexity of the given problem. Let us have a look at the different combinations.

#### 1. for循环的嵌套 (1. Nesting of for loop)

Syntax:

    for (initialize ; condition ; increment)
{
Statement(s);
for (initialize ; condition ; increment)
{
Statement(s);
...;
}
...;
}



Example: Print number 1 to 10, 5 times

    1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10



C code

C代码

#include <stdio.h>

int main()
{
int i; //for outer loop counter
int j; //for inner loop counter

for( i=1; i<=5; i++)
{
for( j=1; j<=10; j++)
{
printf("%d ",j);
}
printf("\n");
}

return 0;
}



#### 2. while循环的嵌套 (2. Nesting of while loop)

These loops are mostly used for making various pattern programs in C like number patterns or shape patterns, etc.

Syntax:

    while (condition 1)
{
Statement(s);
while (condition 2)
{
Statement(s);
...;
}
...;
}




Example 1: Print number 1 to 10, 5 times

    1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10



C code

C代码

#include <stdio.h>

int main()
{
int i; //for outer loop counter
int j; //for inner loop counter

i=1;
while( i<=5 )
{
j=1;
while( j<=10 )
{
printf("%d ",j);
j++;
}
printf("\n");
i++;
}

return 0;
}



Example 2: Printing the following pattern,

    1
1 2
1 2 3
1 2 3 4
1 2 3 4 5



C code

C代码

#include <stdio.h>

int main()
{
int i; //for outer loop counter
int j; //for inner loop counter

i=1;
while( i<=5 )
{
j=1;
while( j<=i )
{
printf("%d ",j);
j++;
}
printf("\n");
i++;
}

return 0;
}




#### 3. do ... while循环的嵌套 (3. Nesting of do...while loop)

Syntax:

    do
{
Statement(s);
do
{
Statement(s);
...;
} while (condition 2);
...;
} while (condition 1);



Example 1: Print number 1 to 10, 5 times

    1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10



C code

C代码

#include <stdio.h>

int main()
{
int i; //for outer loop counter
int j; //for inner loop counter

i=1;
do
{
j=1;

do
{
printf("%d ",j);
j++;
}while( j<=10 );

printf("\n");
i++;

}while( i<=5 );

return 0;
}



Example 2: Printing the following pattern,

    1
1 2
1 2 3
1 2 3 4
1 2 3 4 5



C code

C代码

#include <stdio.h>

int main()
{
int i; //for outer loop counter
int j; //for inner loop counter

i=1;
do
{
j=1;

do
{
printf("%d ",j);
j++;
}while( j<=i );

printf("\n");
i++;
}while( i<=5 );

return 0;
}



Author's note:

These programs are tried and tested in LINUX GCC Compiler. For better understanding of code, try it yourself and run it. Don’t restrict yourself to only these conditions and try other problems from different books or internet.

I'm always there to help in case of problems. Happy coding :)

c语言 循环嵌套循环

• 4
点赞
• 8
收藏
觉得还不错? 一键收藏
• 0
评论
03-04 337
08-19 6752
11-09 417
05-18 1278
10-28 250
01-04 981
02-05 2327
09-07 1153
07-08 1万+

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。