While Loops 10/14/13. Repetition Allows a program to do tasks over and over. Very powerful because the computer is fast and accurate.

Slides:



Advertisements
Similar presentations
Chapter 2: Basic Elements of C++
Advertisements

Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Chapter 04 (Part III) Control Statements: Part I.
Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
Start. More Loops 03/14/11 Look at geometric series example.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Repetition Repetition Important  Do things over and over thousands of times.  Computer fast, accurate and doesn't get bored. Example  count.c.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
1 Engineering Problem Solving with C++ An Object Based Approach Chapter 2 Simple C++ Programs.
Copyright © Texas Education Agency, Computer Programming For Loops.
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.
For Loops Programming. COMP102 Prog Fundamentals I: for Loops/Slide 2 The for Statement condition action true false initialization update.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved For Loops October 16, 2013 Slides by Evan Gallagher.
ECE 264 Object-Oriented Software Development Instructor: Dr. Michael Geiger Spring 2009 Lecture 2: Basic C++ Programs.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
 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.
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 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Chapter 6 Looping CS185/09 - Introduction to Programming Caldwell College.
1 Three C++ Looping Statements Chapter 7 CSIS 10A.
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.
Control Structures RepetitionorIterationorLooping Part I.
Overview Go over parts of quiz? Another iteration structure for loop.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Fundamental Programming Fundamental Programming More on Repetition.
2/4/2016Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
Chapter 3 – Variables and Arithmetic Operations. First Program – volume of a box /************************************************************/ /* Program.
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
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 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
Algorithms JPC and JWD © 2002 McGraw-Hill, Inc. 2 Algorithms 2 An Algorithm is a finite set of precise instructions for performing a computation or for.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
1 ICS103 Programming in C Lecture 7: Repetition Structures.
Chapter 5: Looping. Using the while Loop Loop – A structure that allows repeated execution of a block of statements Loop body – A block of statements.
LESSON 5 Loop Control Structure. Loop Control Structure  Operation made over and over again.  Iterate statement.
Computer Programming -1-
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
REPETITION CONTROL STRUCTURE
CHAPTER 6: REPETITION AND LOOP STATEMENTS
CS161 Introduction to Computer Science
for Repetition Structures
CHAPTER 5A Loop Structure
Chapter 5: Loops and Files.
Repetition Structures (Loops)
Chapter 4 Repetition Structures
Chapter 8 Repetition Statements
Loops October 10, 2017.
Iteration with While You can say that again.
Outline Altering flow of control Boolean expressions
Unary Operators ++ and --
Repetition Control Structure
Chapter 6: Repetition Statements
Let’s all Repeat Together
Repetition Statements (Loops) - 2
PROGRAM FLOWCHART Iteration Statements.
Statements in C Programming
Types of loops definite loop: A loop that executes a known number of times. Examples: Repeat these statements 10 times. Repeat these statements k times.
Programming Fundamental
Presentation transcript:

While Loops 10/14/13

Repetition Allows a program to do tasks over and over. Very powerful because the computer is fast and accurate.

Control Variable Loop – repeated portion of a program Control Variable – Variable whose value determines if body of loop is executed Example: ch4/manyHellos.cpp

Loop Components Needed Initialization of loop control variable –cin >> count_down Evaluation of loop control variable – while (count_down > 0) Update loop control variable –count_down--;

Other Definitions Loop Body – statements repeated Iteration – repetition of a loop body Example – ch4/odds.cpp

Other Definitions Infinite Loop – loop that repeats indefinitely – stop with cntl-C

Fix odds.cpp

Syntax of while-loop while (bool expression)‏ stmt;

Syntax of while-loop while (bool expression)‏ { stmt1; stmt2;. stmtn; }

01/27/1210 Increment and Decrement Operators  Increment Operator ++ post incrementx++; pre increment++x;  Decrement Operator - - post decrementx- -; pre decrement- -x;  For examples assume k=5 prior to executing the statement. m= ++k;both m and k become 6 n = k- -; n becomes 5 and k becomes 4

01/27/12Engineering Problem Solving with C++, Etter,Ingber Second Edition 11 Abbreviated Assignment Operators operatorexampleequivalent statement +=x+=2; x=x+2; -=x-=2;x=x-2; *=x*=y;x=x*y; /=x/=y;x=x/y; %=x%=y;x=x%y;

Find the Sum of Ten Numbers. Write a program that uses a while loop to input 10 numbers then output their sum. First, let's write an algorithm.

Hand Tracing It can be useful to step through loops, keeping track of the variables as you do. int i = 2; int n = 4; while(i <= n){ cout << i; i++; }

Hand Tracing It can be useful to step through loops, keeping track of the variables as you do. Example: trace.cpp What does the loop do for arbitrary values of a and r?