game102 - intro while loops

26
GAME102 - INTRO WHILE LOOPS G. MacKay

Upload: wade-barton

Post on 30-Dec-2015

41 views

Category:

Documents


0 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: GAME102 - Intro WHILE LOOPS

GAME102 - INTROWHILE LOOPS

G. MacKay

Page 2: GAME102 - Intro WHILE LOOPS

Fundamental Control Structures STRAIGHT LINE

CONDITIONAL

LOOPS

Page 3: GAME102 - Intro WHILE LOOPS

What is LOOP?

Logic that repeats itself

Usually verbally identified by “while” or “until”

Page 4: GAME102 - Intro WHILE LOOPS

In human terms?

while (the light is red) wait

while the meat is raw cook

Page 5: GAME102 - Intro WHILE LOOPS

Flowchart of a WHILE LOOP

Note

ONE ENTRANCE

ONE EXIT

from structure

Page 6: GAME102 - Intro WHILE LOOPS

C++ Code of a WHILE LOOP As follows

while (test) statement;

Page 7: GAME102 - Intro WHILE LOOPS

C++ Code for Counter

As follows

count = 1;while (count<=10){ cout << count<< endl; count = count +1;}

Page 8: GAME102 - Intro WHILE LOOPS

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

Page 9: GAME102 - Intro WHILE LOOPS

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

Page 10: GAME102 - Intro WHILE LOOPS

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

Page 11: GAME102 - Intro WHILE LOOPS

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

Page 12: GAME102 - Intro WHILE LOOPS

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

Page 13: GAME102 - Intro WHILE LOOPS

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)

Page 14: GAME102 - Intro WHILE LOOPS

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

Page 15: GAME102 - Intro WHILE LOOPS

PROBLEM using loops

Add 10 numberstogether and display the sum

Page 16: GAME102 - Intro WHILE LOOPS

Requirements?

Read 10 numbers Add the numbers Display sum

Page 17: GAME102 - Intro WHILE LOOPS

Design

Flowchart

Page 18: GAME102 - Intro WHILE LOOPS

Design

Pseudo-codePrompt userSet count to 1Set sum to 0While count <=10 Read number Add number to sum Add to 1 to countDisplay sum

Page 19: GAME102 - Intro WHILE LOOPS

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

Page 20: GAME102 - Intro WHILE LOOPS

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

Page 21: GAME102 - Intro WHILE LOOPS

Testing your code?

In large projects, test groups actually plan tests around the REQUIREMENTS of the project

Page 22: GAME102 - Intro WHILE LOOPS

Testing does not work?

LOGIC ERROR Check that code matches

design FIX code

Check the design is correct FIX design

Page 23: GAME102 - Intro WHILE LOOPS

The OTHER loop…..

do{ statement1; statement2;} while(test);

Page 24: GAME102 - Intro WHILE LOOPS

DO…WHILE Design Difference

PROCESS BLOCK is ALWAYS PERFORMED AT LEAST ONCE

Page 25: GAME102 - Intro WHILE LOOPS

DO…WHILE Design Usage

Do you want to play again?

MAJOR USAGE

Page 26: GAME102 - Intro WHILE LOOPS

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