Iteration Conditional Loops Counted Loops. Charting the Flow of Control We’ve used flow charts to visualise the flow of control. The simplest form is.

Slides:



Advertisements
Similar presentations
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
Advertisements

CMPS 1371 Introduction to Computing for Engineers
CS0004: Introduction to Programming Repetition – Do Loops.
Repeating Actions While and For Loops
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.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
Iteration (Looping Constructs in VB) Iteration: Groups of statements which are repeatedly executed until a certain test is satisfied Carrying out Iteration.
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Chapter 5: Control Structures II (Repetition)
Chapter 4 Loops and Character Manipulation Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
VB .NET Programming Fundamentals
The switch Statement, DecimalFormat, and Introduction to Looping
5.05 Apply Looping Structures
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
CIS Computer Programming Logic
1 Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops 6.3 List Boxes and Loops.
Chapter 5: Control Structures II (Repetition)
Do … while ( continue_cond ) Syntax: do { stuff you want to happen in the loop } while (continue_condition);
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.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
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.
Loops and Iteration for Statements, while Statements and do-while Statements.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
Compunet Corporation1 Programming with Visual Basic.NET While, Do and For – Next Loops Week 5 Tariq Ibn Aziz.
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 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
ISBN Chapter 8 Statement-Level Control Structures.
sequence of execution of high-level statements
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Control Structures sequence of execution of high-level statements.
6.2 For…Next Loops General Form of a For…Next Loop
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 5A Repetition (Concepts)
J AVA P ROGRAMMING 2 C H 03: C ONTROL STATEMENTS if, for loop (review) switch, while, do while break, continue Fall Java Programming.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 4.
1 Iterative Statements Repeated execution of a (compound) statement by iteration or recursion –Iteration is statement level –Recursion is unit-level control.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops 6.4 A Case Study:
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
Tutorial 6: The Repetition Structure1 Tutorial 6 The Repetition Structure.
For…Next and Do...While Loops! Happy St. Patrick’s Day!
Controlling Program Flow with Looping Structures
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Fourth Quarter.  Involves loops or cycles ◦ Loops: means that a process may be repeated as long as certain condition remains true or remains false. ◦
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
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 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.
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.
REPETITION CONTROL STRUCTURE
Def: A control structure is a control statement and
Repetition Structures Chapter 9
Think What will be the output?
Chapter 5: Repetition Structures
Java Programming: Guided Learning with Early Objects
Chapter 4: Repetition Structures: Looping
Conditional Loops Counted Loops
Presentation transcript:

Iteration Conditional Loops Counted Loops

Charting the Flow of Control We’ve used flow charts to visualise the flow of control. The simplest form is sequence. Declare vars for input and result get input produce result from input show result

Charting the Flow of Control We’ve also used flow charts to map several different styles of selection. e.g. If/Then/Else Condition TrueFalse Process

Flow of Control The third way control can be transferred is called iteration, repetition, or looping. There are several ways to describe a looping process. For example: –Repeat the following steps if conditions are right. conditional loop –Repeat the following steps X times. counted loop

Repeat the following steps if conditions are right. “if conditions are right” can mean different things: –… until there’s no more data. –… while there’s still data. The difference is simply in how the condition is stated. “the following steps” implies that the condition will be tested before the steps are executed. –This is called a pre-test.

Conditional loops In VB, loops are specified by the keywords Do and Loop. Statements that make up “the following steps” are bracketed by this pair and will be repeatedly executed. Do ' step 1 ' step 2... Loop

Conditional loops The keywords Until or While specify the Conditions.Note: Until counter >= Max and While counter < Max are functionally equivalent.

Pre-test Do Until Do Until counter = max step1 step2 step3 counter += 1 Loop process data step1 step2… stop? N Y increment counter

Pre-test Do While Do While counter < max step1 step2 step3 counter += 1 Loop process data step1 step2… repeat? Y N increment counter

Post-test Loops There are other ways of expressing conditional loops: –Repeat steps 1, 2, 3, & 4 until there’s no more data. –Repeat steps 1, 2, 3, & 4 while there’s still data. These are the same types of conditions. The difference is that the condition will be tested after the steps are executed each time. –This is called a post-test.

Post-test Do... Loop Until Do step1 step2 step3 counter += 1 Loop Until counter = max process data step1 step2… stop? N Y increment counter

Post-test Do... Loop While Do step1 step2 step3 counter += 1 Loop While counter < max process data step1 step2… repeat? Y N increment counter C

Repeat the following steps X times. Counted loops are so common that most programming languages have a special syntax which optimises counted loops. In VB this structure is implemented as the For/Next structure. For loopIndex = start To finish ‘ body of loop Next loopIndex

For/Next Loops Dim counter As Integer For counter = 1 To 5 ‘ loop body Next counter For/Next loops use a pre-test, and have a Do Until style. loop body limit exceeded? N Y increment loop index initialise loop index variable

For/Next Loops In the most general case, For/Next loops use 4 integer parameters. Dim counter As Integer‘loop index Dim start As Integer‘initial value Dim finish As Integer‘final value Dim increment As Integer‘delta value For counter = start To finish Step increment ‘ loop body Next counter

For/Next Loops The scope of counter extends beyond the loop, so its value can be used or changed when the loop is finished. This may be a useful side effect. Dim counter As Integer For counter = 1 To 5 ‘loop body Next counter txtDisplay.Text = “Counter is ” & Str(Counter) will display …

For/Next Loops The scope of counter extends beyond the loop, so its value can be used or changed when the loop is finished. This may be a useful side effect. Dim counter As Integer For counter = 1 To 5 ‘loop body Next counter txtDisplay.Text = “Counter is ” & Str(Counter) will display 6.

For/Next Loops Since they are integers they can have positive or negative values. _________________________________________________________ For counter = -11 To 2 Step 4 ‘ loop body Next counter _________________________________________________________ What values will counter have? _________________________________________________________

For/Next Loops Since they are integers they can have positive or negative values. _________________________________________________________ For counter = -11 To 2 Step 4 ‘ loop body Next counter _________________________________________________________ What values will counter have? _________________________________________________________ counter will have the values -11, -7, -3, 1, and 5.

For/Next Loops Another example. _________________________________________________________ For counter = 10 To 0 Step -3 ‘ loop body Next counter _________________________________________________________ What values will counter have? _________________________________________________________

For/Next Loops Another example. _________________________________________________________ For counter = 10 To 0 Step -3 ‘ loop body Next counter _________________________________________________________ What values will counter have? _________________________________________________________ counter will have the values 10, 7, 4, 1 and -2.

For/Next Loops The start, finish, and increment values can be specified as literals or as Integer variables declared before the loop. The loop index must be an Integer variable. It must be declared before the loop.

For/Next Loops VB ignores attempts to change the loop parameters within the loop, Dim counter, start, finish, increment As Integer For counter = 1 To 5 start = -2 finish = 10 increment = 5 ‘ will have no effect Next counter except…

For/Next Loops …the loop index. Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter Simulate this program to see what values will be displayed. skip

For/Next Simulation Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter displaycounter

For/Next Simulation Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter 1 displaycounter

For/Next Simulation Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter 11 displaycounter

For/Next Simulation Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter 4 11 displaycounter

For/Next Simulation Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter displaycounter

For/Next Simulation Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter displaycounter

For/Next Simulation Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter displaycounter

For/Next Simulation Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter displaycounter

For/Next Simulation Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter displaycounter

For/Next Simulation Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter displaycounter

For/Next Simulation Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter displaycounter

For/Next Simulation Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter In general this is poor programming displaycounter

For/Next Loops Here’s an even poorer example. Dim counter As Integer For counter = 1 To 5 counter = 1 Next counter Since counter is set to 1 each time the loop executes it can never reach 5, so the loop is infinite. Avoid changing the loop index!

Nested Loops The body of a loop can contain any VB elements, including a loop. For outerIndex = 0 To 9 For innerIndex = 0 To 9 ‘display (Str(outerIndex) & Str(innerIndex)) Next innerIndex Next outerIndex What is the ‘output’ from this loop?

Nested Loops innerIndexouterIndex

Nested Loops innerIndexouterIndexinnerIndexouterIndex

Notes VB allows programmers to omit the loop index from the Next statement: For counter = start To finish ‘ loop body Next [counter]

Notes This can make code difficult to follow. For outerIndex = 0 To 9 For middleIndex = 0 To 9 For innerIndex = 0 To 9 ‘display (Str(outerIndex) & _ (Str(middleIndex ) & Str(innerIndex)) Next

Notes Proper use of indentation also helps. For outerIndex = 0 To 9 For middleIndex = 0 To 9 For innerIndex = 0 To 9 ‘display (Str(outerIndex) & _ (Str(middleIndex ) & Str(innerIndex))‏ Next innerIndex Next middleIndex Next outerIndex

Notes VB allows programs to exit For - Next loops before termination is reached. For counter = start To finish Exit For Next

Notes Usually this command is controlled by an If statement. For counter = start To finish If Then Exit For End If ‘ other steps Next