Repetition Chapter 7. Overview u For Loop u Do Loop  Do While Loop  Do Until Loop  Do Loop While  Do Loop Until u Nested Loops.

Slides:



Advertisements
Similar presentations
Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Advertisements

Looping Structures: Do Loops
Programming with Microsoft Visual Basic 2008 Fourth Edition
Programming with Microsoft Visual Basic th Edition
CS0004: Introduction to Programming Repetition – Do Loops.
Repeating Actions While and For Loops
Repetition Chapter 4: Control structures. Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 2 Loop Statements After reading and studying this Section,
Iteration (Looping Constructs in VB) Iteration: Groups of statements which are repeatedly executed until a certain test is satisfied Carrying out Iteration.
Do/Loops A loop repeats a series of instructions. An iteration is a single execution of the statement(s) in the loop. Used when the exact number of iterations.
Iteration Statements Lather Rinse Repeat. Three Iteration Statements For Each For Next While.
Iteration (Looping Constructs in VB) Iteration: Groups of statements which are repeatedly executed until a certain test is satisfied Carrying out Iteration.
Public Class Form1 Inherits System.Windows.Forms.Form Private Sub bntRedDemo_Click(…. Sleep(5000) bntRedDemo.Text = "You waited 5 seconds for this?" End.
Chapter 5: Control Structures II (Repetition)
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Repetition  Some additional operators increment and decrement.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
5.05 Apply Looping Structures
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
110-K1 Iterations (1) Up to now, need to call the procedure again (e.g. click again on a command button) To repeat a piece of code: Can also use loops:
Lecture Set 5 Control Structures Part D - Repetition with Loops.
CIS 115 Lecture 8. There are 3 control structures common to most computer languages that determine the flow, or path of execution, of the code:  Sequential.
Chapter 12: How Long Can This Go On?
Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do While and Do Until Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops.
Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops.
The Repetition Process in Visual Basic. The Repetition Process The capability to repeat one or more statements as many times as necessary is what really.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for 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.
ISAT 252 Visual Basic Repetition. Assignment Should have read Chapter 5 ( ) on loops Do tutorial 5-4.
Compunet Corporation1 Programming with Visual Basic.NET While, Do and For – Next Loops Week 5 Tariq Ibn Aziz.
CSC 162 Visual Basic I Programming. Repetition Structures Pretest Loop –Exit condition is tested before the body of code is executed Posttest Loop –Exit.
Chapter 6: The Repetition Structure
Chapter 4 Looping Statements Adapted From: Starting Out with Visual Basic 2008 (Pearson)
Visual Basic.net Loops. Used to do multiple executions of the same block of code Do while loops Do until loops For next loops.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 5A Repetition (Concepts)
1 Chapter 9. To familiarize you with  Simple PERFORM  How PERFORM statements are used for iteration  Options available with PERFORM 2.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Counting Loops.
1 do-while Statement 2 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX.
For…Next Loops, Checked List Boxes, and Combo Boxes Chapter 5.
Tutorial 6: The Repetition Structure1 Tutorial 6 The Repetition Structure.
CSC 162 Visual Basic I Programming. Repetition Structures Pretest Loop –Exit condition is tested before the body of code is executed Posttest Loop –Exit.
Controlling Program Flow with Looping Structures
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.
Fourth Quarter.  Involves loops or cycles ◦ Loops: means that a process may be repeated as long as certain condition remains true or remains false. ◦
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
Chapter 5 - VB 2008 by Schneider1 Chapter 5 – Repetition 5.1 Do Loops 5.2 Processing Lists of Data with Do Loops 5.3 For...Next Loops.
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.
UNIT 5 Lesson 15 Looping.
Programming Logic and Design Fourth Edition, Comprehensive
CHAPTER 5A Loop Structure
Lists, Loops, Validation, and More
Chapter 5: Repetition Structures
JavaScript: Control Statements I
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
Outline Altering flow of control Boolean expressions
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Chapter (3) - Looping Questions.
Iteration: Beyond the Basic PERFORM
Repetition Control Structure
Chapter 6: Repetition Statements
Case & Repetitive Statements
Presentation transcript:

Repetition Chapter 7

Overview u For Loop u Do Loop  Do While Loop  Do Until Loop  Do Loop While  Do Loop Until u Nested Loops

Repetition u Computers are good at doing tasks humans find tedious u Examples:  Determine if the letter 'a' appears in a string  Find the sum of the numbers from 1 to  Calculate pi to 1000 digits  Calculate monthly loan payments

Types of Loops u Using VB, we can tell a computer to repeat a specific set of instructions  A predetermined number of times  For loop  While (or until) some conditional statement is true  Do loop

For Loop u A For Loop is a structure that is repeated a fixed number of times u We usually select a For Loop when the loop starts at a specific value, increments by a set amount, and terminates at a specific value u The syntax for the For Loop: For LoopCounter = InitialValue to TerminatingValue Program Statement(s) Next LoopCounter

For Loop – Example (1) Default Increment Value of 1 Private Sub CmdOutput_Click() Dim intCounter as Integer Dim intSum as Integer intSum = 0 For intCounter = 1 to 5 intSum = intSum + intCounter Next intCounter MsgBox intSum End Sub intCounterintSum 0?/ / / / // // // intSum = 15 after the loop is executed

Private Sub CmdOutput_Click() Dim intCounter as Integer Dim intSum as Integer intSum = 0 For intCounter = 1 to 5 Step 2 intSum = intSum + intCounter Next intSum MsgBox intSum End Sub intCounterintSum ?0/ 1 / 1/ 3 / 4/ 5 / 9 intSum = 9 after the loop is executed For Loop – Example (2) Incrementing by Values Other than One

For Loop – Example (3) Decrementing the Loop Counter Private Sub CmdOutput_Click() Dim intCounter as Integer Dim intSum as Integer intSum = 0 For intCounter = 5 to 1 Step -2 intSum = intSum + intCounter Next intSum MsgBox intSum End Sub intCounterintSum ?0/ 5 / 5/ 3 / 8/ 1 / 9 intSum = 9 after the loop is executed

Do Loops u Do While Loops are used when you are repeating a process that is not controlled by counting with a fixed-step amount, as in a For Loop u Use this construct when your loop will continue while a certain condition evaluates to True u There are four formats that you may use Do Loops with in VB

First Do Loop Construct u This format is used when you wish to test if a condition evaluates to True, before you execute the program statements, and to continue to execute the statement while the condition evaluates to True. Do While (Condition) Program statement(s) Loop

Do While Loop intAnswer = 0 Do While (intAnswer <> vbYes) intAnswer = Msgbox ("Continue?",_ vbYesNo) Loop 'Pretest loop

Second Do Loop Construct u This is used when you wish the loop to execute until a given condition evaluates to True Do Until (Condition) Program statement(s) Loop

Do Until Loop intAnswer = 0 Do Until (intAnswer = vbYes) intAnswer = Msgbox ("Continue?",_ vbYesNo) Loop 'Pretest loop

Third Do Loop Construct u Another form of the Do Loop is used when you wish to execute the program statements at least once and continue to execute the statement while the condition evaluates to True. Do Program statement(s) Loop While (Condition)

Do Loop While Loop Do intAnswer = Msgbox ("Continue?",_ vbYesNo) Loop While (intAnswer <> vbYes) 'Post-test loop

Fourth Do Loop Construct u The final looping construct is used when you wish to execute the program statements at least once, and continue to execute them until the given condition evaluates to True. Do Program statement(s) Loop Until (Condition)

Do Loop Until Loop Do intAnswer = Msgbox ("Continue?",_ vbYesNo) Loop Until (intAnswer = vbYes) 'Post-test loop

Nested Loops u When a loop is contained within the body of another loop, it is said to be nested  Like nesting dolls, hollow wooden dolls that fit one inside of the other u The body of the inner loop is executed many times

Nested Loops intAcc = 0 For intI = 1 to 20 For intJ = 1 to 5 intAcc = intAcc + 1 Next intJ Next intI

Nested Loops intAcc = 0 For intI = 0 to 20 Step 4 For intJ = 100 to 5 Step -16 intAcc = intAcc + 1 Next intJ Next intI