Computer Programming 简明教程

Computer Programming - Loops

我们考虑一下一个你想打印 Hello, World! 五次的情况。这里有一个简单的 C 程序来实现 -

Let’s consider a situation when you want to print Hello, World! five times. Here is a simple C program to do the same −

#include <stdio.h>

int main() {
   printf( "Hello, World!\n");
   printf( "Hello, World!\n");
   printf( "Hello, World!\n");
   printf( "Hello, World!\n");
   printf( "Hello, World!\n");
}

当执行上述程序时,它将生成以下结果 −

When the above program is executed, it produces the following result −

Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!

这很简单,但再考虑一下你想写 Hello, World! 一千次的情况。我们肯定无法写一千次 printf() 语句。几乎所有编程语言都提供了一个称为 loop 的概念,它有助于执行一条或多条语句直至达到所需次数。所有高级编程语言都提供各种形式的循环,可用于重复执行一条或多条语句。

It was simple, but again, let’s consider another situation when you want to write Hello, World! a thousand times. We can certainly not write printf() statements a thousand times. Almost all the programming languages provide a concept called loop, which helps in executing one or more statements up to a desired number of times. All high-level programming languages provide various forms of loops, which can be used to execute one or more statements repeatedly.

让我们借助一个 while loop 来编写上述 C 程序,稍后我们将讨论这个循环如何工作

Let’s write the above C program with the help of a while loop and later, we will discuss how this loop works

#include <stdio.h>

int main() {
   int i = 0;

   while ( i < 5 ) {
      printf( "Hello, World!\n");
      i = i + 1;
   }
}

当执行上述程序时,它将生成以下结果 −

When the above program is executed, it produces the following result −

Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!

上述程序使用了一个 while loop ,该 while loop 用于执行用 {…​.} 括起来的一组编程语句。在这里,计算机首先检查给定的条件(即,变量 “a” 是否小于 5),如果发现条件为真,则进入循环体来执行给定的语句。这里,我们在循环体中使用以下两个语句 -

The above program makes use of a while loop, which is being used to execute a set of programming statements enclosed within {…​.}. Here, the computer first checks whether the given condition, i.e., variable "a" is less than 5 or not and if it finds the condition is true, then the loop body is entered to execute the given statements. Here, we have the following two statements in the loop body −

  1. First statement is printf() function, which prints Hello World!

  2. Second statement is i = i + 1, which is used to increase the value of variable i

在执行了循环体中给出的所有语句之后,计算机会返回 while( i < 5) ,并再次检查给定的条件 (i < 5),如果该条件为真,则再次执行循环。此过程一直重复,直到给定的条件仍然为真,这意味着变量 “a” 的值小于 5。

After executing all the statements given in the loop body, the computer goes back to while( i < 5) and the given condition, (i < 5), is checked again, and the loop is executed again if the condition holds true. This process repeats till the given condition remains true which means variable "a" has a value less than 5.

总之,循环语句允许我们多次执行一条或多条语句。下面给出的是大多数编程语言中循环语句的一般形式 -

To conclude, a loop statement allows us to execute a statement or group of statements multiple times. Given below is the general form of a loop statement in most of the programming languages −

loop architecture

本教程旨在向非程序员介绍编程的基本概念,因此,让我们讨论 C 编程语言中可用的两个最重要的循环。一旦你清楚了这两个循环,那么你可以选用 C 编程教程或参考书,并查看 C 中可用的其他循环以及它们的工作方式。

This tutorial has been designed to present programming’s basic concepts to non-programmers, so let’s discuss the two most important loops available in C programming language. Once you are clear about these two loops, then you can pick-up C programming tutorial or a reference book and check other loops available in C and the way they work.

The while Loop

C 编程语言中可用的 while loop 具有以下语法 -

A while loop available in C Programming language has the following syntax −

while ( condition ) {
   /*....while loop body ....*/
}

上面的代码可以用流程图的形式表示,如下所示 -

The above code can be represented in the form of a flow diagram as shown below −

cpp while loop

关于 while 循环需要了解以下重点 -

The following important points are to be noted about a while loop −

  1. A while loop starts with a keyword while followed by a condition enclosed in ( ).

  2. Further to the while() statement, you will have the body of the loop enclosed in curly braces {…​}.

  3. A while loop body can have one or more lines of source code to be executed repeatedly.

  4. If the body of a while loop has just one line, then its optional to use curly braces {…​}.

  5. A while loop keeps executing its body till a given condition holds true. As soon as the condition becomes false, the while loop comes out and continues executing from the immediate next statement after the while loop body.

  6. A condition is usually a relational statement, which is evaluated to either true or false. A value equal to zero is treated as false and any non-zero value works like true.

The do…​while Loop

while 循环在执行主体部分的任何语句之前都会检查给定的条件。C 编程提供另一种称为 do…​while 的循环形式,它允许在检查给定条件之前先执行一次循环体。其语法如下:

A while loop checks a given condition before it executes any statements given in the body part. C programming provides another form of loop, called do…​while that allows to execute a loop body before checking a given condition. It has the following syntax −

do {
   /*....do...while loop body ....*/
}
while ( condition );

上面的代码可以用流程图的形式表示,如下所示 -

The above code can be represented in the form of a flow diagram as shown below −

cpp do while loop

如果使用 do…​while 循环编写上述示例,则 Hello, World 将生成相同的结果:

If you will write the above example using do…​while loop, then Hello, World will produce the same result −

#include <stdio.h>

int main() {
   int i = 0;

   do {
      printf( "Hello, World!\n");
      i = i + 1;
   }
   while ( i < 5 );
}

当执行上述程序时,它将生成以下结果 −

When the above program is executed, it produces the following result −

Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!

The break statement

当在循环内部遇到 break 语句时,循环将立即终止,程序控制将从紧接循环后的下一条语句恢复。C 中 break 语句的语法如下:

When the break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop. The syntax for a break statement in C is as follows −

break;

break 语句可以用流程图的形式表示,如下所示:

A break statement can be represented in the form of a flow diagram as shown below −

cpp break statement

以下是上述程序的一个变体,但它只打印三次 Hello World!就会退出:

Following is a variant of the above program, but it will come out after printing Hello World! only three times −

#include <stdio.h>

int main() {
   int i = 0;
   do {
      printf( "Hello, World!\n");
      i = i + 1;

      if( i == 3 ) {
         break;
      }
   }
   while ( i < 5 );
}

当执行上述程序时,它将生成以下结果 −

When the above program is executed, it produces the following result −

Hello, World!
Hello, World!
Hello, World!

The continue statement

C 编程语言中的 continue 语句在某种程度上类似于 break 语句。 continue 不是强制终止,而是强制执行循环的下一次迭代,跳过其间的任何代码。C 中 continue 语句的语法如下:

The continue statement in C programming language works somewhat like the break statement. Instead of forcing termination, continue forces the next iteration of the loop to take place, skipping any code in between. The syntax for a continue statement in C is as follows −

continue;

continue 语句可以用流程图的形式表示,如下所示:

A continue statement can be represented in the form of a flow diagram as shown below −

cpp continue statement

以下是上述程序的一个变体,但当变量的值等于 3 时,它会跳过打印:

Following is a variant of the above program, but it will skip printing when the variable has a value equal to 3 −

#include <stdio.h>

int main() {
   int i = 0;
   do {
      if( i == 3 ) {
         i = i + 1;
         continue;
      }
      printf( "Hello, World!\n");
      i = i + 1;
   }
   while ( i < 5 );
}

当执行上述程序时,它将生成以下结果 −

When the above program is executed, it produces the following result −

Hello, World!
Hello, World!
Hello, World!
Hello, World!

Loops in Java

以下是用 Java 编写的等效程序,它也支持 whiledo…​while 循环。以下程序像我们在 C 编程中的情况一样打印 Hello, World! 五次:

Following is the equivalent program written in Java that too supports while and do…​while loops. The following program prints Hello, World! five times as we did in the case of C Programming −

您可以尝试执行以下程序以查看输出,其必须与上述示例生成的结果相同。

You can try to execute the following program to see the output, which must be identical to the result generated by the above example.

public class DemoJava {
   public static void main(String []args) {
      int i = 0;

      while ( i < 5 ) {
         System.out.println("Hello, World!");
         i = i + 1;
      }
   }
}

Java 编程中的 breakcontinue 语句的工作方式与它们在 C 编程中的工作方式完全相同。

The break and continue statements in Java programming work quite the same way as they work in C programming.

Loops in Python

以下是用 Python 编写的等效程序。Python 也支持 whiledo…​while 循环。以下程序像我们在 C 编程中的情况一样打印 Hello, World! 五次。这里必须注意,Python 不使用花括号作为循环体,而是简单地使用语句缩进识别循环体。

Following is the equivalent program written in Python. Python too supports while and do…​while loops. The following program prints Hello, World! five times as we did in case of C Programming. Here you must note that Python does not make use of curly braces for the loop body, instead it simply identifies the body of the loop using indentation of the statements.

你可以尝试执行以下程序查看输出。为了显示差异,我们使用了一条额外的 print 语句,当循环结束时它将被执行。

You can try to execute the following program to see the output. To show the difference, we have used one more print statement, which will be executed when the loop will be over.

i = 0

while (i < 5):
   print "Hello, World!"
   i = i + 1
print "Loop ends"

当执行上述程序时,它将生成以下结果 −

When the above program is executed, it produces the following result −

Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Loop ends

Python 中的 breakcontinue 语句的工作方式与它们在 C 编程中的工作方式完全相同。

The break and continue statements in Python work quite the same way as they do in C programming.