Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.

Slides:



Advertisements
Similar presentations
Looping Structures: Do Loops
Advertisements

CS0004: Introduction to Programming Repetition – Do Loops.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Repeating Actions While and For Loops
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
Loops Counting down from N to 1 in assembly we used JUMP statements to repeat previous instructions thus looping READ LOOP: WRITE SUB decrement JPOSLOOP.
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 Do..Loop terminates based on a condition that is specified Execution of a Do..Loop continues while a condition is True or until a condition.
Types of LOOP Structures Do While ……. Loop Do Until …… Loop For …… Next loop.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Chapter 61 Post Test Loop Do statement(s) Loop Until condition Loop is executed once and then the condition is tested. If it is False, the loop is run.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
An Introduction to Programming with C++ Fifth Edition
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
5.05 Apply Looping Structures
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
© 1999, by Que Education and Training, Chapter 7, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly 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?
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 4 Objectives  Learn about repetition (looping) control structures.
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.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
Python Repetition. We use repetition to prevent typing the same code out many times and to make our code more efficient. FOR is used when you know how.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Tutorial 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.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Count and add list of numbers From user input and from file.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
CS 100 Introduction to Computing Seminar October 7, 2015.
Looping Structures Do Loops, For Next Do...Loop While structures check the condition after executing the code and repeat a code block until the test.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
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
Lab 6 (1) Range Review, Control Logic and Loops ► Control Logic and Loops ► Exercise.
Intro to Loops 1.General Knowledge 2.Two Types of Loops 3.The WHILE loop 1.
Lecture 7: Menus and getting input. switch Multiple-selection Statement switch Useful when a variable or expression is tested for all the values it can.
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.
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.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition.
Pendalaman Materi Conditional dan Repetition (Pengulangan)
Looping Structures. A B dialog box that pops up and prompts the user for input Text area that pops up and prompts the user for to wait while it gets.
Control Structures WHILE Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Chapter 6 Controlling Program Flow with Looping Structures.
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.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
Chapter 5: Control Structures II
Tutorial 10 – Class Average Application Introducing the Do…Loop While and Do…Loop Until Repetition Statements Outline Test-Driving the Class Average.
Chapter 5: Control Structures II
Lists, Loops, Validation, and More
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Repetition-Counter control Loop
Lecture 07 More Repetition Richard Gesick.
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
حلقات التكرار.
Chapter (3) - Looping Questions.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Repetition Statements (Loops) - 2
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.
Presentation transcript:

Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True

3 Requirements for a Successful Loop Initialize a Loop Control Variable Test the Loop Control Variable against an Exit Condition Update the Loop Control Variable Must be approaching exit criteria

Counted / For Loop Specified Number of Times Syntax: For loopVariable = start To End [Step StepValue] Statements Next [loopVariable] [ ] means optional

Example For counter = 1 to 10 Step 1 msgBox (“The current number is: “ & counter) Next

Try It Write a program that uses a counted/for loop to read in a 5 numbers (using an inputBox) and display their average in a label.

Try it Again Write a program using a counted/for loop that will add the numbers from 1 – 50 and display the output in a label. Write a program using a counted/for loop that will add the odd numbers from 1 – 50 and display the output in a label.

While Loops Continue Looping While a Condition is True Syntax: Do While (condition statement) statements Loop

Pre-Test Loop In a While Loop the Loop Exit condition is tested before the program enters the loop Pre-Test Loop If Condition is False, the Loop code may never execute Minimum number of executions of loop is 0

Example Do While (number <=10) MsgBox (“The current number is: “ & number) number = number + 1 Loop

Try It Write a program to require the user to enter a correct password (“Friday”). When they enter the correct password the program will display “Have a Great Week-end”.

Try it Again Write a program that will calculate the number of years it will take for a given input deposit amount to increase to a million dollars at 6% annual interest. Test  $100,000 should take 40 years

Do Until Loop Continues Executing Until a Condition is True Syntax Do statements Loop Until (condition)

Post Test Loop In a Do..Until Loop the loop checks Exit Condition After the Loop Executes Post Test Loop Minimum Number of Executions of Loop is 1

Example Dim strPassword Do strPassword = InputBox (“Enter Password”) Loop Until (strPassword = “Friday”)

Try It Note: All Loops can be written as Do While Loops Practice this example using a D0 Until Loop Write a program that reads in a list of positive integers from the user until the user enters the value of -1 (a sentinel value – exit value). At that time the program should display the largest value in the input list.

Review of Loops 3 Requirements for Successful Loop Initialize a Loop Control Variable Test the Loop Control Variable against an Exit Condition Update the Loop Control Variables 3 Types of Loops Counted Loop – executes a specific number of times While Loop – Pre-test loop Do Until Loop – Post-test loop ALL Loops can be written as While Loops