1. Trang chủ
  2. » Giáo án - Bài giảng

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

35 133 0

Đ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

Thông tin cơ bản

Định dạng
Số trang 35
Dung lượng 111,48 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Selection Statements Conditional Expression... Selection Statements Conditional ExpressionSelection Criteria Selection criteria = value of expression ⇒ select appropriate flow of control

Trang 1

Selection Statements

Conditional Expression

C++ Selection Structures

Department of Computer Science

HCMC University of Technology, Viet Nam

02, 2014

Trang 2

Selection Statements Conditional Expression

Trang 3

Selection Statements Conditional Expression

yes no

Trang 4

Selection Statements Conditional Expression

Selection Criteria

Selection criteria = value of expression ⇒ select appropriate flow of control

C++

if statement: 2 values (true/false, 0/not 0)

switch statement: multiple discrete values

Trang 5

Selection Statements Conditional Expression

Relational Operators

used to compare 2 operands

return Boolean value (true/false) (i.e bool type)

Trang 6

Selection Statements Conditional Expression

Logical Operators

used to create more complex conditions

its operands in bool type

also return bool value

short-circuit evaluation

Trang 7

Selection Statements Conditional Expression

Operator Precedence and Associativity

List in the decreasing order of the precedence

Trang 8

Selection Statements Conditional Expression

Trang 9

Selection Statements Conditional Expression

Order of evaluation for Arithmetic/Relational Expression

Higher precedence operators are evaluated before lower precedence operator

Trang 10

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

Trang 11

Selection Statements Conditional Expression

The bool data type

C++ uses bool for Boolean type

The actual value of true is 1 and false is o

Trang 12

The if-else statement

directs the computer to

select a statement based

no

Trang 13

taxable income greater

than $20,000, taxes are

2.5% of the income that

exceeds $20,000 plus a

fixed amount of $400

StartInput taxable

taxable > CUTOFF

taxes = HIGHRATE * (taxable - CUTOFF) + FIXEDAMT

taxes= LOWRATE

* taxable

Output taxesEnd

yes

no

Trang 15

Please t y p e i n t h e t a x a b l e income : 30000Taxes are $ 650

Trang 16

no

Trang 22

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

Trang 23

is based on the value of an expression

requires the expression in int type

Syntax:

switch ( < e x p r e s s i o n >) {

case < v a l u e 1 >: [ < statements > ]

[ break ; ] case < v a l u e 2 >: [ < statements > ]

[ break ; ] case < v a l u e 3 >: [ < statements > ]

Trang 24

is based on the value of an expression

requires the expression in int type

Syntax:

switch ( < e x p r e s s i o n >) {

case < v a l u e 1 >: [ < statements > ]

[ break ; ] case < v a l u e 2 >: [ < statements > ]

[ break ; ] case < v a l u e 3 >: [ < statements > ]

[ d e f a u l t : < s ta te men ts > ]

Then, compare to <value 1>

Trang 25

is based on the value of an expression

requires the expression in int type

Syntax:

switch ( < e x p r e s s i o n >) {

case < v a l u e 1 >: [ < statements > ]

[ break ; ] case < v a l u e 2 >: [ < statements > ]

[ break ; ] case < v a l u e 3 >: [ < statements > ]

[ d e f a u l t : < s ta te men ts > ]

}

If match,execute <statements>

Trang 26

is based on the value of an expression

requires the expression in int type

Syntax:

switch ( < e x p r e s s i o n >) {

case < v a l u e 1 >: [ < statements > ]

[ break ; ] case < v a l u e 2 >: [ < statements > ]

[ break ; ] case < v a l u e 3 >: [ < statements > ]

[ d e f a u l t : < s ta te men ts > ]

If not match, compare to <value 2>

Trang 27

is based on the value of an expression

requires the expression in int type

Syntax:

switch ( < e x p r e s s i o n >) {

case < v a l u e 1 >: [ < statements > ]

[ break ; ] case < v a l u e 2 >: [ < statements > ]

[ break ; ] case < v a l u e 3 >: [ < statements > ]

Trang 28

is based on the value of an expression

requires the expression in int type

Syntax:

switch ( < e x p r e s s i o n >) {

case < v a l u e 1 >: [ < statements > ]

[ break ; ] case < v a l u e 2 >: [ < statements > ]

[ break ; ] case < v a l u e 3 >: [ < statements > ]

[ d e f a u l t : < s ta te men ts > ]

If there is break,

go out of switch

Trang 29

is based on the value of an expression

requires the expression in int type

Syntax:

switch ( < e x p r e s s i o n >) {

case < v a l u e 1 >: [ < statements > ]

[ break ; ] case < v a l u e 2 >: [ < statements > ]

[ break ; ] case < v a l u e 3 >: [ < statements > ]

Trang 34

Selection Statements

Conditional Expression

Conditional Expression

There are 3 argument expressions

If the value of the first expression is true, evaluate and return the value of the second expression.

Otherwise, evaluate and return the value of the third.

Trang 35

Selection Statements

Conditional Expression

Summary

Relational and logical expression

If-Else and If-No-Else statement

Nested if statement

Switch statement

Conditional Expression

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

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w