c语⾔if循环次数怎么写代码,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:
我们可以通过三种⽅式实现这⼀⽬标:
The for loop
for循环
The while loop, and
while循环,和
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:
流程图:
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
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
int main()
{
int i; //loop counter
//method 1
printf("\n");
for(i=1; i<=5; i++)
printf("%d",i);
printf("\n");
//method 2 (increment is in printf statement)
printf("\n");
for(i=1; i<=5; )
printf("%d",i++);
printf("\n");
//method 3 (printf statement with ++ in third section)
printf("\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("\n");
i=1;
for( ; i<=5; )
printf("%d",i++);
return 0;
}
Output
输出量
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
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
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