Repetition Statements

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 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.
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.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
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.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
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.
For Loops Programming. COMP102 Prog Fundamentals I: for Loops/Slide 2 The for Statement condition action true false initialization update.
CONTROLLING PROGRAM FLOW
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
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.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
Review the following : Flowcharting Variable declarations Output Input Arithmetic Calculations Conditional Statements Loops.
1 Chapter 4: Basic Control Flow ► Chapter Goals  To be able to implement decisions using if statements  To understand statement blocks  To learn how.
Fundamental Programming Fundamental Programming for Loops.
Lecture 7: Making Decisions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Overview Go over parts of quiz? Another iteration structure for loop.
 for loop  while loop  do-while loop for (begin point; end point ; incrementation ) { //statements to be repeated }
Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types.
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 FIRST BOOK OF C++ CHAPTER 5 REPETITION. OBJECTIVES In this chapter, you will learn about: The while Statement Interactive while Loops The for Statement.
A First Book of C++ Chapter 5 Repetition.
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.
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.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
C++ Programming: CS102 LOOP. Not everything that can be counted counts, and not every thing that counts can be counted. −Albert Einstein Who can control.
Computer Programming -1-
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Sentinel Loops. while Syntax while(expression) statement.
Introduction to Computer Programming
Topic 4: Looping Statements
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
while Repetition Structure
Unit 3 Lesson 9 Repetition Statements (Loops)
C++ Programming: CS150 For.
Intro to Programming Week # 6 Repetition Structure Lecture # 10
Control Structures II (Repetition)
Chapter 2.2 Control Structures (Iteration)
Repetition Structures (Loops)
Programming Fundamentals
For & do/while Loops.
Introduction to Programming
Conditional Construct
Repetition Control Structure
Chapter 2.2 Control Structures (Iteration)
Flow Control Statements
Control Structures Part 1
Looping III (do … while statement)
Let’s all Repeat Together
do/while Selection Structure
Fundamental Programming
Repetition Statements (Loops) - 2
PROGRAM FLOWCHART Iteration Statements.
Presentation transcript:

Repetition Statements The question being asked is "Should I execute the statement block AGAIN?" All repetition statements use a boolean expression to make the decision. 3 Repetition statements while for do / while "while" is the general all-purpose looper. The other two are special purpose.

while( boolean expression ) statement; { statement1; statement2; statement3; } int counter = 1; . while( counter < 10 ) cout << "My name is Lee." << endl; counter++; { }

cout << "Enter a value below 100 to start with: "; double start; cout << "Enter a value below 100 to start with: "; cin >> start; while( start < 100 ) start *= 1.1; cout << "The final value of start is: " << start << endl; How many times is the loop going to execute? Don't know, it depends on the starting value. BUDDY SAGA. loopcount.cpp loopcount1.cpp loopflag.cpp loopsentinel.cpp NEXT: LoopBADSentinel.cpp

#include <iostream.h> int main() { double sqMeters = 1, //Input by user sqYards; //Calculated while( sqMeters != 0 ) cout << "Enter number of square meters: "; cin >> sqMeters; //Apply the conversion factor and report. sqYards = sqMeters * 1.196; cout << "Number of square yards: " << sqYards << endl; } return 0; Next – "for" loop

for( initialization ; boolean expression ; incrementing action ) statement block; int counter; for( counter = 0; counter < 10; counter++ ) cout << "This is line number " << counter + 1 << endl; int reps, total; for (reps = 0; reps <= 6; reps++) { total = reps * 8; cout << total << endl; } Counter control only

while( <boolean expression> ); do statement; while( <boolean expression> ); do { statement1; statement2; . statementn; } while( <boolean expression> ); When the code in the statement block MUST execute at least one time.

int count = 0; do { cout << "Count: " << count << " " << count * count << endl; count++; } while( count < 20 ); int count = 0; do cout << "This is line " << ++count << endl; while( count < 20 );

Selecting the Correct Repetition Statement if( the statement block must execute at least one time ) Select the do/while else if( it's pure counter-control ) Select the for else //for anything else Select the while