Download presentation
Presentation is loading. Please wait.
Published byPhillip Hunt Modified over 9 years ago
1
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1
2
Control Structures II (Repetition) 2
3
Why Is Repetition Needed? Repetition allows efficient use of variables Can input, add, and average multiple numbers using a limited number of variables For example, to add five numbers: ◦ Declare a variable for each number, input the numbers and add the variables together ◦ Create a loop that reads a number into a variable and adds it to a variable that contains the sum of the numbers 3
4
while Looping (Repetition) Structure Syntax of the while statement: expression acts as a decision maker and is usually a logical expression statement is called the body of the loop The parentheses are part of the syntax 4
5
while Looping (Repetition) Structure (cont’d.) 5
6
6
7
The body of the while executes when the expression evaluates to true The expression checks whether a variable called loop control variable (LCV) satisfies certain condition Previous example i was LCV LCV should be initialized and eventually make the expression evaluate to false (We do this by updating LCV) 7
8
Generally while loops are written in the following form: /*initialize loop control variable*/ while (expression){ /*expression tests LCV*/ //update LCV } 8
9
while Looping (Repetition) Structure (cont’d.) 9
10
Case 1: Counter-Controlled while Loops When you know exactly how many times the statements need to be executed ◦ Use a counter-controlled while loop 10
11
Example Ch5_CounterControl.cpp 11
12
Case 2: Sentinel-Controlled while Loops Sentinel variable is tested in the condition Loop ends when sentinel is encountered 12
13
Example Ch5_SentinelControl.cpp 13
14
Case 3: Flag-Controlled while Loops Flag-controlled while loop: uses a bool variable to control the loop 14
15
Example Ch5_FlagControlledLoop.cpp 15
16
Case 3: Flag-Controlled while Loops Flag-controlled while loop: uses a bool variable to control the loop 16
17
Example Ch5_FlagControlledLoop.cpp 17
18
Case 4: EOF-Controlled while Loops End-of-file (EOF)-controlled while loop: when it is difficult to select a sentinel value 18
19
eof Function The function eof can determine the end of file status eof is a member of data type istream Syntax for the function eof : Ch5_EOFControlledLoop.cpp 19
20
for Looping (Repetition) Structure for loop: called a counted or indexed for loop Syntax of the for statement: The initial statement, loop condition, and update statement are called for loop control statements 20
21
for Looping (Repetition) Structure (cont’d.) 21
22
for Looping (Repetition) Structure (cont’d.) 22
23
for Looping (Repetition) Structure (cont’d.) 23
24
for Looping (Repetition) Structure (cont’d.) The following is a semantic error: 24
25
The following is a legal (but infinite) for loop: for (;;) cout << "Hello" << endl; 25
26
for Looping (Repetition) Structure (cont’d.) 26
27
do…while Looping (Repetition) Structure Syntax of a do...while loop: The statement executes first, and then the expression is evaluated ◦ As long as expression is true, loop continues To avoid an infinite loop, body must contain a statement that makes the expression false 27
28
do…while Looping (Repetition) Structure (cont’d.) The statement can be simple or compound Loop always iterates at least once 28
29
do…while Looping (Repetition) Structure (cont’d.) 29
30
do…while Looping (Repetition) Structure (cont’d.) 30
31
While Loops Exercises Powerpoint on cs150 website Documents: good examples for exam questions http://www.cs.odu.edu/~cs150/week_04/ workpage_whiles_ifs/ http://www.cs.odu.edu/~cs150/week_04/ workpage_whiles_ifs/ http://www.cs.odu.edu/~cs150/week_04/t ues_Sept_15_2015.doc http://www.cs.odu.edu/~cs150/week_04/t ues_Sept_15_2015.doc 31
32
Examples arithmetic_quiz.cpp http://www.cs.odu.edu/~cs150/week_04/t uesday_codeExamples/ http://www.cs.odu.edu/~cs150/week_04/t uesday_codeExamples/ http://www.cs.odu.edu/~cs150/week_04/t hursday_codeExamples/students/ http://www.cs.odu.edu/~cs150/week_04/t hursday_codeExamples/students/ 32
33
User-defined functions 33
34
A C++ program is a collection of functions. main(){} Short programs: Programming instructions are packed in a function main. For larger programs it is not practical break the program into manageable pieces. 34
35
Functions (modules) They are like mini programs that can be combined to form larger programs They allow complicated programs to be divided into manageable pieces 35
36
Functions In C++, a function is similar to that of a function in algebra ◦ It has a name ◦ It does some computation It is a rule or correspondence between values f(x) = 2x +5 f(1) = 7 1 is argument, 7 is corresponding value of the function 36
37
Predefined Functions Functions can be user-defined or predefined Some of the predefined mathematical functions are: sqrt(x) pow(x, y) floor(x) calculates the largest whole number that is less than or equal to x 37
38
Predefined Functions (cont'd.) pow (2.0, 3) = 8.0 pow is double sqrt (4) = 2 floor(48.79) = 48.0 the largest whole number that is less than or equal to 48.79 38
39
Predefined Functions (cont'd.) Predefined functions are organized into separate libraries ◦ I/O functions are in iostream header ◦ cmath header contains math functions ◦ To use predefined functions, you must include the header file using an include statement 39
40
40
41
41
42
Example Example_6-1.cpp 42
43
User-Defined Functions You can use functions in a program again and again (previous examples, pow function) C++ does not provide every function that you will ever need You must learn how to write your own functions 43
44
User-Defined Functions Value-returning functions: have a return type ◦ Return a value of a specific data type using the return statement ◦ Remember main function and return 0? Void functions: do not have a return type ◦ Do not use a return statement to return a value 44
45
Value-Returning Functions To use these functions, you must: ◦ Include the appropriate header file in your program using the include statement ◦ Know the following items: Name of the function Number of parameters, if any Data type of each parameter Data type of the value returned: called the type of the function 45
46
Value-Returning Functions (cont’d.) Can use the value returned by a value- returning function by: ◦ Saving it for further calculation ◦ Ex: x = pow (3.0,5.0) ◦ Using it in some calculation ◦ Ex: area = PI * pow ( radius, 2.0) ◦ Printing it ◦ Ex: cout << abs (-5) ; 46
47
Value-Returning Functions (cont’d.) A value-returning function is used in an assignment an output statement 47
48
Value-Returning Functions (cont’d.) Heading (or function header): first line of the function ◦ Example: int abs(int number) Formal parameter: variable declared in the heading ◦ Example: number 48
49
Another Example: Heading of function: double pow (double base, double exponent ); Formal parameters are: base and exponent 49
50
double u = 2.5; double v = 3.0; double x,y; x = pow (u, v); y = pow (2.0, 3.2); Value of u is copied into base Value of v is copied into exponent u and v are actual parameters Same for 2.0 and 3.2 ! 50
51
Value-Returning Functions (cont’d.) Formal parameter: variable declared in the heading Actual parameter: variable or expression listed in a call to a function ◦ Example: x = pow(u, v) 51
52
Syntax: Value-Returning Function Syntax: functionType is also called the data type or return type (the value that the function returns) Statements (declaration or executable) 52
53
Syntax: Formal Parameter List 53
54
Function Call Syntax to call a value-returning function: 54
55
Syntax: Actual Parameter List Syntax of the actual parameter list: Formal parameter list can be empty: A call to a value-returning function with an empty formal parameter list is: 55
56
return Statement Function returns its value via the return statement ◦ It passes this value outside the function Syntax: In C++, return is a reserved word When a return statement executes ◦ Function immediately terminates When a return statement executes in the function main, the program terminates 56
57
Syntax: return Statement (cont’d.) 57
58
Function Prototype Function prototype: function heading without the body of the function. Comes before main. It’s not a definition. Gives the program the name of the function. Syntax: Data type of each parameter must be specified 58
59
Example Ch6_Largest.cpp 59
60
Value-Returning Functions: Some Peculiarities 60
61
Value-Returning Functions: Some Peculiarities (cont’d.) 61
62
Example Example_6-3.cpp Example_6-4_RollDice.cpp Example 6-6_CableCompanyBill.cpp 62
63
Flow of Compilation and Execution Execution always begins at the first statement in the function main Function prototypes appear before any function definition ◦ Compiler compiles these first Compiler can then correctly translate a function call 63
64
Void Functions User-defined void functions can be placed either before or after the function main If user-defined void functions are placed after the function main ◦ The function prototype must be placed before the function main Void function does not have a return type 64
65
Void Functions (cont’d.) Formal parameters are optional Void function definition syntax: 65
66
Void Functions (cont’d.) Formal parameter list syntax: Function call syntax: Actual parameter list syntax: 66
67
Void Functions (cont’d.) Value parameter: a formal parameter that receives a copy of the content of corresponding actual parameter Reference parameter: a formal parameter that receives the location (memory address) of the corresponding actual parameter 67
68
Value Parameters If a formal parameter is a value parameter: ◦ The value of the corresponding actual parameter is copied into it ◦ Formal parameter has its own copy of the data During program execution ◦ Formal parameter manipulates the data stored in its own memory space 68
69
Reference Variables as Parameters If a formal parameter is a reference parameter ◦ It receives the memory address of the corresponding actual parameter During program execution to manipulate data ◦ Changes to formal parameter will change the corresponding actual parameter 69
70
Reference Variables as Parameters (cont’d.) Reference parameters are useful in three situations: ◦ Returning more than one value ◦ Changing the actual parameter ◦ When passing the address would save memory space and time 70
71
Value and Reference Parameters and Memory Allocation When a function is called ◦ Memory for its formal parameters and its local variables is allocated in the function data area For a value parameter, the actual parameter’s value is copied into the formal parameter’s memory cell ◦ Changes to the formal parameter do not affect the actual parameter’s value 71
72
Value and Reference Parameters and Memory Allocation (cont’d.) For a reference parameter, the actual parameter’s address passes to the formal parameter Both formal and actual parameters refer to the same memory location During execution, changes made to the formal parameter’s value permanently change the actual parameter’s value 72
73
Example Example 6-15.cpp 73
74
Scope of an Identifier Scope of an identifier: where in the program the identifier is accessible Local identifier: identifiers declared within a function (or block) Global identifier: identifiers declared outside of every function definition C++ does not allow nested functions – Definition of one function cannot be included in the body of another function 74
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.