今天我们继续学习C语言程序设计第4章选择控制结构的内容。今天的主要内容有三部分,分别是用于多分支控制的条件语句,用于多路选择的switch语句,逻辑运算符和逻辑表达式。TodaywecontinueourstudyofCProgrammingChapter4SelectionControlStructures。Therearethreemainpartstotodaystopic,namelyconditionalstatementsformultibranchcontrol,switchstatementsformultiplechoice,logicaloperatorsandlogicalexpressions。 No。1用于多分支控制的条件语句 条件语句的第三种形式是elseif形式的条件语句。 Thethirdformofconditionalstatementistheelseifformofconditionalstatement。 No。2用于多路选择的switch语句 当问题需要讨论的情况较多(一般大于三种)时,通常使用开关语句代替条件语句来简化程序的设计。c语言中的开关语句,也称为switch语句。switch语句相当于一系列的ifelse语句,被测试的表达式写在关键字switch后面的圆括号中,表达式只能说char型或int型。注意,常量与case中间至少有一个空格,常量的后面是冒号,常量的类型应与switch后括号内表达式的类型一致。 Whentherearemorecasestodiscussinaproblem(usuallymorethanthree),itiscommontouseswitchstatementsinsteadofconditionalstatementstosimplifythedesignoftheprogram。switchstatementsinc,alsoknownasswitchstatements,areequivalenttoaseriesofifelsestatements,wheretheexpressionbeingtestediswritteninparenthesesafterthekeywordswitch,andtheexpressioncanonlysaychartypeorint。Notethatthereisatleastonespacebetweentheconstantandthecase,theconstantisfollowedbyacolon,andthetypeoftheconstantshouldbethesameasthetypeoftheexpressionintheparenthesesaftertheswitch。 No。3逻辑运算符与逻辑表达式 在c语言中用逻辑运算符表达复杂的逻辑关系,用逻辑运算符连接操作数组成的表达式称为逻辑表达式。逻辑表达式的值,即逻辑运算的结果值同样只有真和假两个值,c语言规定用1表示真,用0表示假。 逻辑与运算的特点是:仅当两个操作数都为真时,运算结果才为真;只要有一个为假,运算结果就为假。 逻辑或运算的特点是:两个操作数中只要有一个为真,运算结果就为真;仅当两个操作数都为假,运算结果才为假。 逻辑非运算的特点是:若操作数的值为真,则其逻辑非运算的结果为假;反之,则为真。 Complexlogicalrelationshipsareexpressedintheclanguageusinglogicaloperators。Expressionsconsistingofoperandsconnectedbylogicaloperatorsarecalledlogicalexpressions。Thevalueofalogicalexpression,i。e。theresultvalueofalogicaloperation,alsohasonlytwovalues,trueandfalse,andtheclanguagespecifiesthat1isusedtoindicatetrueand0toindicatefalse。 Logicalandoperationsarecharacterisedbythefactthattheresultofanoperationistrueonlyifbothoperandsaretrue;aslongasoneisfalse,theresultoftheoperationisfalse。 Logicaloroperationsarecharacterisedbythefactthatifoneofthetwooperandsistrue,theresultoftheoperationistrue;ifbothoperandsarefalse,theresultoftheoperationisfalse。 Logicalnonoperationsarecharacterisedbythefactthattheresultofalogicalnonoperationisfalseifthevalueoftheoperandistrue,andtrueiftheoppositeistrue。 参考资料:文字:百度;图片:微博;翻译:百度翻译 本文由LearningYard新学苑原创,部分图片文字来自网络,如有侵权请联系。