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.

Slides:



Advertisements
Similar presentations
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Advertisements

1 Loops zDo While Loop: yRepeats a block of code WHILE a condition remains TRUE Do While condition Code to execute Loop.
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.
Input and Message Boxes. InputBox() Function An input box is a dialog box that opens and waits for the user to enter information. Syntax: InputBox(prompt[,title][,default])
BACS 287 Programming Fundamentals 4. BACS 287 Programming Fundamentals This lecture introduces the following iteration control structure topics: – Do.
For Next Looping My first Looping Structure. For…Next FOR counter_variable = initial_value TO end_value STEP increment Statement-1 Statement-2 ……. Statement-n.
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
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:
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.
For Loops (ProjFor1, ProjFor2, ProjFor3, ProjFor4, textbox, textbox1) Please use speaker notes for additional information!
ENGR 112 Decision Structures.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
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.
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 2) UTPA – Fall 2011 Part of the slides is from Dr. John Abraham’s previous.
ISAT 252 Visual Basic Repetition. Assignment Should have read Chapter 5 ( ) on loops Do tutorial 5-4.
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.
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 - Visual Basic Schneider 1 Chapter 6 Repetition.
CHAPTER SIX LOOPS © Prepared By: Razif Razali 1. FORMAT OR REFRESH!! WHAT HAVE WE LEARN? Differentiate between the types of selection structure? Which.
4.1 Introduction Structured programming –Control structures Helpful in building and manipulating objects BZUPAGES.COM.
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.
CS285 Visual Basic 2 Department of Computing UniS 1 Statements in Visual Basic A statement is the fundamental syntactical element of a program smallest.
Chapter 16: Programming Structures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Overview of VBA Programming & Syntax. Programming With Objects u Objects –Properties: attributes or characteristics of an object (e.g., font size, color,
Repetition Statements
Chapter 16: Programming Structures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
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.
CW-V1 SDD 0901 Principals of Software Design and Development Loops Starter: Water JugsWater Jugs.
Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional.
Chapter 3 Control Structures. The If…Then Statement The If…Then statement is a Decision statement = that executes a set of statements when a condition.
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 Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something.
For…Next Loops, Checked List Boxes, and Combo Boxes Chapter 5.
For…Next and Do...While Loops! Happy St. Patrick’s Day!
Higher Computing Software Development -So Far- 5/10/10.
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
5.1 Introduction Problem Solving –Requires understanding of: Building blocks Program-construction principles BZUPAGES.COM.
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()
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
CSD 340 (Blum)1 Introducing Text Input elements and Ifs.
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.
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.
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.
UNIT 5 Lesson 15 Looping.
Visual Basic 6 (VB6) Data Types, And Operators
Tutorial 10 – Class Average Application Introducing the Do…Loop While and Do…Loop Until Repetition Statements Outline Test-Driving the Class Average.
3rd prep. – 2nd Term MOE Book Questions.
3rd prep. – 2nd Term MOE Book Questions.
Introducing Do While & Do Until Loops & Repetition Statements
Control Structures: Part 1
حلقات التكرار.
Chapter (3) - Looping Questions.
Higher Computing Using Loops.
CIS 16 Application Development Programming with Visual Basic
Case & Repetitive Statements
Alternate Version of STARTING OUT WITH C++ 4th Edition
Prepared By: Deborah Becker
Presentation transcript:

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 expression evaluates as false. Do strPassword = InputBox("Enter your password") Loop While strPassword <> "Lefty" The example code will loop until the correct password is entered

Do...Loop Until structures check the condition after executing the code and repeat a code block until the test expression evaluates as true. Dim intLoopCount As Integer Do intLoopCount = intLoopCount + 1 Loop Until MsgBox("Loop?", vbYesNo) = vbNo The example code will loop until the No button is clicked in a message box.

Do While...Loop structures check the condition before executing the code. The code in the loop is executed only if the condition evaluates as true, and repeats until the test expression evaluates as false. Dim strPasswordTry As String Const PASSWORD As String = "password" Do While strPasswordTry <> PASSWORD strPasswordTry = InputBox("Enter password") Loop The example code will ask the user for a password until the correct password is typed into the input box

Do Until...Loop structures check the condition before executing the code. The code in the loop is executed only if the condition evaluates as false, and repeats until the test expression evaluates as true. Dim intValue As Integer Dim strInput As String Do Until intValue = 3 strInput = InputBox("Pick a number between 1 and 5") intValue = Val(strInput) Loop

CHALLENGE: Dim strMsg As String Dim strFilespec As String Dim strMatch As String strMsg = "Enter a file specification." 'Get file extension strFilespec = InputBox(strMsg) 'Find first match strMatch = Dir(strFilespec) Do Until Len(strMatch) = 0 'Display matching files MsgBox strMatch 'Find next match strMatch = Dir() Loop MsgBox "There are no more matching files."

When both the condition and the number of times a code block will execute is known, use a For loop. For counter = start To end [Step increment] [statements] Next [counter]

Dim intNumStudents As Integer Dim intCounter As Integer Dim sglScore As Single Dim sglTotalScore As Single Dim sglAverage As Single intNumStudents = InputBox(prompt:="How many students?") For intCounter = 1 To intNumStudents sglScore = CSng(Val(InputBox(prompt:="Enter score"))) sglTotalScore = sglTotalScore + sglScore Next intCounter sglAverage = sglTotalScore / intNumStudents The following example code displays an input box that prompts the user to enter a number. It then executes the code in the For...Next loop the specified number of times, calculating and displaying a result.

Dim intOuter As Integer, intInner As Integer Dim strOut As String ' Outer Loop For intOuter = 1 To 10 ' Inner Loop For intInner = 1 To 10 ' Concatenate Outer * Inner results to strOut strOut = strOut & " " & Format(intOuter * intInner, Next intInner ' Add a carriage return and linefeed to end of line strOut = strOut & vbCrLf Next intOuter ' Monospaced font Me.Font = "Courier New" ' Display strOut on form Me.Print strOut The first For...Next loop generates each new line within the table. The second For...Next loop is nested within the first and generates each column within a row