Repetition in C++

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

Info iconThis 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. ..
Background image of page 1

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

View Full DocumentRight 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"
Background 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!
Background image of page 3

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

View Full DocumentRight 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
Background image of page 4
The syntax of a while while (condition) // note no semicolon  { statement; statement; statement;    } Indentation useful for humans, not compiler
Background image of page 5

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

View Full DocumentRight 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
Background image of page 6
The semantics of a while (cont'd) If the condition at the top turns out to be 
Background image of page 7

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

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/13/2011 for the course CS 115 taught by Professor Staff during the Fall '08 term at Kentucky.

Page1 / 24

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

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online