3、条件语句 1 x% [/ M. o% B: h1 n; p/ O
l if 语句:单分支语句
& `( c" G' N, }$ R1 wif (Boolean_expression) & Q% u' c; Z+ S: ], Z9 Z" n! T
{
8 `/ G7 Z& K, m- u2 @0 W. l statements; // Boolean_expression为true时执行 4 N/ C3 B4 ~; r/ D) ^' d+ c& s
} ! v( O4 [8 O6 G! H
) m$ x9 q( B1 y+ k( v) il if-else 语句 7 a, @ c# o7 q% V9 x# }6 P, Z+ `
if (boolean_expression)
) D5 f7 z# y) v9 y2 C$ G{ & j5 ~- ]. g/ J' t1 P2 K
Statements1; // Boolean_expression为true时执行 4 n: n, n" t0 r* a* t; X9 j
} / d# o9 t# R4 `" b) v) Z0 ~
else
( T1 R e5 E& s+ ?{
" |7 F- @. Y! j* |# `9 |) L statements2; // Boolean_expression为false时执行
! V" s0 C9 f, A9 T& X f1 [ }
; N, w2 t) I5 P$ q+ r7 z# e4 M! tl if/else 语句可以是嵌套的
4 s3 g% f8 y; y: a if (boolean_expression) 8 U0 U( U" b. q! D2 K
{
4 s" g2 P+ A2 g( O if (boolean_expression2) 6 h6 T# o3 G% p/ N, {
{
7 o2 c, V) e( x9 W& C+ x Statements1; // Boolean_expression2为true时执行
+ A0 C3 B$ v4 i q. _ }
~. V" U" n( e! ]else
6 `; [; h" M8 w* D{
6 J0 h, r5 d9 f! s p statements2; // Boolean_expression2为false时执行
. C% @- \) T3 ]& |& L' E% X} ! r5 e3 \" g0 Y% X; j% @; M
}
' D5 @8 f5 P! Z) I" C3 I3 G else ' F: z+ g( K) K Q; ?. L' o; P) {
{ 9 h: b1 I8 Q% @$ i' k
if (boolean_expression3)
3 r) a, e' Y8 p1 t3 O8 Q( k{ X2 c# L4 p; y5 b
Statements3; // Boolean_expression3为true时执行
! B; `" g; _1 G# C2 w, P }
0 x" q7 F& i3 p# J! v$ N; K; F! q8 Gelse , a, N- w: {7 C3 a/ |+ D
{
! c( v9 i1 d5 M statements4; // Boolean_expression3为false时执行 % L7 P& {, N! C$ `8 H* `
} . @) F1 E" \ w
}
. c0 K) [& C' d. N- z o' h) T程序例: |