Loops - Loops Slides adapted from the lecture slides for Starting Out with C From Control Structures through Objects Tony Gaddis Pearson Education The

Loops - Loops Slides adapted from the lecture slides for...

This preview shows page 1 - 14 out of 55 pages.

Loops Slides adapted from the lecture slides for Starting Out with C++: From Control Structures through Objects, Tony Gaddis, Pearson Education
Image of page 1
The Increment and Decrement Operators ++ is the increment operator. It adds one to a variable. val++; is the same as val = val + 1; ++ can be used before (prefix) or after (postfix) a variable: ++val; val++;
Image of page 2
The Increment and Decrement Operators -- is the decrement operator. It subtracts one from a variable. val--; is the same as val = val - 1; -- can be also used before (prefix) or after (postfix) a variable: --val; val--;
Image of page 3
Increment and Decrement Operators in Program 5-1 Continued…
Image of page 4
Increment and Decrement Operators in Program 5-1
Image of page 5
Prefix vs. Postfix ++ and -- operators can be used in complex statements and expressions In prefix mode ( ++val, --val ) the operator increments or decrements, then returns the value of the variable In postfix mode ( val++, val-- ) the operator returns the value of the variable, then increments or decrements
Image of page 6
Prefix vs. Postfix - Examples int num, val = 12; cout << val++; // displays 12, // val is now 13; cout << ++val; // sets val to 14, // then displays it num = --val; // sets val to 13, // stores 13 in num num = val--; // stores 13 in num, // sets val to 12
Image of page 7
Introduction to Loops: The while Loop Loop : a control structure that causes a statement or statements to repeat General format of the while loop: while ( expression ) statement ; statement ; can also be a block of statements enclosed in { }
Image of page 8
The while Loop – How It Works while ( expression ) statement ; expression is evaluated if true , then statement is executed, and expression is evaluated again if false , then the loop is finished and program statements following statement execute
Image of page 9
The Logic of a while Loop
Image of page 10
The while loop in Program 5-3
Image of page 11
How the while Loop in Program 5-3 Lines 9 through 13 Works
Image of page 12
Flowchart of the while Loop in Program 5-3
Image of page 13
Image of page 14

You've reached the end of your free preview.

Want to read all 55 pages?

  • Fall '08
  • Staff
  • Control flow, loop, do-while loop

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes