260 likes | 339 Views
GAME102 - Intro WHILE 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.
E N D
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) waitwhile the meat is raw cook
Flowchart of a WHILE LOOP • NoteONE ENTRANCEONE EXITfrom structure
C++ Code of a WHILE LOOP • As followswhile (test) statement;
C++ Code for Counter • As followscount = 1;while (count<=10){cout << count<<endl; count = count +1;}
C++ Code for Counter • As followscount = 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 followscount = 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 followscount = 1;while (count<=10){cout << count<<endl; count ++;} Operator ++ Means add one to the variablecount = count + 1;count ++;// SAME!!!
C++ Code for Counter - PROBLEM • As followscount = 1;while (count<=10)cout << count<<endl; count = count +1; What happens? • There are no braces, so ONE statement goes with the whileIncrement is OUTSIDE the while • INFINITE LOOP!!!!!
C++ Code for Counter - PROBLEM • As followscount = 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 followscount = 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 followscount = 1;while (count<=10){cout << count<<endl; count += 2;} Operator += Means add whatever is on the right side to whatever is on the left sidecount = 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-code Prompt user Set count to 1 Set sum to 0 While count <=10 Read number Add number to sum Add to 1 to count Display 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