++ and – are operators that add and subtract 1 from their operands.
The expression num++ is pronounced num plus plus and the expression num—is pronounced num minus minus.
THE DIFFFERENCE BETWEEN POSTFIX AND PREFIX MODES
Cout << num++; postfix mode
The above statement is doing two things, first it is displaying the value of num and then it is incrementing it.
Cout << ++num; prefix mode
In this statement, the increment will happen first.
USING ++ AND – IN MATHEMATICAL EXPRESSIONS
a = 2;
b = 5;
c = a * b++; // c is assigned the value of a times be which is 10, then the variable b is
incremented
Cout << a << “ “ << b << “ “ << c; //This will display 2 6 10
Using ++ and – in relational expressions
X = 10;
If(x++ > 10) // comparison happens first, then its incremented
Cout << “x is greater than 10. \n”; //will not execute
If(++x > 10) // increment happens first, then its compared
1|P a g e
, COS132 Chapter 5
Cout << “x is greater than 10. \n”; //will execute
UNIT 5.2 – INTO TO LOOPS: THE WHILE LOOP
A loop is part of a program that repeats.
THE WHILE LOOP
It has two important parts:
1. An expression that is tested for a true or false value
2. A statement or block that is repeated as long as the expression is true
While(expression) // loop header
Statement; // body of loop, considered a conditionally executed statement
Each repetition of a loop is known as an iteration.
THE WHILE LOOP IS A PRETEST LOOP
This means it tests its expression before each iteration.
INFINITE LOOPS
Continues to repeat the program until the program is interrupted. This can be accidentally created by placing a
semicolon after the first line of the while loop – as it is assumed to be a null statement and disconnects the while
statement from the block that comes after it.
PROGRAMMING STYLE AND THE WHILE LOOP
- If there is only one statement repeated by the loop it should appear on the line after the while statement and be
indented one additional level
- If the loop repeats a block, each line inside the braces should be indented.
UNIT 5.3 – USING THE WHILE LOOP FOR INPUT VALIDATION
2|P a g e
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller jennamortonx. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $5.08. You're not tied to anything after your purchase.