Presentation is loading. Please wait.

Presentation is loading. Please wait.

GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS.

Similar presentations


Presentation on theme: "GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS."— Presentation transcript:

1 GAME102 - INTRO WHILE 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) wait while the meat is raw cook

5 Flowchart of a WHILE LOOP  Note ONE ENTRANCE ONE EXIT from structure

6 C++ Code of a WHILE LOOP  As follows while (test) statement;

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

8 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

9 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

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

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

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

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

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

15 PROBLEM using loops  Add 10 numbers together and display the sum

16 Requirements?  Read 10 numbers  Add the numbers  Display sum

17 Design  Flowchart

18 Design  Pseudo-codePrompt 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 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 AND limits

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


Download ppt "GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS."

Similar presentations


Ads by Google