6.2 For…Next Loops General Form of a For…Next Loop

Slides:



Advertisements
Similar presentations
Chapter 6 - VB 2005 by Schneider1 Sample Dim i As Integer For i = 1 To 5 lstTable.Items.Add(i & " " & i ^ 2) Next The loop control variable, i, is Initialized.
Advertisements

CS0004: Introduction to Programming Repetition – Do Loops.
Programming Logic and Design, Third Edition Comprehensive
Objectives In this chapter, you will learn about:
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
Types of LOOP Structures Do While ……. Loop Do Until …… Loop For …… Next loop.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
CSI 101 Elements of Computing Spring 2009 Lecture # 8 Looping and Recursion Wednesday, February 25 th, 2009.
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.
Chapter 6 - VB.Net by Schneider1 Chapter 6 – Repetition 6.1 Do Loops 6.2 Processing Lists of Data with Do Loops Peek Method Counters and Accumulators Flags.
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.
Chapter 6 - Visual Basic Schneider
1 Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops 6.3 List Boxes and Loops.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Interest Calculator Application Introducing the For...Next Repetition Statements.
Copyright 2009 by Pearson Education Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos: Ch.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
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.
PL/SQL Loops. Building Logical Conditions All logical conditions must yield a boolean condition. You can build a simple Boolean condition by combining.
Programming Logic and Design Fifth Edition, Comprehensive
Computer Science 12 Mr. Jean May 2 nd, The plan: Video clip of the day Review of common errors in programs 2D Arrays.
Programming Logic and Design Sixth Edition Chapter 5 Looping.
1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice.
Compunet Corporation1 Programming with Visual Basic.NET While, Do and For – Next Loops Week 5 Tariq Ibn Aziz.
Chapter 6 - Visual Basic Schneider 1 Chapter 6 Repetition.
Visual Basic.net Loops. Used to do multiple executions of the same block of code Do while loops Do until loops For next loops.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
Chapter 6 - VB.Net by Schneider1 Chapter 6 – Repetition 6.1 Do Loops 6.2 Processing Lists of Data with Do Loops Peek Method Counters and Accumulators Flags.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
Counter-Controlled Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
JavaScript, Fourth Edition
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
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:
Building Java Programs Chapter 2 Primitive Data and Definite Loops Copyright (c) Pearson All rights reserved.
Tutorial 6: The Repetition Structure1 Tutorial 6 The Repetition Structure.
Chapter 61 Example : For … To… Step For index = 0 To n Step s lstValues.Items.Add(index) Next Control variable Start value Stop value Amount to add to.
5.1 Introduction Problem Solving –Requires understanding of: Building blocks Program-construction principles BZUPAGES.COM.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
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.
Chapter 6 - VB 2008 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:
1 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: Analyze a Loan.
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.
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
Outline Altering flow of control Boolean expressions
Chapter (3) - Looping Questions.
Building Java Programs
Building Java Programs
Variables variable: A piece of the computer's memory that is given a name and type, and can store a value. Like preset stations on a car stereo, or cell.
Building Java Programs
Chapter 6 - VB.Net by Schneider
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Chapter 2 Lecture 2-2: The for Loop reading: 2.3
Presentation transcript:

6.2 For…Next Loops General Form of a For…Next Loop Nested For…Next Loops Local Type Inference

For…Next Loops Used when we know how many times we want the loop to execute A counter controlled loop

For…Next Loop Syntax

Sample The loop counter variable, i, is initialized to 1 For i As Integer = 1 To 5 lstTable.Items.Add(i & " " & i ^ 2) Next The loop counter variable, i, is initialized to 1 tested against the stop value, 5 incremented by 1 at the Next statement The loop counter variable is often used in statements within the loop body. In this case, displaying a sequence of numbers and their squares

Similar Do While Loop Dim i As Integer = 1 Do While i <= 5 lstTable.Items.Add(i & " " & i ^ 2) i += 1 Loop Loop counter initialization Loop counter initialization increment

Example 1: Output

Example 1: Code Dim pop As Double = 300000 For yr As Integer = 2010 To 2014 lstTable.Items.Add(yr & " " & FormatNumber(pop, 0)) pop += 0.03 * pop Next

Step Clause Normally after each pass the value of the counter variable increases by 1 If the clause Step s is appended to the For statement, the value of s will be added to the counter variable after each pass. If the value of s is a negative number, the value of the counter variable will decrease after each pass.

Example with Negative Step Value For j As Integer = 10 To 1 Step -1 lstBox.Items.Add(j) Next lstBox.Items.Add("Blastoff") Looping backward through the numbers. Start at 10, test to see if j >= 1, decrement by 1. Question: What would happen with Step -2?

Example with Negative Step Value For j As Integer = 1 To 10 Step -1 lstBox.Items.Add(j) Next lstBox.Items.Add("Blastoff") Question: What would happen in this case?

Example 2 This is a good example for testing out various values for the loop’s test and step increment/decrement.

Example 3 Note: Here we have a separate function that is called by the event procedure. Function call Parameter passing Function declaration More on this in Ch5.

Example 3 Here, the loop counter variable is used as an index into the string. It is common practice to use loops for retrieving or manipulating successive positions of a string or an array or a listbox’s collection.

Example 3 Loop starts at the last character of the string, and goes backward until the first.

Nested Loops Loops can be nested inside other loops.

Example 4 Outer loop will execute three times Each time the outer loop executes, the inner loop will execute three times Therefore, the inner loop executes a total of nine times

Example: Nested For…Next Loops For i As Integer = 65 To 70 For j As Integer = 1 To 25 lstBox.Items.Add(Chr(i) & j) Next Output: A1 A2 A3 : Outer loop Inner loop Question 1: how many times will the outer loop execute? Question 2: how many times (total) will the inner loop execute?

The Nested for Loop -- Flowchart Statements preceding loop Initialize outer loop counter The Nested for Loop -- Flowchart Test to continue outer loop Increment outer loop counter No Statements following loop Yes Outer loop statement(s) Initialize inner loop counter Test to continue inner loop Increment inner loop counter No Yes Inner loop statement(s) Outer loop statement(s)

Example 4 This example produces a multiplication table. The inner and outer loop counters are used for doing the calculations and displaying the results.

For and Next Pairs For and Next statements must be paired. If one is missing, the automatic syntax checker will complain with a wavy underline and a message such as “A ‘For’ must be paired with a ‘Next’.”

Start, Stop, and Step values Consider a loop beginning with For i As Integer = m To n Step s The loop will be executed exactly once if m equals n no matter what value s has. The loop will not be executed at all if m is greater than n and s is positive, or if m is less than n and s is negative.

Altering the Counter Variable The value of the counter variable should not be altered within the body of the loop. Doing so might cause the loop to repeat indefinitely or have an unpredictable number of repetitions.

Non-Integer Step Values Can lead to round-off errors with the result that the loop is not executed the intended number of times. We will only use Integers for all values in the header.