Download presentation
Presentation is loading. Please wait.
Published byHester Dixon Modified over 9 years ago
1
Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02 QUESTIONS?? HW #1 due today at 5!! Today: Loops, and two new data types Reading (for Mon): 2.3, 2.5, 6.1-6.2b Exercises: p. 50 #15, 16 New files/handouts: WhileEx1.cpp, WhileEx2.cpp, NestedLoop.cpp
2
Wednesday, 9/18/02, Slide #2 Repetition (Looping) Structures Provides the ability to repeat a list of statements (an "action") more than once Three looping structures in C++ while structure for structure do-while structure
3
Wednesday, 9/18/02, Slide #3 Looping tasks With loops you can: Repeat an action a specific number of times Play a game 5 times Repeat until some condition is met Play game until user says quit Play game until user loses 3 times Accumulate a total Add up a grocery bill and compute tax Compute the average of a set of marks Count how many occurrences of a letter there are in a given passage
4
Wednesday, 9/18/02, Slide #4 The while structure The syntax of the while structure is: The statements of the loop (the “body” of the loop) repeat while the T-F condition remains true -- possibly forever! How do we change the true/false value of the condition?? while (T-F condition) or while (T-F condition) single statement;{ list of statements }
5
Wednesday, 9/18/02, Slide #5 Initialize, Update, Exit! Initialize variables in loop condition before entering loop; Update these variables inside the loop, so it can eventually terminate; Check that the loop condition can be made false inside loop, so that loop will end. If you accidentally execute an infinite loop in VCPP, press Ctrl-Break to stop it. Statement to initialize loop condition variables while (condition) { Loop body contains statements to update loop condition variables }
6
Wednesday, 9/18/02, Slide #6Examples Write a program that inputs a number from the user, representing a restaurant bill, that adds 7% tax, then 15% tip to that, and prints the total. The process should repeat until the user enters a number <= 0. Write a program that writes “TGIF” ten times, using only one output statement
7
Wednesday, 9/18/02, Slide #7 First while-loop example Write a program that inputs a number from the user, representing a restaurant bill, that adds 7% tax, then 15% tip to that, and prints the total. The process should repeat until the user enters a number <= 0. See WhileEx1.cpp for full program float tax = 0.07; float tip = 0.15; float amount; cin >> amount; //while positive amount, compute tax and tip while (amount > 0) { float taxtotal = amount * (1 + tax); float total = taxtotal * (1 + tip); cout << total << endl ; cin >> amount; } Initialize loop condition objects before loop! Update loop condition objects inside loop!
8
Wednesday, 9/18/02, Slide #8 Sentinels and Counters The object amount in the last program is called a sentinel, because its value signals when to end the loop. If we have a loop that should repeat a fixed number of times, we can use a loop counter to control the loop. The loop counter starts at 0 It is increased by 1 each time the loop executes The loop ends when the counter reaches the desired number of executions
9
Wednesday, 9/18/02, Slide #9 Second While Loop Example Write a program that writes “TGIF” ten times, using only one output statement See WhileEx2.cpp for complete program. Off-by-one errors: A common error is to write loops that execute one too many or one too few times – always check!! float numTimes = 10; float count = 0; //count = # loop executions while (count < numTimes) { cout << "TGIF" << endl; count = count + 1; }
10
Wednesday, 9/18/02, Slide #10 Accumulating values The statement count = count + 1; inside the loop, causes the object count to increase by 1 each time We can accumulate other values too: Write a program to input a list of positive numbers and compute their sum; 0 or negative number ends input Modify to compute average of the numbers Modify to count how many are over 50
11
Wednesday, 9/18/02, Slide #11 “Nested Loops”: An example Assume that the objects numRows and numCols have the values 3 and 5, resp. Trace the following code segment by hand, showing values in memory and on monitor. See NestedLoop.cpp for complete program float row = 0; while (row < numRows) { float col = 0; while (col < numCols) { cout << '*'; col = col + 1; } cout << endl; row = row + 1; }
12
Wednesday, 9/18/02, Slide #12 Nested Loops If we want to repeat a process that uses a loop, then we put one loop inside another as part of its action. For each iteration of the outer loop, the inner loop must be completely executed. Example: Modify the previous program to ask the user if she/he would like to draw another rectangle.
13
Wednesday, 9/18/02, Slide #13 Choosing the right data type: int, float, and double In many of the previous programs, the float objects we used were always whole numbers. C++ provides different data types for whole numbers and numbers with fractional parts: int: An object of type int may only be a whole number float: An object of type float may have a fractional part double: Similar to float, but with larger number of decimal places, and larger magnitude permitted Look at programs we did today. Which objects should be type int, and which should be float or double?
14
Wednesday, 9/18/02, Slide #14 More on int, float, and double C++ constants: If we use a numeric constant value in C++ that is a whole number, it is automatically given type int. If it has a fractional part, it is given type double. Integer arithmetic: Any arithmetic operation involving only int type objects produces an int type object. For example, what is 5 / 3 + 3 / 5
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.