13-Problem-Solving.pptx - CMPSC 201 Lecture 13...

Info icon This preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon
CMPSC 201 Lecture 13 Problem-solving (some slides adapted from Jenn Plasterr)
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Objectives Be able to use identify different abnormal statements Be able to determine the type of loops to use Be able to avoid common loop problems Be able to debug looping problems when them arise Martin K.-C. Yeh 2
Image of page 2
The Empty Statement A semicolon creates a C++ statement Placing a semicolon after x++ creates the statement: x++; Placing a semicolon after nothing creates an empty statement that compiles but does nothing: cout << "Hello" << endl; ; cout << "Good Bye"<< endl; Martin K.-C. Yeh 3
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Extra Semicolon Placing a semicolon after the parentheses of a for loop creates an empty statement as the body of the loop Example: for(int count=1; count <= 10; count++); cout << "Hello\n"; prints one "Hello", but not as part of the loop! The empty statement is the body of the loop cout << "Hello\n"; is not part of the loop body! Martin K.-C. Yeh 4
Image of page 4
Designing Loops Designing a loop involves designing: The body of the loop The initializing statements The conditions for ending the loop Martin K.-C. Yeh 5
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Which Loop To Use? Choose the type of loop late in the design process First design the loop using pseudocode Translate the pseudocode into C++ The translation generally makes the choice of an appropriate loop clear Martin K.-C. Yeh 6
Image of page 6
Which Loop To Use? Should the loop always execute at least once? Yes  do-while No  while or for Should the loop be count controlled or sentinel controlled? Count  any of the loops ( for is really good for this) Sentinel  while or do-while (usually). Numeric calculations? For loops really good for this, especially when using a variable changed by equal amounts each time the loop iterates Martin K.-C. Yeh 7
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Sums and Products A common task is reading a list of numbers and computing the sum Pseudocode for this task might be: sum = 0; repeat the following this many times get input and store it in next ; sum = sum + next; end of loop This pseudocode can be implemented with a for-loop as shown on the next slide Martin K.-C. Yeh 8
Image of page 8
for-loop for a sum The pseudocode from the previous slide is implemented as: int sum = 0; for(int count=1; count<=thisMany; count++) { cin >> next; sum += next; } (sum must be initialized prior to the loop body!) Martin K.-C. Yeh 9
Image of page 9

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Repeat "this many times" Pseudocode containing the line repeat the following "this many times" is often implemented with a for-loop Example: for(int count=1;count<=numOfRepetition; count++) { [Loop body]; } Martin K.-C. Yeh 10
Image of page 10
for
Image of page 11

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern