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

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Repetition – Do Loops.
Advertisements

Loops (Part 1) Computer Science Erwin High School Fall 2014.
Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
True or false A variable of type char can hold the value 301. ( F )
Computer Science 1620 Loops.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Chapter 5: Loops and Files.
Summary of Loops Programming. COMP102 Prog Fundamentals I: Summary of Loops /Slide 2 Which Loop to Use? l for loop n for calculations that are repeated.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
What is the out put #include using namespace std; void main() { int i; for(i=1;i
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
CSE1222: Lecture 7The Ohio State University1. logExample.cpp // example of log(k) for k = 1,2,..,8... int main() { cout
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Overview Go over parts of quiz? Another iteration structure for loop.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
1 do-while Statement 2 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX.
Loops and Files. 5.1 The Increment and Decrement Operators.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Chapter 4 October 22, The If Statement Programs make decisions If(condition){ Statement(s); } Condition  boolean expression Evaluates to either.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Fundamental Programming Fundamental Programming More on Repetition.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
LESSON 5 Loop Control Structure. Loop Control Structure  Operation made over and over again.  Iterate statement.
Lab 4: Loops and Iteration Graham Northup
Introduction to Computer Programming
Chapter 5: Loops and Files.
Programming Fundamentals
Iteration with While You can say that again.
Repetition Statements
Counting Loops.
Repetition Control Structure
Computing Fundamentals
Control Structures Part 1
Repetition Statements (Loops) - 2
PROGRAM FLOWCHART Iteration Statements.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Presentation transcript:

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

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 numbers together and display the sum

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

Design  Flowchart

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

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

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

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