Control Structures 2 Chapter 6. © Janice Regan 2003 Basic Loops  When one action is to be repeated a number of times a loop is used. Loops are repetition.

Slides:



Advertisements
Similar presentations
Algorithms 10 IST – Topic 6.
Advertisements

While loops.
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Repetition control structures
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Repeating Actions While and For Loops
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Control Structures for Loops.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
Types of LOOP Structures Do While ……. Loop Do Until …… Loop For …… Next loop.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Chapter 6 - Visual Basic Schneider
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
1 Fall 2008ACS-1903 Ch 4 Loops and Files while loop do-while loop Other topics later on …
1 Fall 2007ACS-1903 Ch 4 Loops and Files Increment & decrement statements while loop do-while loop Other topics later on …
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
1 Chapter 5 File Objects and Looping Statements (Some slides have been modified from their original format)
CHAPTER 5 CONTROL STRUCTURES II (Repetition). In this chapter, you will:  Learn about repetition (looping) control structures  Explore how to construct.
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 4 Objectives  Learn about repetition (looping) control structures.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Chapter 71 Repetition - Do Loops n A Loops, is used to repeat a sequence of statements a number of time n There are two loops commands in Visual Basic.
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.
Chapter 6 Looping CS185/09 - Introduction to Programming Caldwell College.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
CHAPTER 3 CONTROL STRUCTURES ( REPETITION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Loops George Mason University. Loop Structure Loop- A structure that allows repeated execution of a block of statements Loop body- A block of statements;
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Pascal Programming Pascal Loops and Debugging. Pascal Programming Pascal Loops In our first brush with the while do loops, simple comparisons were used.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Looping ROBERT REVAES. Logical Operators  && AND  Both have to be true for it to evaluate to be true.  || OR  One or the other has to be true for.
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
1 Fall 2009ACS-1903 Ch 4 Loops and Files while loop do-while loop for loop Other topics later on …
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students, continue; and break; statements.
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.
CHAPTER 6: REPETITION AND LOOP STATEMENTS Learning outcomes  Define the concept of repetition structure.  Specify.
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.
Follow up from lab See Magic8Ball.java Issues that you ran into.
Identify the Appropriate Method for Handling Repetition
Chapter 7 JavaScript: Control Statements, Part 1
Chapter 9 Repetition.
Topic 4: Looping Statements
Chapter 5: Control Structures II
CHAPTER 6: REPETITION AND LOOP STATEMENTS
Lecture 6 Repetition Richard Gesick.
Chapter 5: Control Structures II
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
CiS 260: App Dev I Chapter 4: Control Structures II.
Java Programming: Guided Learning with Early Objects
Ch 7: JavaScript Control Statements I.
Control Structures - Repetition
Chapter 5 Repetition.
Looping and Repetition
Chapter 9 Control Structures.
MSIS 655 Advanced Business Applications Programming
Introduction to Object-Oriented Programming with Java--Wu
Chapter 8: More on the Repetition Structure
CMPT 102 Introduction to Scientific Computer Programming
Chapter 6: Repetition Statements
Seating “chart” Front - Screen rows Back DOOR.
PROGRAM FLOWCHART Iteration Statements.
Looping and Repetition
Presentation transcript:

Control Structures 2 Chapter 6

© Janice Regan 2003 Basic Loops  When one action is to be repeated a number of times a loop is used. Loops are repetition structures  There are two common types of loops  while loop or do...while loop Used to continue repetition while a condition holds Used to continue repetition while a condition holds  for loop Used to repeat a particular number of times Used to repeat a particular number of times

© Janice Regan 2003 Definition of while Loop while ( condition ) while ( condition ) { /*Series of actions to be taken each time the loop is executed*/ action 1; action 2; }

© Janice Regan 2003 Flowchart for a while loop T F condition Statement 1: Statement n:

© Janice Regan 2003 Definition of do…while Loop do do{ /*Series of actions to be taken each time the loop is executed*/ action 1; action 2; } while ( condition );

© Janice Regan 2003 Flowchart for a do…while loop T F condition Statement 1: Statement n: T Loop condition

© Janice Regan 2003 Definition FOR Loop for ( initial statement ; loop condition ; update statement) for ( initial statement ; loop condition ; update statement){ /*Series of actions to be taken each time the loop is executed*/ action 1; action 2; }

© Janice Regan 2003 Flowchart for a for loop T F Condition LoopIndex+=step LoopIndex=Start; Statement 1: Statement n: Initial statement Update statement Loop condition Statements in body of loop

© Janice Regan 2003 Control of while loops  Counter controlled  Sentinel controlled  Flag controlled  EOF controlled

© Janice Regan 2003 Sentinel Controlled while loops  Sentinel value is a special value of a variable in the loop condition that makes the loop condition false.  The sentinel value of a sentinel variable must be a value of the variable that is not encountered in normal operation of the loop  When the sentinel variable has the sentinel value at the end of the statements in the loop, execution will pass to the next statement following the loop  Often used for reading unknown amounts of input data

© Janice Regan 2003 Sentinel Controlled while loop while ( sentinel variable != sentinel value ) { //Series of actions to be taken //each time the loop is executed //one of these actions will change the value // of the sentinel variable action 1; action 2; }

© Janice Regan 2003 Flag Controlled while loops  Uses a boolean variable to control the loop  The boolean value is set before the loop begins to execute  At some point a condition within the loop is met and the value of the boolean variable is changed  The next time the loop condition is tested execution passes to the statement following the loop

© Janice Regan 2003 Flag Controlled while loop flagvalue = false; while ( !flagvalue ) { //Series of actions to be taken //each time the loop is executed action 1; if(expression) flagvalue = true; action n; }

© Janice Regan 2003 End of File (EOF) Controlled while loops  Uses an end of file condition to control the loop  The end of file condition occurs when the program attempts to read data after the end of the file is reached

© Janice Regan 2003 End of File Controlled while loop inputLine = inFile.readLine(); while ( inputLine != null ) { //Series of actions to be taken //each time the loop is executed tokenizer = new StringTokenizer(inputLine); action 1; action n; inputLine = inFile.readLine(); }

© Janice Regan 2003 End of File Controlled while loop nextValue = inFile.read(); while ( nextvalue != -1 ) { //Series of actions to be taken //each time the loop is executed ch = (char)nextValue; action 1; action n; inputLine = inFile.read(); }