Developing Software Applications Iteration in Visual Basic (Loops)

Slides:



Advertisements
Similar presentations
Chapter 7 - Iteration. Chapter Goals Program repitiation statements – or loops – with the for, while, and do-while statements Program repitiation statements.
Advertisements

Looping Structures: Do Loops
How SAS implements structured programming constructs
Execute Blocks of Code Multiple Times Telerik Software Academy C# Fundamentals – Part 1.
CS0004: Introduction to Programming Repetition – Do Loops.
Iteration (Looping Constructs in VB) Iteration: Groups of statements which are repeatedly executed until a certain test is satisfied Carrying out Iteration.
REPETITION (loops or iteration) Schneider: Sec. 6.1 & 6.3.
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.
Arrays in Visual Basic Week 9 CM What is an array ? An array is a data structure that enables us to store a list of values that can be thought.
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.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Chapter 5: Control Structures II (Repetition)
CSI 101 Elements of Computing Spring 2009 Lecture # 8 Looping and Recursion Wednesday, February 25 th, 2009.
Developing Software Applications Introduction to Programming Fundamentals Scoping in VB Simple Ifs in VB.
Lecture 12 Another loop for repetition The while loop construct © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
Iteration and Simple Menus Deterministic / Non-deterministic loops and simple menus.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
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.
DiagrammaticRepresentation Iteration Construct False True Condition Exit from Statement (s) loop Sequence construct Selection construct Statement 1 Statement.
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.
Logic Structure - focus on looping Please use speaker notes for additional information!
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.
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 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.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
CS285 Visual Basic 2 Department of Computing UniS 1 Statements in Visual Basic A statement is the fundamental syntactical element of a program smallest.
9-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)
Visual Basic Programming
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Coding Design Tools Rachel Gauci. Task: Counting On Create a program that will print out a sequence of numbers from "1" to a "number entered”. Decision’s.
PROGRAMMING ITERATION 2. Starter  Put the following code in order (write down the line numbers).  The program should display the numbers 1-24 on screen.
6.2 For…Next Loops General Form of a For…Next Loop
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.
CPS120 Introduction to Computer Science Iteration (Looping)
 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!
Controlling Program Flow with Looping Structures
Intro to Loops 1.General Knowledge 2.Two Types of Loops 3.The WHILE loop 1.
COMP Loop Statements Yi Hong May 21, 2015.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
PGT C Programming1 Week 4 – Repetition Structures / Loops.
CPSC 233 Tutorial 5 February 2 th /3 th, Java Loop Statements A portion of a program that repeats a statement or a group of statements is called.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
1 VB-04-Control Structures 16 March 2016 Visual Basic Control Structures - Selection.
5a – While Loops Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
Chapter 6 Controlling Program Flow with Looping Structures.
26/06/ Iteration Loops For … To … Next. 226/06/2016 Learning Objectives Define a program loop. State when a loop will end. State when the For.
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.
Chapter 6: Loops.
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
PROGRAM CONTROL STRUCTURE
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Introducing Do While & Do Until Loops & Repetition Statements
Control Structure Senior Lecturer
حلقات التكرار.
Chapter (3) - Looping Questions.
Iteration: Beyond the Basic PERFORM
Iteration: Beyond the Basic PERFORM
Algorithms Take a look at the worksheet. What do we already know, and what will we have to learn in this term?
Loops.
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
WJEC GCSE Computer Science
Presentation transcript:

Developing Software Applications Iteration in Visual Basic (Loops)

Looping Programs often have a requirement to process a set of instructions several times This sequence might be for a fixed number of times Or whether it is repeated might depend on a particular condition This means that there are a number of choices ………..

Iteration Constructs in VB For … Next Do … Loop Until Do While … Loop

Iteration constructs are of 3 types Repeat a block of code FOR a given number of times WHILE some condition is true, repeat a block of code Repeat a block of code UNTIL a condition is true

For loops The simplest loop Used when we know exactly how many times we want to repeat things e.g. Input and add together 20 exam marks

For loops - in structured English e.g. Input and add together 20 exam marks For 20 times Input a mark Add mark to total

For loops - a simple example e.g. Input and add together 20 exam marks For iCount = 1 To 20 iMark = InputBox(“Enter a mark”) iTotal = iTotal + iMark Next iCount

Expanding that code... Dim iCount, iMark, iTotal as Integer iTotal = 0 For iCount = 1 To 20 iMark = InputBox(“Enter a mark”) iTotal = iTotal + iMark Next iCount MsgBox (“Sum of marks is “ & iTotal)

A fuller syntax of the For loop.. For = To Step Next e.g. iSum = 0 For iCount = 2 To 100 Step 2 iSum = iSum + iCount Next iCount Adds the even numbers 2, 4, 6,.. 100

Step can be decreasing … For iLoopCounter = x To y [Step] Perform functions Next For a = 1 To 10 For a = 1 To 10 Step 5 For a = 10 To 1 Step -1 For a = 10 To 1 Step -5

Example: add the values of numbers 1 to 10 iTotal = 0 For iLoopCounter = 1 to 10 iTotal = iTotal + iLoopCounter Next

Nested For Loops … For iCount1 = 1 to 20 For iCount2 = 1 to 100 statements ….. Next iCount2 Next iCount1 In nested For loops, the Next statement must implicitly state which loop is progressing next

Demonstrating the For loop with a ListBox Dim iTotal as Integer Dim iCount as Integer iTotal = 0 For iCount = 1 To 10 iTotal = iTotal + iCount lstNos.AddItem iTotal Next lblTotal.Caption = iTotal

Question What about if we want the loop to stop when the total exceeds 100 ?

Stopping the For loop Dim iTotal as Integer Dim iCount as Integer iTotal = 0 For iCount = 1 To 100 iTotal = iTotal + iCount lstNos.AddItem iCount & ", " & iTotal If iTotal > 100 Then iCount = 101 End If Next lblTotal.Caption = iTotal Setting iCount to a value >100 causes this For loop to stop

trivial example works, but is not very elegant code using the IF the statement to force the exit of the FOR loop is not considered to be “good code” … it is bad practice

Do While loop Do while condition statement(s) Loop

Back to earlier example iTotal = 0 iCount = 1 Do While iTotal < 100 iTotal = iTotal + iCount lstNos.AddItem iCount & ", " & iTotal iCount = iCount + 1 Loop lblTotal.Caption = iTotal

Comparing For with Do While Dim iTotal, iCount as Integer iTotal = 0 For iCount = 1 To 100 iTotal = iTotal + iCount lstNos.AddItem iTotal If iTotal > 100 Then iCount = 101 End If Next lblTotal.Caption = iTotal Dim iTotal, iCount as Integer iTotal = 0 iCount = 1 Do While iTotal < 100 iTotal = iTotal + iCount lstNos.AddItem iTotal iCount = iCount + 1 Loop lblTotal.Caption = iTotal

Second syntax – Do …….. Loop Until …. Dim iTotal, iLoopCount as Integer iTotal = 0 iLoopCount = 1 Do iTotal = iTotal + iLoopCount lstNos.AddItem iLoopCount & ", " & iTotal iLoopCount = iLoopCount + 1 Loop Until iTotal >= 100 lblTotal.Caption = iTotal

Comparing the 2 deterministic loops.. iTotal = 0 iCount = 1 Do While iTotal < 10 iTotal = iTotal + iCount lstNos.AddItem iTotal iCount = iCount + 1 Loop lblTotal.Caption = iTotal iTotal = 0 iLoopCount = 1 Do iTotal = iTotal + iLoopCount lstNos.AddItem iTotal iLoopCount = iLoopCount + 1 Loop Until iTotal > 10 lblTotal.Caption = iTotal May look the same … but the results may be different !!

How many times is this loop executed ? Dim iTotal, iLoopCount As Integer iTotal = 0 iLoopCount = 1 Do While iTotal < 10 iTotal = iTotal + iLoopCount lstNos.AddItem iLoopCount & ", " & iTotal iLoopCount = iLoopCount + 1 Loop Label1.Caption = iTotal

How many times is this loop executed ? Dim iTotal, iLoopCount As Integer iTotal = 0 iLoopCount = 1 Do iTotal = iTotal + iLoopCount lstNos.AddItem iLoopCount & ", " & iTotal iLoopCount = iLoopCount + 1 Loop Until iTotal > 10 lblTotal.Caption = iTotal

Comparing the 2 While loops.. Do While iTotal < 10 ………… Loop Do ……………… Loop Until iTotal > 10 Will not execute at all if iTotal is already 10 or more Will always execute the code once, and then test if iTotal is over 10

Summary … For iCount = 1 To 10 ………… Next iCount Do …………. Loop Until condition Do While condition …………. Loop Loops a FIXED NUMBER OF TIMES ALWAYS EXECUTES CODE ONCE before testing whether to loop TESTS CONDITION FIRST, then executes code if appropriate

Do …. Loop Until...example Calculating a factorial e.g Factorial 6 (mathematically, 6! ) = 6 * 5 * 4 * 3 * 2 * 1 could be coded like this …..

factorial ex. using Do … Loop Until … Dim iNum, iFactorial as Integer iFactorial = 1 iNum = 6 Do iFactorial = iFactorial * iNum iNum = iNum – 1 Loop Until iNum = 1 Stop when iNum is equal to 1

Seeing how factorial ex. works Dim iNum, iFactorial as Integer iFactorial = 1 iNum = 6 Do iFactorial = iFactorial * iNum iNum = iNum – 1 Loop Until iNum = 1 iFactorial 1 iNum

Summarising again … For … Do … Loop Until … Do While ….. Loop Loops a FIXED NUMBER OF TIMES ALWAYS EXECUTES CODE ONCE before testing whether to loop or to stop TESTS CONDITION FIRST, then executes code if appropriate