Computer Programming 简明教程
Decision Statements
决策对计算机编程至关重要。在许多情况下,你将获得两个或更多选项,你将必须根据给定条件选择一个选项。例如,我们想根据学生的获得的分数对学生进行评语。以下是情况 −
Decision making is critical to computer programming. There will be many situations when you will be given two or more options and you will have to select an option based on the given conditions. For example, we want to print a remark about a student based on his secured marks. Following is the situation −
Assume given marks are x for a student:
If given marks are more than 95, then
Student is brilliant
If given marks are less than 30, then
Student is poor
If given marks are less than 95 and more than 30, then
Student is average
现在,问题是如何编写编程代码来处理此类情况。几乎所有编程语言都提供基于以下流程图工作的条件语句 −
Now, the question is how to write a programming code to handle such situations. Almost all the programming languages provide conditional statements that work based on the following flow diagram −

让我们借助 if conditional statements 编写一个 C 程序,将上述情况转换为编程代码 −
Let’s write a C program with the help of if conditional statements to convert the above given situation into a programming code −
#include <stdio.h>
int main() {
int x = 45;
if( x > 95) {
printf( "Student is brilliant\n");
}
if( x < 30) {
printf( "Student is poor\n");
}
if( x < 95 && x > 30 ) {
printf( "Student is average\n");
}
}
当执行上述程序时,它将生成以下结果 −
When the above program is executed, it produces the following result −
Student is average
上述程序使用 if conditional statements 。此处,第一个 if statement 检查给定条件,即变量 x 大于或等于 95,并且如果条件为真,则进入条件主体以执行给定语句。此处,我们只有一个 printf() 语句来对学生进行评语。
The above program uses if conditional statements. Here, the first if statement checks whether the given condition i.e., variable x is greater than 95 or not and if it finds the condition is true, then the conditional body is entered to execute the given statements. Here we have only one printf() statement to print a remark about the student.
第二个 if statement 函数类似地工作。最后,执行第三个 if statement ,此处,我们有两个条件 −
Similarly, the second if statement works. Finally, the third if statement is executed, here we have the following two conditions −
-
First condition is x > 95
-
Second condition is x < 30
计算机评估给定的两个条件,然后使用二进制运算符 && 将总体结果合并。如果最终结果为真,则将执行条件语句,否则不执行任何语句。
The computer evaluates both the given conditions and then, the overall result is combined with the help of the binary operator &&. If the final result is true, then the conditional statement will be executed, otherwise no statement will be executed.
本教程将带你了解 if statements 的各种形式以及 C 编程语言中提供的 switch 语句。不同的编程语言提供不同类型的决策语句,但基本概念与本教程中解释的内容相同。
This tutorial will give you a basic idea on various forms of if statements and an introduction to switch statements available in C programming language. Different programming languages provide different types of decision-making statements, but the basic concept remains the same as explained in this tutorial.
if…else statement
一个 if 语句后面可以跟随可选的 else 语句,当布尔表达式为假时执行该语句。C 编程语言中 if…else 语句的语法是 −
An if statement can be followed by an optional else statement, which executes when the Boolean expression is false. The syntax of an if…else statement in C programming language is −
if(boolean_expression) {
/* Statement(s) will execute if the boolean expression is true */
} else {
/* Statement(s) will execute if the boolean expression is false */
}
上述语法可以表示为流程图形式,如下所示 −
The above syntax can be represented in the form of a flow diagram as shown below −

当我们必须在两个选项中做出决策时, if…else 语句非常有用。例如,如果一个学生获得的分数超过 95 分,那么该学生就是优秀的,否则无法对这种情况进行编码,如下所示 −
An if…else statement is useful when we have to take a decision out of two options. For example, if a student secures more marks than 95, then the student is brilliant, otherwise no such situation can be coded, as follows −
#include <stdio.h>
int main() {
int x = 45;
if( x > 95) {
printf( "Student is brilliant\n");
} else {
printf( "Student is not brilliant\n");
}
}
当执行上述程序时,它将生成以下结果 −
When the above program is executed, it produces the following result −
Student is not brilliant
if…elseif…else statement
一个 if 语句后面可以跟随一个可选的 else if…else 语句,这对于测试各种条件非常有用。
An if statement can be followed by an optional else if…else statement, which is very useful to test various conditions.
在使用 if, else if, else 语句时,需要记住以下几点:
While using if, else if, else statements, there are a few points to keep in mind −
-
An if can have zero or one else’s and it must come after an else if.
-
An if can have zero to many else…if’s and they must come before the else.
-
Once an else…if succeeds, none of the remaining else…if’s or else’s will be tested.
C 编程语言中 if…else if…else 语句的语法为:
The syntax of an if…else if…else statement in C programming language is −
if(boolean_expression 1) {
/* Executes when the boolean expression 1 is true */
}
else if( boolean_expression 2) {
/* Executes when the boolean expression 2 is true */
}
else if( boolean_expression 3) {
/* Executes when the boolean expression 3 is true */
} else {
/* Executes when the none of the above condition is true */
}
现在,借助 if…elseif…else 语句,第一个程序可以编码为:
Now with the help of if…elseif…else statement, the very first program can be coded as follows −
#include <stdio.h>
int main() {
int x = 45;
if( x > 95) {
printf( "Student is brilliant\n");
}
else if( x < 30) {
printf( "Student is poor\n");
}
else if( x < 95 && x > 30 ) {
printf( "Student is average\n");
}
}
当执行上述程序时,它将生成以下结果 −
When the above program is executed, it produces the following result −
Student is average
The Switch Statement
switch 语句是 if statements 的替代语句,它允许测试变量是否等于值列表。每个值称为 case ,并且要切换的变量针对每个 switch case 进行检查。其语法如下:
A switch statement is an alternative of if statements which allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case. It has the following syntax −
switch(expression){
case ONE :
statement(s);
break;
case TWO:
statement(s);
break;
......
default :
statement(s);
}
switch 语句中使用的 expression 必须提供整数,使其将针对给出的不同 case 进行相等性比较。当表达式值与 case 值匹配时,将执行该 case 的主体,最后,将使用 break 语句终止 switch。如果没有提供 break 语句,则计算机继续执行匹配的 case 下方提供的其他语句。如果没有 case 匹配,则执行 default case 主体。
The expression used in a switch statement must give an integer value, which will be compared for equality with different cases given. Wherever an expression value matches with a case value, the body of that case will be executed and finally, the switch will be terminated using a break statement. If no break statements are provided, then the computer continues executing other statements available below to the matched case. If none of the cases matches, then the default case body is executed.
上述语法可以表示为流程图形式,如下所示 −
The above syntax can be represented in the form of a flow diagram as shown below −

现在,让我们考虑另一个示例,我们希望为给定数字编写等效的英语单词。然后,可以对其进行如下编码:
Now, let’s consider another example where we want to write the equivalent English word for a given number. Then, it can be coded as follows −
#include <stdio.h>
int main() {
int x = 2;
switch( x ){
case 1 :
printf( "One\n");
break;
case 2 :
printf( "Two\n");
break;
case 3 :
printf( "Three\n");
break;
case 4 :
printf( "Four\n");
break;
default :
printf( "None of the above...\n");
}
}
当执行上述程序时,它将生成以下结果 −
When the above program is executed, it produces the following result −
Two
Decisions in Java
以下是使用 Java 编写的等效程序,它也支持 if 、 if…else 、 if…elseif…else 和 switch 语句。
Following is the equivalent program written in Java which too supports if, if…else, if…elseif…else, and switch statements.
您可以尝试执行以下程序以查看输出,其必须与上述 C 示例生成的输出相同。
You can try to execute the following program to see the output, which must be identical to the result generated by the above C example.
public class DemoJava {
public static void main(String []args) {
int x = 45;
if( x > 95) {
System.out.println( "Student is brilliant");
}
else if( x < 30) {
System.out.println( "Student is poor");
}
else if( x < 95 && x > 30 ) {
System.out.println( "Student is average");
}
}
}
当执行上述程序时,它将生成以下结果 −
When the above program is executed, it produces the following result −
Student is average
Decisions in Python
以下是使用 Python 编写的等效程序。Python 提供 if 、 if…else 、 if…elif…else 和 switch 语句。此处,您必须注意,Python 不使用花括号作为条件主体,而是使用语句的缩进识别块的主体。
Following is the equivalent program written in Python. Python provides if, if…else, if…elif…else, and switch statements. Here, you must note that Python does not make use of curly braces for conditional body, instead it simply identifies the body of the block using indentation of the statements.
您可以尝试执行以下程序以查看输出:
You can try to execute the following program to see the output −
x = 45
if x > 95:
print "Student is brilliant"
elif x < 30:
print "Student is poor"
elif x < 95 and x > 30:
print "Student is average"
print "The end"
当执行上述程序时,它将生成以下结果 −
When the above program is executed, it produces the following result −
Student is average
The end