c语言程序设计循环控制结构(1)
今天我们学习C语言程序设计第5章——循环控制结构的内容。今天的主要内容有三部分,分别是循环控制结构与循环语句,计数控制的循环,嵌套循环。
Today we are learning about C Programming Chapter 5 - Loop Control Structures. The three main parts of today"s topic are loop control structures and loop statements, counting controlled loops, and nested loops.
No. 1 循环控制结构与循环语句
实际应用中的许多问题,都会涉及重复执行一些操作,如级数求和,穷举或迭代求解等。若需重复处理的次数是已知的,则称为计数控制的循环,若重复处理的次数是未知的,是由给定条件控制的,称为条件控制的的循环。
for语句:在循环顶部进行循环调节测试,如果循环条件第一次测试就为假,则循环体一次也不执行,适合循环次数已知、计数控制的循环。
while语句:适合于循环次数未知、条件控制的循环。
do-while语句:在循环底部进行循环条件测试,循环至少执行一次。适合于循环次数未知、条件控制的循环。
注意:
1.循环开始前要将各个变量初始化。
2.while和for语句后的复合语句要记住带上{}
3.如何对循环变量进行增值,决定了循环的执行次数,如果在循环体内再次改变这个变量的值,将改变循环正常的执行次数。
4.由于每次循环体执行完以后,都要执行一次增值表达。因此,这里在最后退出for循环之后,i的值实际为n+1。
Many problems in practical applications involve the repeated execution of operations such as summation of series, exhaustive enumeration or iterative solution. If the number of repetitions to be performed is known, the loop is called a count-controlled loop, if the number of repetitions is unknown and is controlled by a given condition, the loop is called a condition-controlled loop.
if the loop condition is false on the first test, the loop body is not executed once, suitable for loops with a known number of cycles and count control.
while statement: suitable for loops where the number of loops is unknown and the condition is controlled.
do-while statement: The loop condition is tested at the bottom of the loop and the loop is executed at least once. Suitable for loops with an unknown number of loops and condition control.
Caution.
1. Each variable is initialised before the loop starts.
2. Remember to include {} in the compound statement after the while and for statements.
3. How the loop variable is value-added determines the number of times the loop is executed. If the value of this variable is changed again within the loop, it will change the normal number of times the loop is executed.
4. Since the value-adding expression is executed once after each execution of the loop body. Therefore, here the value of i is actually n+1 after the final exit from the for loop.
No. 2计数控制的循环
循环次数事先已知的循环称为计数控制的循环。累加求和问题和累乘求积问题都是典型的使用计数控制的循环来编程实现。
Loops in which the number of loops is known in advance are called count-controlled loops. The cumulative sum problem and the cumulative product problem are both typically implemented programmatically using count-controlled loops.
No. 3嵌套循环
将一个循环语句放在另一个循环语句的循环体中构成的循环,称为嵌套循环。
执行嵌套循环时,先有外层进入内层循环,并在内层循环终止后接着执行外层循环,再由外层循环进入内层循环中,当外层循环全部终止时,程序结束。
编写累加求和程序的关键在于寻找累加项(即通项)的构成规律。通常,当累加的项较为复杂或者前后项之间无关时,需要单独的计算每个累加项。而当累加项的前项与后项之间有关时,则可以根据累加项的后项与前项之间的关系,通过前项来计算后项。
A loop formed by placing one loop statement within the body of another loop statement is called a nested loop.
When a nested loop is executed, the outer loop first enters the inner loop and is followed by the outer loop after the inner loop terminates, then the outer loop enters the inner loop, and the program ends when all the outer loops terminate.
The key to writing a cumulative summation program is to find a pattern in the composition of the summation terms (i.e. the generic terms). Usually, when the terms are complex or unrelated to each other, each term is calculated separately. When the first and second terms of the summation are related, the second term can be calculated from the first term based on the relationship between the second and second terms of the summation.
参考资料:文字:百度;图片:微博;翻译:百度翻译
本文由LearningYard新学苑原创,部分图片文字来自网络,如有侵权请联系。