Iteration with While You can say that again.

Slides:



Advertisements
Similar presentations
Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
Advertisements

Computer Science 1620 Loops.
1 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Chapter 5: Control Structures II (Repetition)
Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 5: Control Structures II (Repetition)
What is the out put #include using namespace std; void main() { int i; for(i=1;i
Chapter 5: Control Structures II (Repetition)
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
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.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS.
Chapter 5: Control Structures II
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Overview Go over parts of quiz? Another iteration structure for loop.
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
1 Looping Chapter 6 2 Getting Looped in C++ Using flags to control a while statement Trapping for valid input Ending a loop with End Of File condition.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Follow up from lab See Magic8Ball.java Issues that you ran into.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
Structured Programming The Basics
REPETITION CONTROL STRUCTURE
Chapter 5: Control Structures II (Repetition)
Lecture 6 Repetition Richard Gesick.
CS161 Introduction to Computer Science
Lesson 05: Iterations Class Chat: Attendance: Participation
Chapter 5: Control Structures II
Control Structures II (Repetition)
Chapter 5: Loops and Files.
Incrementing ITP © Ron Poet Lecture 8.
Lecture 4A Repetition Richard Gesick.
For Loops October 12, 2017.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Looping and Repetition
Loops October 10, 2017.
Outline Altering flow of control Boolean expressions
Loops A loop is a repetition control structure.
LOOPS BY: LAUREN & ROMEO.
Module 4 Loops.
Loop Strategies Repetition Playbook.
Control Structures Part 1
Seating “chart” Front - Screen rows Back DOOR.
Repetition Statements (Loops) - 2
Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 14, 2011
General Condition Loop
Based on slides created by Bjarne Stroustrup & Tony Gaddis
PROGRAM FLOWCHART Iteration Statements.
Presentation transcript:

Iteration with While You can say that again

Problem You want to add 100 numbers

Problem 100 variables? int a,b,c,d…. cin >> a; cin >> b; … int answer = a + b + c…

Problem Use one variable 100 times? int num, answer = 0; cin >> num; answer += num; cin >> num; answer += num; …

Golden Rule of Programming If you write the same code more than once you are probably doing it wrong.

Problem Repeat to eliminate duplication: int num, answer = 0; Repeat 100 times cin >> num; answer += num;

While While statement expression Boolean expression statement is the body of the loop Usually compound using { } After executing, test expression again

While Sample Sample loop Counts 1 to 100 Happens once Happens 0+ times

While Sample Sample loop Expression : boolean expression True – execute body, then check again False – done; skip to end of loop

Infinite Loop Condition is always true : infinite loop Ctrl-C breaks a program

While Sample Sample loop Expression : boolean expression Usually tests a variable (loop control variable)

Loop Control Variables i, j, k : accepted identifiers for loop control variables

While Sample Sample loop Update: change loop control variable "Go to next step" Do as first or last thing in loop body

Infinite Loop No update to LCV = infinite loop int i = 0; while(i < 20) { cout << i << " "; //i never changes!!! }

Counting Example Start at 10, count by 5's, count until 50

Making a list Formatting numbers as a list: version 1

Making a list Formatting numbers as a list: version 2

Few Problems are Unique Pattern solving is pattern recognition

Patterns of Loop Use Typical use patterns Total some values Average some values

Total Total something up: Declare variable before Add current value in loop Use total after

Average Find average Get total Get count Use counting variable After loop, divide total by count

Average Find average - correct