【文章內(nèi)容簡介】
ger a:)。 scanf(%d,a)。 printf(Enter integer b:)。 scanf(%d,b)。 if(a==b) printf(a==b\n)。 else printf(a!=b\n)。 } ex3 : Iput two numbers ,decide them to be equally or not. Result: Enter integer a:12? Enter integer b:12? a==b Result: Enter integer a:12? Enter integer b:9? a!=b 第五章選擇結(jié)構(gòu) If語句嵌套舉例 Return General form: expression1? expression2: expression3 Condition operator mands three operate objects,it is a operator alonely in C language. ex: max=(ab) ? a : b。 Equal to : if(ab) max= a。 else max=b。 第五章選擇結(jié)構(gòu) 條件運算符 Condition operator DCL: ( 1) Execution order : Firstly,solve the expression 1, If the value is ture,then solve the expression 2, this moment here is the value of whole expressions. If the value is false, then solve the expression 3, this moment here is the value of whole expressions. ex: max=(ab) ? a : b。 If a=3,b=4。 max = 4; If a=6, b=4。 max = 6; 第五章選擇結(jié)構(gòu) 條件運算符 ( 2) Condition operator overmatch assigned operator,but under relation and arithmetic operator. ( 3) Condition operator’ s bined direcion is from right to left. ex1: max=(ab) ? a : b。 max= ab ? a : b。 ex2: max=ab? a : b+1。 max= ab ? a : (b+1)。 ex3: max= ab ? a : c d ? c:d ; max= ab ? a : ( c d ? c:d) 。 Suppose a=1,b=2,c=3,d=4 第五章選擇結(jié)構(gòu) 條件運算符 ( 4) Condition operator cann’ t replce ordinary ifstatements,only when the embed statements in it is assigned statements(and two branches assign to the same variable). ex: if (ab) printf(“ %d” ,a)。 else printf(“ %d” ,b)。 printf(“ %d” ,ab?a:b)。 ( 5) The type of expression 1 and expression 2 can be same,also be different. ex: int x。 x ? ‘ a’ : ‘ b’ 。 ex: xy ? 1 : 。 第五章選擇結(jié)構(gòu) 條件運算符 Ex5: