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至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 :)

遇到问题时,我总是会为您提供帮助。 快乐的编码:)

翻译自: https://www.includehelp.com/c/nested-loops.aspx

c语言 循环嵌套循环

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值