Repetition in C++

Repetition in C++ - RepetitioninC code,bycopyandpaste...

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

View Full Document Right Arrow Icon
Repetition in C++ around and around it goes...
Image of page 1

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

View Full Document Right Arrow Icon
Repetition as a control structure Instructions need to be repeated more than  one time to process lots of data Could do it by just literally repeating the  code, by copy and paste But that isn't flexible, nor is it easy to keep  consistent Need a "loop"
Image of page 2
Two Parts to Repetition a Body what needs to be repeated must be marked off in some fashion like {} a Control expression some way to make the repetition stop when  desired Infinite loops not wanted!
Image of page 3

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

View Full Document Right Arrow Icon
a While statement The body can be either one statement  (doesn't need {} then)  or more than one  statement (a block that has {} around it) Any kind of statement can be in the body of  a loop, including other while statements
Image of page 4
The syntax of a while while (condition) // note no semicolon  { statement; statement; statement;    } Indentation useful for humans, not compiler
Image of page 5

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

View Full Document Right Arrow Icon
The semantics of a while The condition at the top of the loop is  evaluated to see if it is true or false it MUST be - will be coerced to bool ! If the condition is true, the body will be  entered at the top and executed all the way  through Then the condition will be evaluated again,  and the process is repeated
Image of page 6
The semantics of a while (cont'd) If the condition at the top turns out to be 
Image of page 7

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

View Full Document Right Arrow Icon
Image of page 8
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