BACS 287 Programming Fundamentals 4. BACS 287 Programming Fundamentals This lecture introduces the following iteration control structure topics: – Do.

Slides:



Advertisements
Similar presentations
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Advertisements

Loops (Part 1) Computer Science Erwin High School Fall 2014.
IS437: Fall 2004 Instructor: Dr. Boris Jukic Program Flow Control: Decisions and Conditions (Branching) Controlled Repetition (Looping)
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.
ITC 240: Web Application Programming
BACS 287 Programming Logic 3. BACS 287 Iteration Constructs Iteration constructs are used when you want to execute a segment of code several times. In.
Iteration (Looping Constructs in VB) Iteration: Groups of statements which are repeatedly executed until a certain test is satisfied Carrying out Iteration.
Introduction to Computing Dr. Nadeem A Khan. Lecture 24.
Loops – While, Do, For Repetition Statements Introduction to Arrays
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
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.
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
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.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
Lecture 4 Introduction to Programming. if ( grade ==‘A’ ) cout
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
ENGR 112 Decision Structures.
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.
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.
Chapter 71 Repetition - Do Loops n A Loops, is used to repeat a sequence of statements a number of time n There are two loops commands in Visual Basic.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
Visual Basic Programming
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
259 Lecture 11 Spring 2013 Advanced Excel Topics – Loops.
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
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.
ITEC 109 Lecture 11 While loops. while loops Review Choices –1 st –2 nd to ?th –Last What happens if you only use ifs? Can you have just an else by itself?
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.
For…Next and Do...While Loops! Happy St. Patrick’s Day!
Higher Computing Software Development -So Far- 5/10/10.
Controlling Program Flow with Looping Structures
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
Tutorial 9 Iteration. Reminder Assignment 8 is due Wednesday.
COMP Loop Statements Yi Hong May 21, 2015.
BO65: PROGRAMMING ITERATION 1. Starter  Write down what you think the code below outputs on screen: Dim intCounter as integer intCounter = 0 Do while.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
Dani Vainstein1 VBScript Session 5. Dani Vainstein2 What we learn last session? Branching Branching using If … Then … Else statement. Branching using.
Learning Javascript From Mr Saem
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
Recursion and Branching By: Engr. Faisal ur Rehman CE-105T Spring 2007 By: Engr. Faisal ur Rehman CE-105T Spring 2007.
UNIT 5 Lesson 15 Looping.
Chapter 4 Repetition Statements (loops)
Advanced Excel Topics – Loops
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
CiS 260: App Dev I Chapter 4: Control Structures II.
Programming Fundamentals
For & do/while Loops.
Outline Altering flow of control Boolean expressions
حلقات التكرار.
Visual Basic 6 Programming.
Higher Computing Using Loops.
Repetition Control Structure
Case & Repetitive Statements
OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS
Prepared By: Deborah Becker
ASP control structure BRANCHING STATEMENTS
Presentation transcript:

BACS 287 Programming Fundamentals 4

BACS 287 Programming Fundamentals This lecture introduces the following iteration control structure topics: – Do Loops – For-Next Loops – For Each-Next

BACS 287 Loop Structures Visual Basic provides 3 major loop structures. – Do-Loop (Do-While, Do-Until) – For-Next – For Each-Next There are 4 variations of the Do-Loop. The For Each-Next loop only applies to objects in a collection and arrays.

BACS 287 Do-While Loop Structures There are 2 “While” structures available: 1) Do While condition test at top of loop statements [Exit Do] Loop 2) Do statements [Exit Do] Loop While condition test at bottom of loop

BACS 287 Do-While Loop Example 1 Dim blnFlag as boolean = False Do While blnFlag = True Debug.Writeline “Hi Mom” Loop How many times will this execute?

BACS 287 Do-While Loop Example 2 Dim blnFlag as boolean = False Do Debug.Writeline “Hi Mom” Loop While blnFlag = True How many times will this execute?

BACS 287 Do-Until Loop Structures There are 2 “Until” structures available: 1) Do Until condition test at top of loop statements [Exit Do] Loop 2) Do statements [Exit Do] Loop Until condition test at bottom of loop

BACS 287 Do-Until Loop Example 1 Dim shoNum as short = 7 Dim shoGuess as short = 0 shoGuess = InputBox(“Enter Guess”) Do Until shoGuess = shoNum shoGuess = InputBox(Enter Guess”) Loop What happens when this executes?

BACS 287 Do-Until Loop Example 2 Dim shoNum as short = 7 Dim shoGuess as short = 0 shoGuess = InputBox(“Enter Guess”) Do shoGuess = InputBox(Enter Guess”) Loop Until shoGuess = shoNum What happens when this executes?

BACS 287 For-Next Loops For-Next loops let you execute a block of statements a fixed number of times. For counter = start To end [Step increment] statements [Exit For] Next [counter]

BACS 287 For-Next Loops – Alternate Syntax A newer syntax exists for For-Next loops that does not require you to define the counter variable prior to the loop. This utilizes new block structure scope for variables. For counter [AS datatype] = start To end [Step increment] statements [Exit For] Next [counter]

BACS 287 For-Next Example 1 Dim shoCnt as Short For shoCnt = 0 to 9 Debug.Writeline shoCnt Next shoCnt OR For shoCnt as short = 0 to 9 Debug.Writeline shoCnt Next shoCnt

BACS 287 Nested Loops Dim Check As Boolean = True Dim Counter As Integer = 0 Do ' Outer loop Do While Counter < 20 ' Inner loop Counter Counter += 1 ' Increment Counter If Counter = 10 Then Check = False Exit Do ' Exit inner loop End If Loop ' Exit inner loop Loop Until Check = False ' Exit outer loop

BACS 287 For-Next Example 2 For shoRow as short = 0 to 9 For shoCol as short = 0 to 4 Debug.Writeline shoRow; shoCol Next shoCol Next shoRow

BACS 287 For-Next Loops For-Next loops are commonly used to increment subscripts to process the elements of an array. If you have a multi-dimension array, use nested For-Next loops. When nested, the outer For-Next increments slowest and the inner most For-Next increments fastest.

BACS 287 For-Next Example 3 Dim strName(9,4) as String... load the array... For shoRow as integer = 0 to 9 For shoCol as integer = 0 to 4 Debug.Writeline strName (shoRow, shoCol) Next shoCol Next shoRow

BACS 287 For Each-Next Structure For Each-Next loops are similar to For-Next loops except that it repeats a block of statements for each element of a collection of objects or elements in an array. This is useful when you do not know how many elements there are in a collection or array.

BACS 287 For Each-Next Structure For Each element In group statements [Exit For] Next [element] If you are using this on a multi-dimension array, you do not need to nest For Each statements to access all elements.

BACS 287 For Each-Next Structure - Alternate Syntax For Each element [AS datatype] In group statements [Exit For] Next [element] The ‘datatype’ must be the type of object that is present in the collection. For example, if the group is a collection of strings, then the datatype would be ‘string’.

BACS 287 For Each-Next Example 1 Dim Found As Boolean = False Dim objCollection As Object For Each objObject as object In objCollection If CStr(objObject.Text)= "Hello" Then Found = True Exit For End If Next

BACS 287 For Each-Next Example 2 Dim intX (9, 19) as integer Dim objCnt as object... For Each objCnt In intX initializes matrix intX = 5 Next objCnt... For Each objCnt In intX prints matrix Print.Writeline objCnt Next objCnt

BACS 287 In-Class Loop Example1 Use a Do-While loop that tests at the top to sum the integers 1 to 100. Print the sum after you have computed it.

BACS 287 Answer Example 1 Dim shoSum as short = 0 Dim shoCnt as short = 1 Do While shoCnt < 101 shoSum += shoCnt shoCnt += 1 Loop Debug.Writeline shoSum

BACS 287 In-Class Loop Example2 Use a Do-Until loop that tests at the bottom to ask the user for a password until their answer is the same as a public variable named txtPassword. Assume that txtPassword is already defined and assigned elsewhere. Print “success” when they give the correct answer.

BACS 287 Answer Example 2 Dim strAnswer As String Do strAnswer = InputBox("Enter the Password") Loop Until strAnswer = strPassword Debug.Writeline "success"

BACS 287 In-Class Loop Example3 Modify example case #2 to give the user 3 tries to get the correct password. After the 3 rd try, tell them “sorry”.

BACS 287 Answer Example 3 Dim strAnswer As String Dim shoCnt As Short = 1 Do strAnswer = InputBox("Enter the Password") shoCnt = shoCnt + 1 Loop Until strAnswer = strPassword Or shoCnt = 4 If strAnswer = strPassword Then Debug.Writeline "success" Else Debug.Writeline "sorry" End If