Team LiB
Previous Section Next Section

Chapter 6. Statements

第六章 语句

CONTENTS

Section 6.1 Simple Statements

192

Section 6.2 Declaration Statements

193

Section 6.3 Compound Statements (Blocks)

193

Section 6.4 Statement Scope

194

Section 6.5 The if Statement

195

Section 6.6 The switch Statement

199

Section 6.7 The while Statement

204

Section 6.8 The for Loop Statement

207

Section 6.9 The do while Statement

210

Section 6.10 The break Statement

212

Section 6.11 The continue Statement

214

Section 6.12 The goto Statement

214

Section 6.13 try Blocks and Exception Handling

215

Section 6.14 Using the Preprocessor for Debugging

220

Chapter Summary

223

Defined Terms

223


Statements are analogous to sentences in a natural language. In C++ there are simple statements that execute a single task and compound statements that consist of a block of statements that execute as a unit. Like most languages, C++ provides statements for conditional execution and loops that repeatedly execute the same body of code. This chapter looks in detail at the statements supported by C++.

语句类似于自然语言中的句子。C++ 语言既有只完成单一任务的简单语句,也有作为一个单元执行的由一组语句组成的复合语句。和大多数语言一样,C++ 也提供了实现条件分支结构的语句以及重复地执行同一段代码的循环结构。本章将详细讨论 C++ 所支持的语句。

By default, statements are executed sequentially. Except for the simplest programs, sequential execution is inadequate. Therefore, C++ also defines a set of flow-of-control statements that allow statements to be executed conditionally or repeatedly. The if and switch statements support conditional execution. The for, while, and do while statements support repetitive execution. These latter statements are often referred to as loops or iteration statements.

通常情况下,语句是顺序执行的。但是,除了最简单的程序外,只有顺序执行往往并不足够。为此,C++ 定义了一组控制流语句,允许有条件地执行或者重复地执行某部分功能。ifswitch 语句提供了条件分支结构,而 forwhiledo while 语句则支持重复执行的功能。后几种语句常称为循环或者迭代语句。

Team LiB
Previous Section Next Section