11.07.2015 Views

tYSR20

tYSR20

tYSR20

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

64 Part I: Introduction to C++ ProgrammingLooping while a condition is trueThe simplest form of looping statement is the while loop. Here’s what thewhile loop looks like:while(condition){// ... repeatedly executed as long as condition is true}The condition is tested. This condition could be if var > 10 or ifvar1 == var2 or anything else you might think of. If it is true, the statementswithin the braces are executed. Upon encountering the closed brace,C++ returns control to the beginning, and the process starts over. The effectis that the C++ code within the braces is executed repeatedly as long as thecondition is true. (Kind of reminds me of how I get to walk around the yardwith my dog until she . . . well, until we’re done.)If the condition were true the first time, what would make it be false in thefuture? Consider the following example program:// WhileDemo - input a loop count. Loop while// outputting astring arg number of times.#include #include #include using namespace std;int main(int nNumberofArgs, char* pszArgs[]){// input the loop countint loopCount;cout > loopCount;// now loop that many timeswhile (loopCount > 0){loopCount = loopCount - 1;cout

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!