For…Next and Do...While Loops! Happy St. Patrick’s Day!

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Repetition – Do Loops.
Advertisements

Loops (Part 1) Computer Science Erwin High School Fall 2014.
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.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Slide 1 VB Program Flow Control. Slide 2 Making Decisions v Decision Statement: control the execution of parts of the program based on conditions. v The.
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
Iteration (Looping Constructs in VB) Iteration: Groups of statements which are repeatedly executed until a certain test is satisfied Carrying out Iteration.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 6: Loop Control Structures.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
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.
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 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
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.
ENGR 112 Decision Structures.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
CIS 234: LOOPS Adapted from materials by Dr. Donald Bell, 2000 (updated April 2007)
ME 142 Engineering Computation I Loops. Key Concepts Looping Basics For… Next Statement Do… Loop Statement For Each… Next Statement.
Compunet Corporation1 Programming with Visual Basic.NET While, Do and For – Next Loops Week 5 Tariq Ibn Aziz.
Statements That Repeat. For...Next Loop Structure For counter = start To end Step increment statements Next counter Where Counter is tested to see if.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Visual Basic Programming Making Decisions: Loops & Decision Structures ©Copyright by Ronald P. Kessler, Ph.D.
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 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.
 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Lab 4 Range Review, Control Logic and Loops ► Range Review ► Control Logic and Loops ► Exercise.
GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS.
6.2 For…Next Loops General Form of a For…Next Loop
259 Lecture 11 Spring 2013 Advanced Excel Topics – Loops.
JavaScript, Fourth Edition
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
CW-V1 SDD 0901 Principals of Software Design and Development Loops Starter: Water JugsWater Jugs.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Repetition. Loops Allows the same set of instructions to be used over and over again Starts with the keyword loop and ends with end loop. This will create.
Counting Loops.
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.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Chapter 6 Looping Structures. Do…LoopDo…Loop Statement Can operate statements repetitively Do intx=intx + 1 Loop While intx < 10 –The Loop While operates.
Visual Basic.NET BASICS Lesson 11 List Boxes, For Next Loops, and Label Settings.
Controlling Program Flow with Looping Structures
Lab 6 (1) Range Review, Control Logic and Loops ► Control Logic and Loops ► Exercise.
Input Boxes, List Boxes, and Loops Chapter 5. 2 Input Boxes Method for getting user’s attention to obtain input. InputBox() for obtaining input MessageBox()
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
National Diploma Unit 4 Introduction to Software Development Data Structures – Loops and selection.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Loop Blocks Chapter 6 Part A. Program Blocks 1.Actions- commands, messages, methods 2.Branches- decisions to be made 3.Loops- actions to be repeated.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
MIC305 Week 6 Beyond controls Review of properties Differences with VB6: using classes and instances Programming constructs.
Dani Vainstein1 VBScript Session 5. Dani Vainstein2 What we learn last session? Branching Branching using If … Then … Else statement. Branching using.
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.
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.
Advanced Excel Topics – Loops
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
Visual Basic 6 (VB6) Data Types, And Operators
ITM 352 Flow-Control: Loops
3rd prep. – 2nd Term MOE Book Questions.
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
حلقات التكرار.
Chapter (3) - Looping Questions.
PROGRAM FLOWCHART Iteration Statements.
Presentation transcript:

For…Next and Do...While Loops! Happy St. Patrick’s Day!

For…Next Loop?  Allows a block of code to be repeated a certain number of times.  Faster and more efficient.  Uses a Counter Variable that increments each time the loop begins a cycle.  The loop stops when the counter reaches a program-specified number.

Syntax The ForNext construct looks like this: For [counter variable] = [start] TO [finish] [do this code] Next [counter variable] Do This Code Next For Counter = Start to Finish If Counter = Finish then Exit

Example Program!  Open Visual Basic.  Create a command button.  Double click on the command button and enter the following code: Dim intValue As Integer Dim strName As String For intValue = 1 to 100 strName = InputBox("What is your name?") If strName = "Donald Duck" Then MsgBox "Impossible! Ducks cant type." Exit For End If Next intValue MsgBox ("All done!")

Step Parameter! You can add the Step parameter to the ForNext construct to make it increment by values other than 1. For example, to display every other number, you would modify the For statement as follows:  For iNumber = iStart To iFinish Step 2 You could make the construct count backwards, as well:  For iNumber = iFinish To iStart Step 1

Things to Remember! There are some things to remember about the ForNext construct:  Unless you specify a negative value for Step, the starting number must be less than or equal to the ending number.  A For...Next construct always performs the code inside the construct at least once. For example, if you specify a start and finish value of zero, the code within the construct will complete one time.

DoWhile Loop! Syntax: Do While [comparison] [do this] Loop As long as the comparison returns a True value, the code within the construct will continue to repeat.

Tip! If your program gets into an infinite loop, you can press CTRL+BREAK to break Visual Basic out of the loop and stop the program. If your program gets into an infinite loop, you can press CTRL+BREAK to break Visual Basic out of the loop and stop the program.

Example: For example, the following DoWhile construct will execute until the user types a value other than 10: Dim intValue As Integer Do While intValue <> 10 intValue = InputBox("Guess a number:") Loop

Open Visual Basic – Example Program!  Open Visual Basic.  Create a command button.  Double click on the command button and enter the following code: Dim intVariable intVariable = 101 Do While intVariable > 100 intVariable = InputBox("Enter a number more than 100") If intVariable > 1000 Then MsgBox "1000 is too big!" Exit Do End If Loop MsgBox "All done!"

 Finish your worksheets.  Quiz tomorrow! On If statements, For…Next, and Do...While Loops. Homework: Study for Quiz! You can use your notes. Only 5 questions!