c语言循环编程练习_C编程中的循环教程

c语言循环编程练习

C语言循环 (Loops in C language )

Loops are used to repeat a part of the program specified number of times or until a specific condition is being specified.

循环用于重复执行部分程序指定次数或直到指定了特定条件。

There are total three ways in which we can achieve this:

我们可以通过三种方式实现这一目标:

  1. The for loop

    for循环

  2. The while loop, and

    while循环,和

  3. The do...while loop

    do ... while循环

These loops are explained in detail as under.

这些循环的详细说明如下。

1. for循环 (1. The for loop)

This is the most commonly used loop by the programmers and requires us to initialize three conditions in a single line at the start of the loop.

这是程序员最常用的循环,要求我们在循环开始时在一行中初始化三个条件。

Syntax of 'for' loop:

for循环的语法:

    for (initialize ; condition ; increment)
    {
	    //body of the loop
	    //Code to be repeated till the test condition;
    }

Flow chart:

流程图:

c语言循环编程练习_C编程中的循环教程_第1张图片

Let us see how the loop functions?

让我们看看循环是如何工作的?

Problem 1: Input two integers and find their average.

问题1:输入两个整数并找到它们的平均值。

Solution: (Flow chart)

解决方案:(流程图)

C Language Code:

C语言代码:

#include <stdio.h>

int main()
{
	int  a, b, avg, count ;
	
	for(count = 1; count<=3; count++)
	{
		printf("Enter values of a and b: ");
		scanf("%d %d",&a,&b);
		avg=(a+b)/2;
		printf("Average = %d" , avg);
	}
	
	return 0;
}

Output

输出量

    Enter the value of a and b: 2 4
    Average = 3
    Enter the value of a and b: 2 3
    Average = 2 
    Enter the value of a and b: 2 6
    Average = 4

Explanation:

说明:

  • When the loop begins for the first time, the value of count is set to be 1.

    第一次开始循环时, count的值设置为1。

  • Now, the condition count<=3 is tested. If the condition is true, the control enters into the loop and the body is executed once.

    现在,条件计数<= 3被测试。 如果条件为真,则控件进入循环,并且主体执行一次。

  • After reaching the closing brace of the loop, the control is sent back to the for statement where the value of count is incremented by 1.

    到达循环的右括号后,控制权被发送回for语句,其中count的值增加1。

  • After this, the condition count<=3 is again checked to see if the value of count is still within the range 1 to 3.

    此后,再次检查条件count <= 3 ,以查看count的值是否仍在1到3的范围内。

  • If the condition stands true, the loop is executed again. This happens till the value of count becomes 3.

    如果条件成立,则再次执行循环。 直到计数值变为3为止。

  • When the value of count becomes 4, the loop terminates and the control is transferred to the statement after for (if any).

    当count的值变为4时,循环终止,并将控制转移到for(如果有)之后的语句。

Now, it is not necessary that we always write the initial, test condition and increment in the same as stated above. There are many other ways to do so. Let us see some of them:

现在,我们不必总是按照上述相同的方式编写初始测试条件和增量。 还有许多其他方法可以这样做。 让我们看看其中的一些:

Problem 2: Print integers from 1 to 5.

问题2:打印1到5之间的整数。

C code:

C代码:

#include <stdio.h>

int main()
{
	int i; //loop counter
	
	//method 1
	printf("Method 1...\n");
	for(i=1; i<=5; i++)
		printf("%d ",i);
	printf("\n");
	
	//method 2 (increment is in printf statement)
	printf("Method 2...\n");
	for(i=1; i<=5; )
		printf("%d ",i++);
	printf("\n");
	
	//method 3 (printf statement with ++ in third section)
	printf("Method 3...\n");
	for(i=1; i<=5; printf("%d ",i++));
	printf("\n");
	
	//method 4 
	//initialization of loop before for statement
	//increment is with the printf statement
	printf("Method 4...\n");
	i=1;
	for( ; i<=5; )
		printf("%d ",i++);
	
	return 0;
}

Output

输出量

    Method 1...
    1 2 3 4 5 
    Method 2...
    1 2 3 4 5 
    Method 3...
    1 2 3 4 5 
    Method 4...
    1 2 3 4 5 


2. while循环 (2. The while loop)

This loop is mostly suitable for conditions where we have to perform a task a fixed number of times. For example, writing the data of 10 students of a class.

此循环最适合必须执行固定次数的任务的情况。 例如,编写一个班级10名学生的数据。

Syntax of 'while' loop

while循环的语法

    while (test_condition)
    {
	    //body
	    //Code to be repeated till the test condition;
	    //other statement like ++/--
    }

Flow chart:

流程图:

Let us see how the loop functions?

让我们看看循环是如何工作的?

Problem 1: Input two integers and find their average.

问题1:输入两个整数并找到它们的平均值。

Solution: (Flow chart)

解决方案:(流程图)

C Language Code:

C语言代码:

#include <stdio.h>

int main()
{
	int  a, b, avg, count ;
	
	count =1;
	
	while( count<=3 )
	{
		printf("Enter values of a and b: ");
		scanf("%d %d",&a,&b);
		avg=(a+b)/2;
		printf("Average = %d" , avg);
	}
	
	return 0;
}

Output

输出量

    Enter the value of a and b: 2 4
    Average = 3
    Enter the value of a and b: 2 3
    Average = 2 
    Enter the value of a and b: 2 6
    Average = 4

Explanation:

说明:

  • Here, as long as the condition within the parenthesis stands true, all the statements inside the loop are executed.

    在这里,只要括号内的条件成立,就执行循环内的所有语句。

  • Which means that the while loop will be executed thrice.

    这意味着while循环将执行三次。

  • To be noted, if we do not write the condition in which we increment the value of count, the loop will become an infinite loop. This happens because the condition count<=3 stands true forever.

    需要注意的是,如果不写增加count值的条件,则循环将成为无限循环。 这是因为条件计数<= 3永远成立。

  • Another important point is, there should be no semicolon after the while statement otherwise the loop will become indefinite.

    另一个要点是,在while语句之后不应使用分号,否则循环将变得不确定。

Now, just as we saw in the for loop, there can be various ways in which we can achieve this result.

现在 ,就像我们在for循环中看到的那样,可以通过多种方式来实现此结果。

Problem 2: Print integers from 1 to 5.

问题2:打印1到5之间的整数。

C code:

C代码:

#include <stdio.h>

int main()
{
	int i; //loop counter
	
	//method 1
	printf("Method 1...\n");
	i=1;
	while(i<=5)
	{
		printf("%d ",i);
		i++;
	}
	printf("\n");
	
	//method 2 (increment is in printf statement)
	printf("Method 2...\n");
	i=1;
	while(i<=5)
	{
		printf("%d ",i++);
	}
	printf("\n");
	
	
	return 0;
}

Output

输出量

    Method 1...
    1 2 3 4 5 
    Method 2...
    1 2 3 4 5 


3. do ... while循环 (3. The do...while loop)

The do...while loop is almost the same as while loop except for one difference.

除了一个区别外, do ... while循环while循环几乎相同。

As an important point, the do...while loop is always executed at least once, unlike the other loops. This happens because the loop runs for the first time and then at the end, the condition is checked to be true or not. If it stands true, the loop runs again and if false, the control comes out.

重要的一点是, do ... while循环始终至少执行一次,这与其他循环不同。 发生这种情况是因为循环是第一次运行,然后最后才检查条件是否为真。 如果为true,则循环再次运行;如果为false,则控制出现。

Syntax of 'do...while' loop:

'do ... while'循环的语法:

    do
    {
	    //body
	    //Code to be repeated till the test condition;
	    //other statement like ++/--
    }while(test_condition);

Flow chart:

流程图:

Let us see how the loop functions?

让我们看看循环是如何工作的?

Problem 1: Input two integers and find their average.

问题1:输入两个整数并找到它们的平均值。

Solution: (Flow chart)

解决方案:(流程图)

C Language Code:

C语言代码:

#include <stdio.h>

int main()
{
	int  a, b, avg, count ;
	
	count =1;
	
	do
	{
		printf("Enter values of a and b: ");
		scanf("%d %d",&a,&b);
		avg=(a+b)/2;
		printf("Average = %d" , avg);
	}while( count<=3 );
	
	return 0;
}

Output

输出量

    Enter the value of a and b: 2 4
    Average = 3
    Enter the value of a and b: 2 3
    Average = 2 
    Enter the value of a and b: 2 6
    Average = 4


翻译自: https://www.includehelp.com/c/looping-tutorial.aspx

c语言循环编程练习

你可能感兴趣的:(java,python,linux,编程语言,人工智能)