Lecture Programming in C++ - Chapter 5: Decision making

25 52 0
Lecture Programming in C++ - Chapter 5: Decision making

Đ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

On completion of this chapter students will learned how to: Create simple if and if-else statements, create relational expressions, utilize logical operators, interpret the order of precedence for all operators (arithmetic, relational, and logical), create multiple decision statements (if-else-if or switch statements), utilize bool data type.

Chapter 5 – Decision Making Simple if Statement Capable of making decisions Relational expression evaluated – Evaluated as true or false if  (relational expression)      { Block of statements  done if results true,       statement(s); skipped if results false!       } Lesson 5.1 Relational Expressions Type of logical expression – Produces result of true or false Compares values of two arithmetic  expressions left_operand  relational_operator   right_operand Lesson 5.1 Relational Operators < >= != Lesson 5.1 less than less than or equal to equal to greater than greater than or equal to not equal Simple If/Else Statement if (expression)    {     statement1a;     statement1b;     …     } if TRUE else    {     statement1a;     statement1b;     …     } if FALSE Lesson 5.2 Provides block of  statements to be executed  for either true OR false Braces optional if only  one statement in block if  Examples x = 3; Simple if – statements only done on true, if (x  0) && (4 == ­2)) Group x = (TRUE || (FALSE && FALSE)) x = (TRUE || FALSE) x = (TRUE) x = 1 Group Lesson 5.5 Example of Single Variable logical value – False if value is 0 – True if value is nonzero if  (c)     statement1; else     statement2; Lesson 5.5 Statement2 executed if c has  value of zero, statement1 is  executed if c has any other  value but zero if­else­if Shifts program control, step by step,  through series of statement blocks Control stops at relational expression  that tests true Lesson 5.6 if (relational_expression_1)             false                       {       statement_block_1       } else else if (relational_expression_2)             false                       {       statement_block_2       }                      else else if (relational_expression_n­1)               true                         {       statement_block_n­1       } else       {       statement_block_n       } Lesson 5.6 if­else­if Form This statement block would be executed! switch Control Structure Constructed like if­else­if Used to transfer control Can nest switch control structures Keyword  switch followed by expression switch (expression) Must use parentheses            { Must result in             statement block integer type value             } Lesson 5.6 switch Statement Block Syntax switch (expression)            {case  constant1:                    statement1a                    statement1b                    …              case  constant2:                    statement2a                    statement2b                    …                    …              default:                   statements            }              Lesson 5.6       Keyword case only used in  switch statement case used to form label case label is constant  followed by colon Constant1, constant2, etc  must be integer expressions Constant expressions must  be unique default optional, used when  no match is found break Statement switch (expression)            {case  constant1:                    statement1a                    break;                                case  constant2:                    statement2a                    break;                    …              default:                   statements            }              Terminates execution  of switch statement Sends control to point  of closing brace Usually last statement  for each case If no break, then  statements in next case  executed bool Data Type Named after mathematician George Boole Can contain one of only two values (0 or 1) – true or false, fail or pass, off or on Declare by using keyword bool – Example:      bool   salty, hard, acidic; Assign value – Example:   acidic = (pH 

Ngày đăng: 30/01/2020, 04:27

Từ khóa liên quan

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

  • Đang cập nhật ...

Tài liệu liên quan