上海交通大学交大密西根 ·联合学院一 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 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 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
Statement Types in C++ Programs in C++consist of a set of classes.Those classes contain functions,and each of those functions consists of a sequence of statements. Statements in C++fall into three basic types: Simple statements Compound statements Control statements Control statements fall into two categories: Conditional statements that specify some kind of test Iterative statements that specify repetition
Statement Types in C++ Statement Types in C++ • Programs in C++ consist of a set of classes. Those classes contain functions, and each of those functions consists of a sequence of statements . • Statements in C++ fall into three basic types: – Simple statements – Compound statements – Control statements • Control statements fall into two categories: – Conditional statements that specify some kind of test – Iterative statements that specify repetition
上海交通大学交大密西根 联合学院· 8T UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Control Statements and Problem Solving Before looking at the individual control statement forms in detail,it helps to look more holistically at a couple of programs that make use of common control patterns. The next few slides extend the Add2Integers program from Chapter 2 to create programs that add longer lists of integers.These slides illustrate three different strategies: Adding new code to process each input value Repeating the input cycle a predetermined number of times Repeating the input cycle until the user enters a special sentinel value
Control Statements and Problem Solving Control Statements and Problem Solving • Before looking at the individual control statement forms in detail, it helps to look more holistically at a couple of programs that make use of common control patterns. • The next few slides extend the Add2Integers program from Chapter 2 to create programs that add longer lists of integers. These slides illustrate three different strategies: – Adding new code to process each input value – Repeating the input cycle a predetermined number of times – Repeating the input cycle until the user enters a special sentinel value
The Add4Integers Program If you don't have access to control statements,the only way you can increase the number of input values is to add a new statement for each one,as in the following example: int main (void) AddIntConsoleT console; int int1,int2,int3,int4,intSum; console.printLine (This program will compute the sum ofn"); console.printLine (two integers specified by the user.nn"); int1 console.readInt (Please input the first integer"); int2 console.readInt (Please input the second integer"); int3=console.readInt (Please input the third integer"); int4 console.readInt (Please input the fourth integer") intSum =int1 int2 int3+int4 console.disp (intSum)j retur 0j It works,but
The Add4Integers Add4Integers Program Program • If you don’t have access to control statements, the only way you can increase the number of input values is to add a new statement for each one, as in the following example: • It works, but …
The Repeat-N-Times Idiom One strategy for generalizing the addition program is to use the Repeat-N-Times idiom,which executes a set of statements a specified number of times.The general form of the idiom is for (int i=0;i<repetitions;i++){ statements to be repeated The information about the number of repetitions is specified by the first line in the pattern,which is called the header line. The statements to be repeated are called the body of the for statement and are indented with respect to the header line. A control statement that repeats a section of code is called a loop. Each execution of the body of a loop is called a cycle
The Repeat The Repeat-N-Times Idiom Times Idiom One strategy for generalizing the addition program is to use the Repeat-N-Times idiom, which executes a set of statements a specified number of times. The general form of the idiom is for (int i = 0; i < repetitions; i++) { statements to be repeated } As is true for all idiomatic patterns in this book, the italicized words indicate the parts of the pattern you need to change for each application. To use this pattern, for example, you need to replace repetitions with an expression giving the number of repetitions and include the statements to be repeated inside the curly braces. The information about the number of repetitions is specified by the first line in the pattern, which is called the header line. The statements to be repeated are called the body of the for statement and are indented with respect to the header line. A control statement that repeats a section of code is called a loop. Each execution of the body of a loop is called a cycle. for (int i = 0; i < repetitions; i++) { statements to be repeated }
The AddNIntegers Program This program uses the Repeat-N-Times idiom to compute the sum of a predetermined number of integer values,specified by the named constant n. int main (void) AddIntConsoleT console; int i,n,intSumj console.printLine (This program will compute the sum ofn); console.printLine (n integers specified by the user.n); n console.readInt (how many integer you wang to add:"); intSum 0; for(i=0gi<ngi++) intSum+=console.readInt (Please input an integer:"); 8 console.disp (intSum); return 0;
The AddNIntegers AddNIntegers Program Program This program uses the Repeat-N-Times idiom to compute the sum of a predetermined number of integer values, specified by the named constant n. The for loop in this example works correctly only if the variable intSum is initialized to 0 before executing the loop. This body of the loop consists of only one statement. Read an integer from the user and then add that value to the variable intSum. This program uses the Repeat-N-Times idiom to compute the sum of a predetermined number of integer values, specified by the named constant n