Conditional Loops Counted Loops

Slides:



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

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.
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.
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.
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.
CPS120 Introduction to Computer Science Iteration (Looping)
Loops and Iteration for Statements, while Statements and do-while Statements.
1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 4.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
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!
Dani Vainstein1 VBScript Session 5. Dani Vainstein2 What we learn last session? Branching Branching using If … Then … Else statement. Branching using.
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.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.-Edited By Maysoon Al-Duwais1.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
Lecture 4b Repeating With Loops
Fundamentals of PL/SQL part 2 (Basics)
UNIT 5 Lesson 15 Looping.
Chapter 6: Loops.
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Def: A control structure is a control statement and
Repetition Structures Chapter 9
Visual Basic 6 (VB6) Data Types, And Operators
Problem Solving and Control Statements: Part 2
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Chapter 5: Repetition Structures
Topic 5 for Loops -Arthur Schopenhauer
Java Programming: Guided Learning with Early Objects
Ch 7: JavaScript Control Statements I.
Topics Introduction to Repetition Structures
LESSON 11 – WHILE LOOPS UNIT 5 – 1/10/17.
Conditinoal Constructs Review
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Arrays, For loop While loop Do while loop
Outline Altering flow of control Boolean expressions
Conditinoal Constructs Review
Introduction to Object-Oriented Programming with Java--Wu
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Iteration: Beyond the Basic PERFORM
Types of Flow of Control
Do … Loop Until (condition is true)
Chapter 6 Control Statements: Part 2
CMPT 102 Introduction to Scientific Computer Programming
Chapter 6: Repetition Statements
3.1 Iteration Loops For … To … Next 18/01/2019.
Topics Introduction to Repetition Structures
Chapter 4: Repetition Structures: Looping
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Loops and Iteration CS 21a: Introduction to Computing I
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Looping and Repetition
Presentation transcript:

Conditional Loops Counted Loops 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 True False 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 process data step1 step2… stop? N Y increment counter Do Until counter = max step1 step2 step3 counter += 1 Loop

Pre-test Do While Do While counter < max step1 step2 step3 Loop Y repeat? N process data step1 step2… 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 step1 step2 step3 counter += 1 Loop Until counter = max process data step1 step2… stop? N Y increment counter

Post-test Do ... Loop While 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 limit exceeded? N Y increment loop index initialise loop index variable 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.

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 For counter = -11 To 2 Step 4 ‘ loop body Next counter 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 For counter = -11 To 2 Step 4 ‘ loop body Next counter 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 For counter = 10 To 0 Step -3 ‘ loop body Next counter Another example. _________________________________________________________ For counter = 10 To 0 Step -3 ‘ loop body Next counter What values will counter have?

For/Next Loops For counter = 10 To 0 Step -3 ‘ loop body Next counter 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 display counter Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter display counter

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

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

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

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

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

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

For/Next Simulation 9 8 5 4 1 display counter Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter 9 8 5 4 1 display counter

For/Next Simulation 9 8 5 4 1 display counter Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter 9 8 5 4 1 display counter

For/Next Simulation 12 9 8 5 4 1 display counter Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter 12 9 8 5 4 1 display counter

For/Next Simulation 12 9 13 8 5 4 1 display counter Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter 12 9 13 8 5 4 1 display counter

For/Next Simulation 12 9 13 8 5 4 1 display counter Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter In general this is poor programming. 12 9 13 8 5 4 1 display counter

Avoid changing the loop index! 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 9 8 7 6 5 4 3 2 1 innerIndex outerIndex

Nested Loops 9 1 8 7 6 5 4 3 2 innerIndex outerIndex

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 <some other condition> Then Exit For End If ‘ other steps Next