View
213
Download
0
Category
Tags:
Preview:
Citation preview
GAME102 - INTROWHILE LOOPS
G. MacKay
Fundamental Control Structures STRAIGHT LINE
CONDITIONAL
LOOPS
What is LOOP?
Logic that repeats itself
Usually verbally identified by “while” or “until”
In human terms?
while (the light is red) wait
while the meat is raw cook
Flowchart of a WHILE LOOP
Note
ONE ENTRANCE
ONE EXIT
from structure
C++ Code of a WHILE LOOP As follows
while (test) statement;
C++ Code for Counter
As follows
count = 1;while (count<=10){ cout << count<< endl; count = count +1;}
C++ Code for Counter
As follows
count = 1;while (count<=10){ cout << count<< endl; count = count +1;}
NOTE:
initialize count BEFORE LOOP
INCREMENT count INSIDE loop
TEST FOR COUNT
C++ Code for Counter
As follows
count = 1;while (count<=10){ cout << count<< endl; count = count +1;}
LOOP RULE Test should make
progress towards completion
WATCH FOR Incorrect test
direction No increment
C++ Code for Counter – ++ As follows
count = 1;while (count<=10){ cout << count<< endl; count ++;}
Operator ++
Means add one to the variable
count = count + 1;count ++;// SAME!!!
C++ Code for Counter - PROBLEM As follows
count = 1;while (count<=10) cout << count<< endl; count = count +1;
What happens? There are no
braces, so ONE statement goes with the while
Increment is OUTSIDE the while
INFINITE LOOP!!!!!
C++ Code for Counter - PROBLEM As follows
count = 1;while (count>=10){ cout << count<< endl; count = count +1;}
What happens? The test direction
is incorrect
When count=1, test is FALSE so the loop is not entered!!!
C++ Code for Counter – by twos As follows
count = 1;while (count<=10){ cout << count<< endl; count = count +2;}
What happens? When count=1, test
is TRUE so the loop is entered.
Display 1 Count becomes 3…
and Display 3 Then.. 5, 7, 9 EXIT… (on 11)
C++ Code for Counter – by twos As follows
count = 1;while (count<=10){ cout << count<< endl; count += 2;}
Operator +=
Means add whatever is on the right side to whatever is on the left side
count = count + 2;count += 2;// SAME!!!
PROBLEM using loops
Add 10 numberstogether and display the sum
Requirements?
Read 10 numbers Add the numbers Display sum
Design
Flowchart
Design
Pseudo-codePrompt userSet count to 1Set sum to 0While count <=10 Read number Add number to sum Add to 1 to countDisplay sum
CODING #include <iostream>
using namespace std;
int main(){ int number; int sum; int count;
cout << "Enter 10 integer numbers: "; count = 1; sum = 0; while ( count <= 10) { cin >> number; sum += number; count++; } cout << "Sum is " << sum << endl; return 0;}
Testing your code?
Identify the limits of your code Maximum number of reads Minimum number of reads Illegal reads High values entered Low values entered
Test NORMAL case ANDlimits
Testing your code?
In large projects, test groups actually plan tests around the REQUIREMENTS of the project
Testing does not work?
LOGIC ERROR Check that code matches
design FIX code
Check the design is correct FIX design
The OTHER loop…..
do{ statement1; statement2;} while(test);
DO…WHILE Design Difference
PROCESS BLOCK is ALWAYS PERFORMED AT LEAST ONCE
DO…WHILE Design Usage
Do you want to play again?
MAJOR USAGE
DO…WHILE Design Usage
Studies have shown that 90% of all programming loops are WHILE type rather than DO…WHILE
Use DO…WHILE only when it makes sense
Recommended