Chapter 2 ABSOLUTE C++ Flow of Control WALTER SAVITCH SECOND EDITION PEARSON Copyright2006 Pearson Addison-Wesley All rights reserved
Chapter 2 Flow of Control
Learning Objectives Boolean Expressions Building,Evaluating Precedence Rules Branching Mechanisms ◆if-else ◆switch ◆Nesting if-.else ◆Loops ◆Vhile,do-while,for ◆Nesting loops Copyright 2006 Pearson Addison-Wesley.All rights reserved. 2-2
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 2-2 Learning Objectives ¨ Boolean Expressions ¨ Building, Evaluating & Precedence Rules ¨ Branching Mechanisms ¨ if-else ¨ switch ¨ Nesting if-else ¨ Loops ¨ While, do-while, for ¨ Nesting loops
Boolean Expressions: Display 2.1 Comparison Operators ◆Logical Operators ·Logical AND(&&) ·Logical OR(Il) Display 2.1 Comparison Operators MATH ENGLISH C++NOTATION C++SAMPLE MATH SYMBOL EQUIVALENT Equal to X+7==2*y ×+7=2y Not equal to I= ans !='n' ans≠'nl Less than < count m +3 count<m+3 Less than or time <=limit time≤limit equal to Greater than time limit time limit Greater than > age>=21 age≥2l or equal to Copyright 2006 Pearson Addison-Wesley.All rights reserved. 23
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 2-3 Boolean Expressions: Display 2.1 Comparison Operators ¨ Logical Operators ¨ Logical AND (&&) ¨ Logical OR (||)
Evaluating Boolean Expressions ◆Data type bool ◆Returns true or false true,false are predefined library consts ◆Truth tables Display 2.2 next slide Copyright 2006 Pearson Addison-Wesley.All rights reserved. 2-4
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 2-4 Evaluating Boolean Expressions ¨ Data type bool ¨Returns true or false ¨true, false are predefined library consts ¨ Truth tables ¨Display 2.2 next slide
Evaluating Boolean Expressions: Display 2.2 Truth Tables Display 2.2 Truth Tables AND Exp_1 Exp_2 Exp_1 &Exp_2 true true true true false false NOT false true false false false false Exp !(Exp) true false OR false true Exp_1 Exp_2 Exp_Exp_2 true true true true false true false true true false false false Copyright 2006 Pearson Addison-Wesley.All rights reserved. 2-5
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 2-5 Evaluating Boolean Expressions: Display 2.2 Truth Tables