COS132 Chapter 5
COS 132
CHAPTER 5 – LOOPS AND FILES
UNIT 5.1- THE INCREMENNT AND DECREMENT OPERATORS
++ 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
COS 132
CHAPTER 5 – LOOPS AND FILES
UNIT 5.1- THE INCREMENNT AND DECREMENT OPERATORS
++ 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