Kỹ thuật lập trình Chương 4

35 132 0
Kỹ thuật lập trình Chương 4

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Selection Criteria Selection Statements Conditional Expression C++ Selection Structures Department of Computer Science HCMC University of Technology, Viet Nam 02, 2014 Department of Computer Science C++ Selection Structures Selection Criteria Selection Statements Conditional Expression Outline Selection Criteria Selection Statements If Statement Switch Statement Conditional Expression Department of Computer Science C++ Selection Structures Selection Criteria Selection Statements Conditional Expression Overview action action no action condition yes action action Department of Computer Science action C++ Selection Structures Selection Criteria Selection Statements Conditional Expression Selection Criteria Selection criteria = value of expression ⇒ select appropriate flow of control C++ if statement: values (true/false, 0/not 0) switch statement: multiple discrete values Department of Computer Science C++ Selection Structures Selection Criteria Selection Statements Conditional Expression Relational Operators used to compare operands return Boolean value (true/false) (i.e bool type) Operator == != >= > = 100 y > 10 x = Left && Left || Left = += -= *= /= Right Low Read http://www.cplusplus.com/doc/tutorial/operators/ for more details Precedence High Department of Computer Science C++ Selection Structures Selection Criteria Selection Statements Conditional Expression Example char key = ’m ’ ; i n t i = , j = , k = 12; double x = 2 ; Expression i + == k - ’a’ + == ’b’ 25 >= x + 1.0 k > || t < Equivalent (i + 2) == (k – 1) (’a’ + 1) == ’b’ 25 >= (x + 1.0) (k > 2) || (t < 5) Department of Computer Science Value 1 Interpretation false true true false C++ Selection Structures Selection Criteria Selection Statements Conditional Expression Order of evaluation for Arithmetic/Relational Expression Higher precedence operators are evaluated before lower precedence operator 10 ∗ + ∗ − / − ∗ 20 + 10 − − 27 30 − − 27 26 − 27 −1 Department of Computer Science C++ Selection Structures Selection Criteria Selection Statements Conditional Expression Order of evaluation for Logical Expression Short-circuit evaluation &&: if the left operand is false ⇒ stop and return false ||: if the left operand is true ⇒ stop and return true a ! = && b / a > 10 f a l s e && b / a > 10 false a == | | b / a > 10 t r u e | | b / a > 10 true Department of Computer Science C++ Selection Structures Selection Criteria Selection Statements Conditional Expression If Statement Switch Statement Dangling else i f ( exp1 ) i f ( exp2 ) stmt1 else stmt2 (exp1) if if else (exp2) stmt1 stmt2 or if if (exp1) (exp2) stmt1 Department of Computer Science else C++ Selection Structures stmt2 Selection Criteria Selection Statements Conditional Expression If Statement Switch Statement Solve dangling else Use block statement if (exp1) {if (exp2) stmt1} else stmt2 if (exp1) {if (exp2) stmt1 else stmt2} Use C++ convention: else matches with closest unmatch if statement if (exp1) if (exp2) stmt1 else stmt2 Department of Computer Science C++ Selection Structures Selection Criteria Selection Statements Conditional Expression If Statement Switch Statement switch Statement switch statement allows to execute a group of statements among multiple groups is based on the value of an expression requires the expression in int type Firstly, evaluate Syntax: switch ( < expression >) { case < v a l u e >: [ < statements > ] [ break ; ] case < v a l u e >: [ < statements > ] [ break ; ] case < v a l u e >: [ < statements > ] [ d e f a u l t : ] } Department of Computer Science C++ Selection Structures Selection Criteria Selection Statements Conditional Expression If Statement Switch Statement switch Statement switch statement allows to execute a group of statements among multiple groups is based on the value of an expression requires the expression in int type Syntax: Then, compare to switch ( < expression >) { case < v a l u e >: [ < statements > ] [ break ; ] case < v a l u e >: [ < statements > ] [ break ; ] case < v a l u e >: [ < statements > ] [ d e f a u l t : ] } Department of Computer Science C++ Selection Structures Selection Criteria Selection Statements Conditional Expression If Statement Switch Statement switch Statement switch statement allows to execute a group of statements among multiple groups is based on the value of an expression requires the expression in int type If match, Syntax: switch ( < expression >) { case < v a l u e >: [ < statements > ] [ break ; ] case < v a l u e >: [ < statements > ] [ break ; ] case < v a l u e >: [ < statements > ] [ d e f a u l t : ] } Department of Computer Science execute C++ Selection Structures Selection Criteria Selection Statements Conditional Expression If Statement Switch Statement switch Statement switch statement allows to execute a group of statements among multiple groups is based on the value of an expression requires the expression in int type Syntax: If not match, compare to switch ( < expression >) { case < v a l u e >: [ < statements > ] [ break ; ] case < v a l u e >: [ < statements > ] [ break ; ] case < v a l u e >: [ < statements > ] [ d e f a u l t : ] } Department of Computer Science C++ Selection Structures Selection Criteria Selection Statements Conditional Expression If Statement Switch Statement switch Statement switch statement allows to execute a group of statements among multiple groups is based on the value of an expression requires the expression in int type Syntax: switch ( < expression >) { case < v a l u e >: [ < statements > ] [ break ; ] case < v a l u e >: [ < statements > ] [ break ; ] case < v a l u e >: [ < statements > ] [ d e f a u l t : ] } Department of Computer Science If match, execute C++ Selection Structures Selection Criteria Selection Statements Conditional Expression If Statement Switch Statement switch Statement switch statement allows to execute a group of statements among multiple groups is based on the value of an expression requires the expression in int type Syntax: switch ( < expression >) { case < v a l u e >: [ < statements > ] [ break ; ] case < v a l u e >: [ < statements > ] [ break ; ] case < v a l u e >: [ < statements > ] [ d e f a u l t : ] } Department of Computer Science If there is break, go out of switch C++ Selection Structures Selection Criteria Selection Statements Conditional Expression If Statement Switch Statement switch Statement switch statement allows to execute a group of statements among multiple groups is based on the value of an expression requires the expression in int type Syntax: switch ( < expression >) { case < v a l u e >: [ < statements > ] [ break ; ] case < v a l u e >: [ < statements > ] [ break ; ] case < v a l u e >: [ < statements > ] [ d e f a u l t : ] } Department of Computer Science If not match any , execute C++ Selection Structures Selection Criteria Selection Statements Conditional Expression If Statement Switch Statement Example # include < iostream > using namespace s t d ; i n t main ( ) { int iCity ; c o u t

Ngày đăng: 30/09/2017, 04:36

Từ khóa liên quan

Mục lục

  • Selection Criteria

  • Selection Statements

    • If Statement

    • Switch Statement

    • Conditional Expression

Tài liệu cùng người dùng

Tài liệu liên quan