Cprogramming 简明教程
Nested Loops in C
在编程环境中,“嵌套”一词是指特定编程元素内嵌于另一个类似元素中。例如,嵌套循环、{s4}、{s5} 等。
In the programming context, the term "nesting" refers to enclosing a particular programming element inside another similar element. For example, nested loops, nested structures, nested conditional statements, etc.
Nested Loops
当 C 中的循环结构用于另一个循环的主体中时,我们称之为{s6}(或循环内循环)。其中,包含其它循环的循环被称为{s7}。被包含的循环被称为{s8}。
When a looping construct in C is employed inside the body of another loop, we call it a nested loop (or, loops within a loop). Where, the loop that encloses the other loop is called the outer loop. The one that is enclosed is called the inner loop.
General Syntax of Nested Loops
嵌套循环的一般形式如下 −
The general form of a nested loop is as follows −
Outer loop {
Inner loop {
...
...
}
...
}
C 为 {s13} 提供了三个关键字 - {s14}、{s15} 和 {s16}。嵌套可以针对这三类循环执行。这意味着你可以在 {s9} 循环内嵌一个 {s10} 循环、{s11} 循环内嵌一个 {s12} 循环,或者执行其他任意的组合。
C provides three keywords for loops formation − while, do-while, and for. Nesting can be done on any of these three types of loops. That means you can put a while loop inside a for loop, a for loop inside a do-while loop, or any other combination.
嵌套循环的一般行为是,针对外层循环的每次迭代,内层循环都完成所有迭代。
The general behaviour of nested loops is that, for each iteration of the outer loop, the inner loop completes all the iterations.
Nested For Loops
{s17} 非常常见。如果外层循环和内层循环预期每个执行三次迭代,那么最内层陈述的总迭代次数将为“3 * 3 = 9”。
Nested for loops are very common. If both the outer and inner loops are expected to perform three iterations each, the total number of iterations of the innermost statement will be "3 * 3 = 9".
Example: Nested for Loop
请看以下示例:
Take a look at the following example −
#include <stdio.h>
int main(){
int i, j;
// outer loop
for(i = 1; i <= 3; i++){
// inner loop
for(j = 1; j <= 3; j++){
printf("i: %d j: %d\n", i, j);
}
printf("End of Inner Loop \n");
}
printf("End of Outer Loop");
return 0;
}
当你运行这段代码时,它将产生以下输出:
When you run this code, it will produce the following output −
i: 1 j: 1
i: 1 j: 2
i: 1 j: 3
End of Inner Loop
i: 2 j: 1
i: 2 j: 2
i: 2 j: 3
End of Inner Loop
i: 3 j: 1
i: 3 j: 2
i: 3 j: 3
End of Inner Loop
End of Outer loop
Explanation of Nested Loop
现在,我们分析上面的程序如何工作。当遇到外层循环时,“i”是外层循环的循环变量,它被初始化为 1。由于测试条件(a ⇐ 3)为真,程序进入外层循环主体。
Now let’s analyze how the above program works. As the outer loop is encountered, "i" which is the looping variable for the outer loop is initialized to 1. Since the test condition (a ⇐ 3) is true, the program enters the outer loop body.
程序到达内层循环,“j”是控制内层循环的变量,它被初始化为 1。由于内层循环的测试条件(j ⇐ 3)为真,程序进入内层循环。打印 “a” 和 “b” 的值。
The program reaches the inner loop, and "j" which is the variable that controls the inner loop is initialized to 1. Since the test condition of the inner loop (j ⇐ 3) is true, the program enters the inner loop. The values of "a" and "b" are printed.
程序到达内层循环的末尾。其变量“j”被递增。控制跳转到步骤 4,直到条件(j ⇐ 3)为真。
The program reaches the end of the inner loop. Its variable "j" is incremented. The control jumps to step 4 until the condition (j ⇐ 3) is true.
当测试条件变为假(因为“j”变为 4 时),控制就会退出内部循环。此时遇到外部循环的末尾。控制外部变量的变量“i”会被递增,且控制跳转至步骤 3。由于这是内部循环的开始,“j”会被再次设置为 1。
As the test condition becomes false (because "j" becomes 4), the control comes out of the inner loop. The end of the outer loop is encountered. The variable "i" that controls the outer variable is incremented and the control jumps to step 3. Since it is the start of the inner loop, "j" is again set to 1.
内部循环完成其迭代并再次结束。在外部循环的测试条件 (i ⇐ 3) 变为假之前,步骤 4 至 8 将被重复。在外部循环的末尾,“i”和“j”分别变为 4 和 4。
The inner loop completes its iteration and ends again. Steps 4 to 8 will be repeated until the test condition of the outer loop (i ⇐ 3) becomes false. At the end of the outer loop, "i" and "j" have become 4 and 4 respectively.
结果显示,对于外部循环变量的每个值,内部循环变量都会获取所有值。打印出的总行数为“3 * 3 = 9”。
The result shows that, for each value of the outer looping variable, the inner looping variable takes all the values. The total lines printed are "3 * 3 = 9".
Nesting a While Loop Inside a For Loop
任何类型的循环都可以嵌套在任何其他类型中。让我们通过在外部 for 循环中放置一个 while 循环,对上述示例进行重写。
Any type of loop can be nested inside any other type. Let us rewrite the above example by putting a while loop inside the outer for loop.
Example: Nested Loops (while Loop Inside for Loop)
请看以下示例:
Take a look at the following example −
#include <stdio.h>
int main(){
int i, j;
// outer for loop
for (i = 1; i <= 3; i++){
// inner while loop
j = 1;
while (j <= 3){
printf("i: %d j: %d\n", i, j);
j++;
}
printf("End of Inner While Loop \n");
}
printf("End of Outer For loop");
return 0;
}
i: 1 j: 1
i: 1 j: 2
i: 1 j: 3
End of Inner While Loop
i: 2 j: 1
i: 2 j: 2
i: 2 j: 3
End of Inner While Loop
i: 3 j: 1
i: 3 j: 2
i: 3 j: 3
End of inner while Loop
End of outer for loop
程序员在很多应用程序中使用嵌套循环。让我们来看一些嵌套循环的更多示例。
Programmers use nested loops in a lot of applications. Let us take a look at some more examples of nested loops.
C Nested Loops Examples
Example: Printing Tables
以下程序借助两个嵌套 for 循环来打印 1 到 10 的表格。
The following program prints the tables of 1 to 10 with the help of two nested for loops.
#include <stdio.h>
int main(){
int i, j;
printf("Program to Print the Tables of 1 to 10 \n");
// outer loop
for(i = 1; i <= 10; i++){
// inner loop
for(j = 1; j <= 10; j++){
printf("%4d", i*j);
}
printf("\n");
}
return 0;
}
Output
运行代码并检查其输出:
Run the code and check its output −
Program to Print the Tables of 1 to 10
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
Example: Printing Characters Pyramid
以下代码打印字符串中字符的增加数。
The following code prints the increasing number of characters from a string.
#include <stdio.h>
#include <string.h>
int main(){
int i, j, l;
char x[] = "TutorialsPoint";
l = strlen(x);
// outer loop
for(i = 0; i < l; i++){
// inner loop
for(j = 0; j <= i; j++){
printf("%c", x[j]);
}
printf("\n");
}
return 0;
}
当你运行这段代码时,它将产生以下输出:
When you run this code, it will produce the following output −
T
Tu
Tut
Tuto
Tutor
Tutori
Tutoria
Tutorial
Tutorials
TutorialsP
TutorialsPo
TutorialsPoi
TutorialsPoin
TutorialsPoint
Example: Printing Two-Dimensional Array
在此程序中,我们将展示如何使用 nested loops 来显示 two-dimensional array 的整数。外部循环控制行号,而内部循环控制列。
In this program, we will show how you can use nested loops to display a two-dimensional array of integers. The outer loop controls the row number and the inner loop controls the columns.
#include <stdio.h>
int main(){
int i, j;
int x[4][4] = {
{1, 2, 3, 4},
{11, 22, 33, 44},
{9, 99, 999, 9999},
{10, 20, 30, 40}
};
// outer loop
for (i=0; i<=3; i++){
// inner loop
for(j=0; j <= 3; j++){
printf("%5d", x[i][j]);
}
printf("\n");
}
return 0;
}
当你运行这段代码时,它将产生以下输出:
When you run this code, it will produce the following output −
1 2 3 4
11 22 33 44
9 99 999 9999
10 20 30 40