Input validation loop do while repeatedly accept

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

View Full Document Right Arrow Icon
Kinds of loops (cont.) Input validation loop ( do-while ): Repeatedly accept interactive input until a value within a specified range is entered General conditional loop ( while , for ): Repeatedly process data until a desired condition is met Let’s look at examples of each of these loop patterns, and how they are implemented in C
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
6 CptS 121 - 9/30/09 Prof. Chris Hundhausen Counter Loops Pseudocode for a counter loop: Set loop counter to 0 while (loop counter < final count) … (Do data processing) add 1 to loop counter endwhile
Image of page 6
7 CptS 121 - 9/30/09 Prof. Chris Hundhausen Counter Loops (cont.) Implementing Counter Loops : the while loop while (<repitition-condition>) { <body> }
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
8 CptS 121 - 9/30/09 Prof. Chris Hundhausen Counter Loops (cont.) Notes on while loops: <repetition-condition> is evaluated at beginning of loop. If it evaluates to true, the loop body is executed. If it evaluates to false, control shifts to first statement after loop body <body> contains one or more C statements After last statement in <body> is executed, control is shifted back to beginning of loop, and <repetition-condition> is re-evaluated. “Progress” must be made within the loop. That is, something must be done so that <repetition- condition> eventually evaluates to false. Otherwise we have an “infinite loop”
Image of page 8
9 CptS 121 - 9/30/09 Prof. Chris Hundhausen Counter Loops (cont.) Example: Notice that the Tollbooth code segment we looked at earlier can be rewritten as a counter while loop in C: int count; count = 0; /* Initialize counter */ while (count < 3) { axles = read_num_axles(infile); weight = read_weight(infile); toll = compute_toll(axles,weight); display_toll(axles, weight, toll); count = count + 1; /* increment counter */ }
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
10 CptS 121 - 9/30/09 Prof. Chris Hundhausen Counter Loops (cont.) Another alternative for implementing Counter Loops : the for loop for (<initialization>; <repetition-condition>; <update-expression>) { <body> }
Image of page 10
11 CptS 121 - 9/30/09 Prof. Chris Hundhausen Counter Loops (cont.) Notes on for loops: <initialization> statement initializes the loop control variables before loop is executed the first time <repetition-condition> is tested at beginning of loop. If it is true, loop <body> is executed.
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