1 / 26

GAME102 - Intro WHILE LOOPS

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.

wade-barton
Download Presentation

GAME102 - Intro WHILE LOOPS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. GAME102 - IntroWHILE LOOPS G. MacKay

  2. Fundamental Control Structures • STRAIGHT LINE • CONDITIONAL • LOOPS

  3. What is LOOP? • Logic that repeats itself • Usually verbally identified by “while” or “until”

  4. In human terms? • while (the light is red) waitwhile the meat is raw cook

  5. Flowchart of a WHILE LOOP • NoteONE ENTRANCEONE EXITfrom structure

  6. C++ Code of a WHILE LOOP • As followswhile (test) statement;

  7. C++ Code for Counter • As followscount = 1;while (count<=10){cout << count<<endl; count = count +1;}

  8. 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

  9. 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

  10. 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!!!

  11. 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!!!!!

  12. 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!!!

  13. 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)

  14. 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!!!

  15. PROBLEM using loops • Add 10 numberstogether and display the sum

  16. Requirements? • Read 10 numbers • Add the numbers • Display sum

  17. Design • Flowchart

  18. 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

  19. 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; }

  20. 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

  21. Testing your code? • In large projects, test groups actually plan tests around the REQUIREMENTS of the project

  22. Testing does not work? • LOGIC ERROR • Check that code matches design • FIX code • Check the design is correct • FIX design

  23. The OTHER loop….. • do{ statement1; statement2;} while(test);

  24. DO…WHILE Design Difference • PROCESS BLOCK is ALWAYS PERFORMED AT LEAST ONCE

  25. DO…WHILE Design Usage • Do you want to play again?MAJOR USAGE

  26. 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

More Related