上海交通大学交大密西根 ·联合学院一 181 UM-SJTU Joint Institute ■ University of Michigan Shanghal Jiao Tong University Vg101:Introduction to Computer and Programming Expressions The McGraw-Hill Companies,Inc.,2000
Vg101:Introduction to Vg101:Introduction to Computer and Programming Computer and Programming Expressions © The McGraw-Hill Companies, Inc., 2000
上海交通大学交大密西根 B ·联合学院 181 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University The bool Type and Operators Often in a program you need to compare two values,such as whether i is greater than j.C++provides six relational operators (also known as comparison operators)in Table 3.1 that can be used to compare two values
The bool Type and Operators Type and Operators • Often in a program you need to compare two values, such as whether i is greater than j. C++ provides six relational operators (also known as comparison operators) in Table 3.1 that can be used to compare two values
上海交通大学交大密西根 8 ·联合学院·一 181 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Operator Name Example Result < less than 1<2 true <= less than or equal to 1<=2 true > greater than 1>2 false >= greater than or equal to 1>=2 false == equal to 1==2 false l= not equal to 1!=2 true
Operator Name Example Result Comparison Operators Comparison Operators < less than 1 < 2 true <= less than or equal to 1 <= 2 true > greater than 1 > 2 false >= greater than or equal to 1 >= 2 false == equal to 1 == 2 false != not equal to 1 != 2 true
Simple if Statement ■ class OddOrEvenConsoleT:public ConsoleT E if (booleanExpression) public: void disp (int number) statement(s); if (number 2 ==0) printLine (number,"is even.n"); if (number 2!=0) printLine (number,"is odd.n"); 5 int main (void) OddOrEvenConsoleT console; int number console.readInt (Enter an integer:")j console.disp (number)j retur 0j
Simple if Statement Statement if (booleanExpression) { statement(s); }
Boolean Operators p !p Example true false !(1 >2)is true,because(1 2)is false. false true !(1>0)is false,because(1 >0)is true. pl p2 p1&&p2 Example false false false (3>2)&&(5>=5)is true,because(3> false true false 2)and (5 >=5)are both true. true false false (3>2)&&(5>5)is false,because(5> true true true 5)is false. pl p2 p1 II p2 Example false false false (2>3)(5>5)is false,because(2>3) false true true and(5>5)are both false. true false true (3>2)(5>5)is true,because (3>2) true true true is true
• Operator Name: ! Not; && and || or p !p true false false true Example !(1 > 2) is true, because (1 > 2) is false. !(1 > 0) is false, because (1 > 0) is true. Boolean Operators Boolean Operators p1 p2 p1 && p2 false false false false true false true false false true true true Example (3 > 2) && (5 >= 5) is true, because (3 > 2) and (5 >= 5) are both true. (3 > 2) && (5 > 5) is false, because (5 > 5) is false. p1 p2 p1 || p2 false false false false true true true false true true true true Example (2 > 3) || (5 > 5) is false, because (2 > 3) and (5 > 5) are both false. (3 > 2) || (5 > 5) is true, because (3 > 2) is true