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