Post on 03-Oct-2020
transcript
1/30
Test overview Test Schedule Miscellanea Chapter 6 Q & A Conclusion References Files
CSC-201 - Computer Science ILecture #4: Chapter 6
Dr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck Cartledge
September 12, 2016 at 5:09pmSeptember 12, 2016 at 5:09pmSeptember 12, 2016 at 5:09pmSeptember 12, 2016 at 5:09pmSeptember 12, 2016 at 5:09pmSeptember 12, 2016 at 5:09pmSeptember 12, 2016 at 5:09pmSeptember 12, 2016 at 5:09pmSeptember 12, 2016 at 5:09pmSeptember 12, 2016 at 5:09pmSeptember 12, 2016 at 5:09pmSeptember 12, 2016 at 5:09pmSeptember 12, 2016 at 5:09pmSeptember 12, 2016 at 5:09pmSeptember 12, 2016 at 5:09pmSeptember 12, 2016 at 5:09pmSeptember 12, 2016 at 5:09pmSeptember 12, 2016 at 5:09pmSeptember 12, 2016 at 5:09pmSeptember 12, 2016 at 5:09pmSeptember 12, 2016 at 5:09pm
2/30
Test overview Test Schedule Miscellanea Chapter 6 Q & A Conclusion References Files
Table of contents I
1 Test overview
2 Test
3 Schedule
4 Miscellanea
5 Chapter 6
6 Q & A
7 Conclusion
8 References
9 Files
3/30
Test overview Test Schedule Miscellanea Chapter 6 Q & A Conclusion References Files
This is it.
Cover chapters 1 through 5(inclusive)
Exam has been written yet
Exam should take about anhour and 15.
One letter sized sheet ofpaper as “cheat sheet”
When finished with exam,leave room with answers
Lecture will start after examends
Any questions? Almost too late.
4/30
Test overview Test Schedule Miscellanea Chapter 6 Q & A Conclusion References Files
The beatings will continue until morale improves.
You’ve got:1 hour 15 minutes
5/30
Test overview Test Schedule Miscellanea Chapter 6 Q & A Conclusion References Files
Schedule for the semester
Wk. Date Topic Wk. Date Topic1 08/22 Chaps. 1 – 2 X 9 10/17 Chap. 102 08/29 Chaps. 3 – 4 X 10 10/24 Chaps. 10 – 113 09/05 Chap. 5 X 11 10/31 Chaps. 10 – 114 09/12 Test – Chap. 6 12 11/07 Test– Chap. 145 09/19 Chap. 7 13 11/14 Chap. 146 09/26 Chap. 8 14 11/28 Chap. 187 10/03 Chap. 9 15 12/05 Chap. 188 10/10 Test – Chap. 10 16 12/12 Exam
6/30
Test overview Test Schedule Miscellanea Chapter 6 Q & A Conclusion References Files
Corrections and additions since last lecture.
Updated assignment #02included files
Homework for chapter 5 duebefore class
7/30
Test overview Test Schedule Miscellanea Chapter 6 Q & A Conclusion References Files
This and that
Read from a file.
Objectives are fairlystraightforward:
Read strings from a datafile.
First string is the key.
Second string is the plaintext.
Output plain and“encrypted” strings to thescreen in a particular order
Submit your source code.
This is a single person effort (nota team effort).
Any questions or problems?
8/30
Test overview Test Schedule Miscellanea Chapter 6 Q & A Conclusion References Files
Looping
Basic looping structures
while loopwhile ( conditional) { statements with changer}for loopfor (initial; conditional; changer) { statements}do ...while loopdo { statements with changer} while (conditional)Handcrafted (which we won’t go into)
9/30
Test overview Test Schedule Miscellanea Chapter 6 Q & A Conclusion References Files
Looping
What is a loop and why do we care?
“A loop executes the same statement (simple orcompound) over and over again, as long as a condition orset of conditions is satisfied.”
Dale and Weems [2]
10/30
Test overview Test Schedule Miscellanea Chapter 6 Q & A Conclusion References Files
Looping
Loop taxonomy
Classical ways of classifying looping structures:
Count controlled Repeat statement(s) aspecified number of times
Event controlled Repeat statement(s) until a“stop” repeating conditionis reached
We’ll look at the different types of looping statements as count orevent controlled structures.
11/30
Test overview Test Schedule Miscellanea Chapter 6 Q & A Conclusion References Files
Looping
The event controlled while loop
1 The conditional is evaluated.
2 If the conditional is FALSEthen branch out
3 Execute the statements inblock
4 Repeat
The statements in the block maynever be executed.
Image from [4].
12/30
Test overview Test Schedule Miscellanea Chapter 6 Q & A Conclusion References Files
Looping
Same image.
Image from [4].
13/30
Test overview Test Schedule Miscellanea Chapter 6 Q & A Conclusion References Files
Looping
The count controlled while loop
A counter is initialized
1 The conditional is evaluated(usually based on thecounter)
2 If the conditional is FALSEthen branch out
3 Execute the statements inblock
4 Update the counter
5 Repeat
The statements in the block maynever be executed.
Image from [3].
14/30
Test overview Test Schedule Miscellanea Chapter 6 Q & A Conclusion References Files
Looping
Same image.
Image from [3].
15/30
Test overview Test Schedule Miscellanea Chapter 6 Q & A Conclusion References Files
Looping
The do . . . while loop
1 Execute the statements inblock
2 The conditional is evaluated
3 If the conditional is FALSEthen branch out
4 Repeat
The statements in the block willalways be executed at least once.
Image from [3].
16/30
Test overview Test Schedule Miscellanea Chapter 6 Q & A Conclusion References Files
Looping
Same image.
Image from [3].
17/30
Test overview Test Schedule Miscellanea Chapter 6 Q & A Conclusion References Files
Looping
Comparing different looping constructs
Image from [3]. The different structures have different shapes.
18/30
Test overview Test Schedule Miscellanea Chapter 6 Q & A Conclusion References Files
Looping
Comparing different looping constructs doing the samething
Image from [3]. Other implementations are possible.
19/30
Test overview Test Schedule Miscellanea Chapter 6 Q & A Conclusion References Files
Expanding on event controlled looping
Different kinds of controlling events
Sentinel Keep processing data until aspecial value that is not apossible data value isentered to indicate thatprocessing should stop
End-of-file Keep processing data aslong as there is more data inthe file
Flag Keep processing data untilthe value of a flag changesin the loop body
We’ll look at each of these different types.
20/30
Test overview Test Schedule Miscellanea Chapter 6 Q & A Conclusion References Files
Expanding on event controlled looping
A simple scenario
There is a data file myInfile that contains blood pressuremeasurements.1
The data file has some special/interesting values:
There may be exactly 100 readings.
There may be some unknown number of readings.
There may be a special value (-1) that means stop reading.
There may be out of range data (>= 200) that means stopreading.
We’ll look at how to handle these cases individually.
1Example taken from slide deck provided by JB Learning.
21/30
Test overview Test Schedule Miscellanea Chapter 6 Q & A Conclusion References Files
Expanding on event controlled looping
Count controlled loop
Read exactly 100 values from the file.
std::string fileName = "/tmp/temp.txt";
std::ifstream inputFile;
int data;
inputFile.open(fileName.c_str());
for (int i = 0; i < 100; i++)
{
data
22/30
Test overview Test Schedule Miscellanea Chapter 6 Q & A Conclusion References Files
Expanding on event controlled looping
End-of-file controlled loop
Read all the data from the file.
std::string fileName = "/tmp/temp.txt";
std::ifstream inputFile;
int data;
inputFile.open(fileName.c_str());
while(inputFile)
{
data
23/30
Test overview Test Schedule Miscellanea Chapter 6 Q & A Conclusion References Files
Expanding on event controlled looping
Sentinel controlled loop
Read all the data from the file until a special value is read.
std::string fileName = "/tmp/temp.txt";
std::ifstream inputFile;
int data = 0;
inputFile.open(fileName.c_str());
for (; data > -1 ;)
{
data
24/30
Test overview Test Schedule Miscellanea Chapter 6 Q & A Conclusion References Files
Expanding on event controlled looping
Flag controlled loop
Read all the data from the file until a special value is read.
std::string fileName = "/tmp/temp.txt";
std::ifstream inputFile;
int data = 0;
inputFile.open(fileName.c_str());
for (; data < 200 ;)
{
data
25/30
Test overview Test Schedule Miscellanea Chapter 6 Q & A Conclusion References Files
Expanding on event controlled looping
The “infinite” loop
There may be times when you want a loop to repeat, but theterminate conditions are too hard to make into a set ofcomparisons.std::string fileName = "/tmp/temp.txt";
std::ifstream inputFile;
int data = 0;
inputFile.open(fileName.c_str());
for (; ;) {
data
26/30
Test overview Test Schedule Miscellanea Chapter 6 Q & A Conclusion References Files
Expanding on event controlled looping
Nesting looping control structures
Looping structures can be“nested” one inside the other.The only rule is that the innerlooping structure has to becompletely inside the outerstructure.
Image from [1].
Looping structures can be nested as deeply as makes sense. (SeelineCt.cpp in source files for example.)
27/30
Test overview Test Schedule Miscellanea Chapter 6 Q & A Conclusion References Files
Q & A time.
“’The Answer to the GreatQuestion . . . Of Life, the Universeand Everything . . . is. . . forty-two,’ said DeepThought, with infinite majestyand calm.”Douglas Adams, TheHitchhiker’s Guide to theGalaxy
28/30
Test overview Test Schedule Miscellanea Chapter 6 Q & A Conclusion References Files
What have we covered?
Test #01Covered looping statements indifferent formsTalked about assignment #002Assignment #002 and Chapter 6homework before class
Next time: Chapter 7 (and return tests)
29/30
Test overview Test Schedule Miscellanea Chapter 6 Q & A Conclusion References Files
References I
[1] Russ Burlingame, Catch all these evolving pokemon nesting dollgraphics, http://comicbook.com/2015/09/16/catch-all-these-evolving-pokemon-nesting-doll-graphics, 2015.
[2] Nell Dale and Chip Weems, Programming and problem solvingwith c++: Comprehensive, Jones & Bartlett Publishers, 2013.
[3] EduGrabs Staff, Difference between for while and do while loopin c++, http://www.edugrabs.com/difference-between-for-while-and-do-while-loop-in-c/, 2015.
[4] TutorialsPoint Staff, C++ while loop, http://www.tutorialspoint.com/cplusplus/cpp_while_loop.htm.
http://comicbook.com/2015/09/16/catch-all-these-evolving-pokemon-nesting-doll-graphicshttp://comicbook.com/2015/09/16/catch-all-these-evolving-pokemon-nesting-doll-graphicshttp://www.edugrabs.com/difference-between-for-while-and-do-while-loop-in-c/http://www.edugrabs.com/difference-between-for-while-and-do-while-loop-in-c/http://www.tutorialspoint.com/cplusplus/cpp_while_loop.htmhttp://www.tutorialspoint.com/cplusplus/cpp_while_loop.htm
30/30
Test overview Test Schedule Miscellanea Chapter 6 Q & A Conclusion References Files
Files of interest
1 Edgar Dijkstra’s letterabout the GOTO statement
Edgar Dijkstra: Go To Statement Considered Harmful
1
Edgar Dijkstra: Go To Statement Considered Harmful
Edgar Dijkstra: Go To Statement Considered Harmful
2
Aus: Communications of the ACM 11, 3 (March 1968). 147-148.
"Chuck Cartledge"
Test overviewTestScheduleMiscellaneaCorrections and additions since last lecture.This and that
Chapter 6LoopingExpanding on event controlled looping
Q & AConclusionReferencesFiles