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至10,共5次
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.
这些循环主要用于制作C语言中的各种图案程序,例如数字图案或形状图案等。
Syntax:
句法:
while (condition 1)
{
Statement(s);
while (condition 2)
{
Statement(s);
...;
}
...;
}
Example 1: Print number 1 to 10, 5 times
示例1:打印数字1至10,共5次
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,
示例2:打印以下图案
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:打印数字1至10,共5次
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,
示例2:打印以下图案
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.
这些程序已在LINUX GCC编译器中经过尝试和测试。 为了更好地理解代码,请自己尝试并运行它。 不要只局限于这些条件,也可以尝试其他书籍或网络上的其他问题。
I'm always there to help in case of problems. Happy coding :)
遇到问题时,我总是会为您提供帮助。 快乐的编码:)
c语言 循环嵌套循环