CS0004: Introduction to Programming Repetition – Do Loops.

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming
Advertisements

CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Repeating Actions While and For 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.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Chapter 5: Control Structures II (Repetition)
Chapter 5: Loops and Files.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Repetition  Some additional operators increment and decrement.
The switch Statement, DecimalFormat, and Introduction to Looping
CS0007: Introduction to Computer Programming Introduction to Arrays.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
Chapter 5: Control Structures II (Repetition)
Lecture Set 5 Control Structures Part D - Repetition with Loops.
Chapter 12: How Long Can This Go On?
LOOPING What are the 3 structures of writing code? sequential decision repetition Def. Looping is the repetition of statements in a program. There various.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
ECE 103 Engineering Programming Chapter 18 Iteration Herbert G. Mayer, PSU CS Status 7/19/2015 Initial content copied verbatim from ECE 103 material developed.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 5A Repetition (Concepts)
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
CS 100 Introduction to Computing Seminar October 7, 2015.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Loops and Files. 5.1 The Increment and Decrement Operators.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
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.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Chapter 6: Loops.
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
The switch Statement, and Introduction to Looping
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.
Chapter 5: Repetition Structures
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Topics Introduction to Repetition Structures
Lecture 07 More Repetition Richard Gesick.
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Chapter 6: Repetition Statements
Let’s all Repeat Together
Introduction to Repetition Structures
Topics Introduction to Repetition Structures
ECE 103 Engineering Programming Chapter 18 Iteration
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
REPETITION Why Repetition?
Presentation transcript:

CS0004: Introduction to Programming Repetition – Do Loops

Review  A condition is…  a statement that can (but does not have to) include relational or logical operators that results to either True or False  Relational Operators…  compare two entities. Returns either True or False.  =, <>,, =  Logical Operators…  combine two or more conditions. Return either True or False.  And, Or, Not

Loops  So far we have used conditions in making decisions that execute a sequence of statements once. If condition Then statement(s) End If  However, we can use conditions to execute a sequence of statements multiple times.  A loop is used to repeatedly execute a sequence of statements a number of times.  Each repetition of the loop is called a pass, or iteration.  Loops can be used for validation, computing naturally repetitive calculations, take in a variable amount of data from the user, or many other tasks.

Do Loops  Do Loops execute the code they contain until a condition is false.  Do Loops come in two forms: Pretest and Posttest.  General Form of Pretest: Do While condition statement(s) Loop 1. First, it checks if condition is true. A. If the condition is false, then the statement(s) are not executed B. If the condition is true, it executes the statement(s) in the loop’s block. a. Then, it goes back to step 1.  Said another way, the statement(s) are executed until condition is false.  This is called Pretest because the condition comes first.

Pretest Do Loop Flowchart Is the condition True? Execute statements within the loop Execute statements that follow the loop No Yes

Pretest Do Loop Examples 1, 2 and 3  New Topics:  Pretest Do Loop  Looping used for validation  Looping used for variable length user input  Notes:  In Example 3:  count is called a counter variable.  Counter Variables count how many times a loop repeats.  sum is called an accumulator variable.  Accumulator Variables reflect the total (accumulation of) work done by all the repetitions done in the loop.  In this case it was the sum of all numbered entered  The loop in this case is called a sentinel-controlled loop.  A Sentinel-Controlled Loop is broken out of when a variable in its condition gets a certain value.  When -1 is entered for num, -1 is called an sentinel value.  Sentinel Values create conditions where a sentinel-controlled loop stops repeating.

Pretest Do Loop Example 4  New Topic:  Looping for calculations  Note: Only use looping for calculations when you cannot easily do the calculations without looping.  How would we do Example 4 without looping?

Posttest Do Loop  General Form of Posttest: Do statement(s) Loop While condition 1. First, it executes the statement(s) 2. Second, it checks the condition A. If the condition is false, then the loop ends B. If the condition is true, it goes back to step 1.  This is called posttest because it checks the condition at the end.

Posttest Do Loop Flowchart Is the condition True? Execute statements within the loop Execute statements that follow the loop No Yes

Posttest Do Loop Example 1  New Topics:  Posttest Do Loop  When to use Posttest

Until Keyword  In both forms of the Do Loop, you can replace While with Until  While loops while the condition is True  Until loops until the condition is True Do While num <> -1 statement(s) Loop Is the same as… Do Until num = -1 statement(s) Loop  Until may make more sense in some sentinel-controlled loops.

Infinite Loops  An infinite loop is a logical error where a loop has no way of reaching the condition where the loop stops executing, and therefore repeats indefinitely.  For example: Dim num As Integer = 1 Do While num < 1 num += 1 Loop  This loop will repeat forever because num will never be less than 1.  This may be a simple example, but they can be more complex and harder to spot. If your program seems to stall at about the time a loop is executing, look to see if you have created an infinite loop.  To stop a program from running while it is in an infinite loop, hit the “Stop Debugging” button.

Closing Notes:  You can end a loop prematurely by using the Exit Do statement in the body of a loop. As soon as Exit Do is executed, execution jumps immediately to the statement following the Loop statement.  Just like with an if statement, variables declared inside of a loop have block-level scope, meaning they cannot be used outside of the loop. Be careful though, these variables are then going to be declared EVERY iteration of the loop.