C语言程序设计选择控制结构
今天我们继续学习C语言程序设计第4章——选择控制结构的内容。今天的主要内容有三部分,分别是用于多分支控制的条件语句,用于多路选择的switch语句,逻辑运算符和逻辑表达式。Today we continue our study of C Programming Chapter 4 - Selection Control Structures. There are three main parts to today"s topic, namely conditional statements for multi-branch control, switch statements for multiple choice, logical operators and logical expressions.
No. 1 用于多分支控制的条件语句
条件语句的第三种形式是else-if形式的条件语句。
The third form of conditional statement is the else-if form of conditional statement.
No. 2 用于多路选择的switch语句
当问题需要讨论的情况较多(一般大于三种)时,通常使用开关语句代替条件语句来简化程序的设计。c语言中的开关语句,也称为switch语句。switch语句相当于一系列的if-else语句,被测试的表达式写在关键字switch后面的圆括号中,表达式只能说char型或int型。注意,常量与case中间至少有一个空格,常量的后面是冒号,常量的类型应与switch后括号内表达式的类型一致。
When there are more cases to discuss in a problem (usually more than three), it is common to use switch statements instead of conditional statements to simplify the design of the program. switch statements in c, also known as switch statements, are equivalent to a series of if-else statements, where the expression being tested is written in parentheses after the keyword switch, and the expression can only say char type or int. Note that there is at least one space between the constant and the case, the constant is followed by a colon, and the type of the constant should be the same as the type of the expression in the parentheses after the switch.
No. 3 逻辑运算符与逻辑表达式
在c语言中用逻辑运算符表达复杂的逻辑关系,用逻辑运算符连接操作数组成的表达式称为逻辑表达式。逻辑表达式的值,即逻辑运算的结果值同样只有真和假两个值,c语言规定用1表示真,用0表示假。
逻辑与运算的特点是:仅当两个操作数都为真时,运算结果才为真;只要有一个为假,运算结果就为假。
逻辑或运算的特点是:两个操作数中只要有一个为真,运算结果就为真;仅当两个操作数都为假,运算结果才为假。
逻辑非运算的特点是:若操作数的值为真,则其逻辑非运算的结果为假;反之,则为真。
Complex logical relationships are expressed in the c language using logical operators. Expressions consisting of operands connected by logical operators are called logical expressions. The value of a logical expression, i.e. the result value of a logical operation, also has only two values, true and false, and the c language specifies that 1 is used to indicate true and 0 to indicate false.
Logical and operations are characterised by the fact that the result of an operation is true only if both operands are true; as long as one is false, the result of the operation is false.
Logical or operations are characterised by the fact that if one of the two operands is true, the result of the operation is true; if both operands are false, the result of the operation is false.
Logical non-operations are characterised by the fact that the result of a logical non-operation is false if the value of the operand is true, and true if the opposite is true.
参考资料:文字:百度;图片:微博;翻译:百度翻译
本文由LearningYard新学苑原创,部分图片文字来自网络,如有侵权请联系。