Chapter81 For....Next Loops n For i = m To n statement (s) Next i n For statement designates a numeric variable, called control variable. n It is initialized.

Slides:



Advertisements
Similar presentations
Chapter 6 - VB 2005 by Schneider1 Do Loop Syntax Do While condition statement(s) Loop Condition is tested, If it is True, the loop is run. If it is False,
Advertisements

CS 4 Intro to Programming using Visual Basic Do Loops Patchrawat Uthaisombut University of Pittsburgh 1 based on lecture notes by D. Schneider.
VB PROJECT “PROJECT SAMPLES”. For Next Loops Design a VB program that displays in a picture box the first N multiples of an input integer Input 3 exam.
Introduction to Computing Dr. Nadeem A Khan. Lecture 9.
VBA Modules, Functions, Variables, and Constants
Introduction to Computing Dr. Nadeem A Khan. Lecture 27.
Chapter 7 - Visual Basic Schneider1 Chapter 7 Arrays.
Types of LOOP Structures Do While ……. Loop Do Until …… Loop For …… Next loop.
Introduction to Arrays Chapter 7 Why use arrays? To store & represent lists of homogeneous values To simplify program code To eliminate the need to reread.
Introduction to Computing Dr. Nadeem A Khan. Lecture 24.
Arrays Array of Controls: several controls, of the same type (Class: a prototype for an object indicating the properties and methods), that have the same.
Chapter 7 - Visual Basic Schneider1 Chapter 7 Arrays.
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
1 Chapter 7 Arrays. 2 Outline and Objective In this chapter we will Learn about arrays One-dimensional arrays Two-dimensional arrays Learn about searching.
Chapter 6 - Visual Basic Schneider
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
Chapter 7 - Visual Basic Schneider1 Chapter 7 Arrays.
Arrays Array of Controls: several controls, of the same type (Class: a prototype for an object indicating the properties and methods), that have the same.
Chapter 7 - Visual Basic Schneider
User-defined Data Types VB provides programmers with the ability to create user-defined types. User-defined Types are collections of related variables.
IE 212: Computational Methods for Industrial Engineering
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling.
1 Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops 6.3 List Boxes and Loops.
Using Arrays and File Handling
Array Processing: Exercises School of Business Eastern Illinois University © Abdou Illia, Spring 2002 (Week 10, Friday 3/28/2003)
Review for Exam 2 School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 10, Friday 3/21/2003) - IF Blocks - Do Loops - Select.
Chapter 8 Arrays Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
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.
Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops.
Arrays and 2D Arrays.  A Variable Array stores a set of variables that each have the same name and are all of the same type.  Member/Element – variable.
© 2012 EMC Publishing, LLC Slide 1 Chapter 8 Arrays  Can store many of the same type of data together.  Allows a collection of related values to be stored.
1 Chapter 7 – Arrays 7.1 Creating and Using Arrays 7.4 Two-Dimensional Arrays.
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.
Arrays. Variable Arrays a group of variables that have the same name and data type each element in a variable array is identified by a subscript refer.
‘Tirgul’ # 3 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #3.
Visual Basic Programming
6.2 For…Next Loops General Form of a For…Next Loop
Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -
JavaScript, Fourth Edition
Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional.
Chapter 71 Arrays Creating and Accessing Arrays Using Arrays Some Additional Types of Arrays.
6-1 Chapter 6 Working with Arrays in VB.NET. 6-2 Learning Objectives Understand the use of list and table arrays in VB.NET projects and the difference.
1 Scripting Languages VBScript - Recognized mainly by Internet Explorer only - Netscape does have a plug-in JavaScript - Recognized by Internet Explorer.
Review for Final (Part 2) School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 15, Friday 5/2/2003)
Arrays Chapter 8. Overview u General discussion u Variable arrays u Control arrays u Multi-dimensional variable arrays  Two-dimensional  Three-dimensional.
Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays.
Chapter 8 P 1 Arrays and Grids Single-dimension arrays Definition An array is a sequence of elements all referred to with a common name. Other terms: table,
Controlling Program Flow with Looping Structures
1 Chapter 7 Arrays. 2 Outline and Objective In this chapter we will Learn about arrays One-dimensional arrays Two-dimensional arrays Learn about searching.
情報基礎 A Lecture 9 Takeshi Tokuyama ・ Jinhee Chun Tohoku University Graduate School of Information Sciences System Information Sciences Design and Analysis.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
Visual Basic CDA College Paphos Campus COM123 Visual Programming 1 Lecture: Charalambous Sotiris Week 8: COM123 Visual Programming 1 Lecture: Charalambous.
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
Dani Vainstein1 VBScript Session 5. Dani Vainstein2 What we learn last session? Branching Branching using If … Then … Else statement. Branching using.
Chapter 6 Controlling Program Flow with 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.
Introduction to Computing Dr. Nadeem A Khan. Lecture 24.
Programming Right from the Start with Visual Basic .NET 1/e
Single Dimensional Arrays
Chapter 7 Arrays.
ARRAYS.
Chapter 4 - Visual Basic Schneider
Lbound and Ubound Functions
Arrays.
For...Next Statements.
Presentation transcript:

Chapter81 For....Next Loops n For i = m To n statement (s) Next i n For statement designates a numeric variable, called control variable. n It is initialized and then automatically changes after each execution of loop. The Next statement increments the control variable n If m<=n, then i is assigned the values m, m+1,...., n in order the body is executed once for each of these values. n If m>n, then execution continues with the statement after the For...Next loop.

Chapter82 For....Next Loop n For example: p278 ex1 Th following program displays a table showing the population each year until 2000 Private Sub cmdDisplay_Click() Dim pop As Single, yr As Integer 'Display population from 1998 to 2002 picTable.Cls pop = For yr = 1998 To 2002 picTable.Print yr, FormatNumber(pop, 0) pop = pop * pop Next yr End Sub

Chapter83 For....Next Loop n Flowchart of a For...Next loop

Chapter84 For....Next Loop (Nested Loop) n There are three type of nested loops For jFor iFor i For k For j For j For k Next j Next k For j For k Next k Next k Next k For k Next j Next j For m Next i Next m Next k Next j Next i

Chapter85 For....Next Loop (Nested Loop) n Example p.281 ex4 A multiplication table for integer 1 to 4 Private Sub cmdDisplay_Click() Dim j As Integer, k As Integer picTable.Cls For j = 1 To 4 For k = 1 To 4 picTable.Print j; "x"; k; "="; j * k, Next k picTable.Print Next j End Sub

Chapter86 Arrays n An Array variable is a collection of simple variables of the same type n For example –Private Sub cmdButton_Click() Dim student1 As String, score1 As Single Dim student2 As String, score2 As Single Dim student3 As String, score3 As Single n It would be nice if we could just write For i = 10 To 30 Input #1, studenti, scorei Next i n However, this will not work as VB will treat studenti and scorei as two variables.

Chapter87 Arrays n VB provides a data structure called an array. n The variables names will be similar to those in the Input # statement. They will be student(1), student(2), student(3), student(30) score(1),score(2),score(3) score(30). n This collections of variables as the array variables student( ) and score( ). n The numbers inside the parenthese of the individual variables are called subscripts. n Each individual variable is called a subscripted variable or element. score ()score(2) score(3) score(30) n score()

Chapter88 Arrays n The statement of the array will be –Dim student(1 To 30) As String –Dim score (1 To 30) As Integer n For example, the following program creates a string array consisting of the names of the first fie World Series winners. teamName(1) teamName(2) teamName(3) teamName(4) n teamName() Red Sox Giants White Sox Cubs

Chapter89 Arrays 'Create array for five strings Dim teamName(5) As String Private Sub cmdWhoWon_Click() Dim n As Integer 'Fill array with World Series Winners teamName(1) = "Red Sox" teamName(2) = "Giants" teamName(3) = "White Sox" teamName(4) = "Cubs" teamName(5) = "Cubs" 'Access array of five strings n = Val(txtNumber.Text) picWinner.Cls picWinner.Print "The "; teamName(n); " won World Series number"; n End Sub

Chapter810 Arrays n When the statement Dim arrayName(1 To n) As varType is placed inside an event procedure or general procedure, then space for n subscripted variables is set aside in memory each time the procedure is invoked and released when procedure is exited. n For example: Private Sub cmdShow_Click() Dim total As Integer, student As Integer, average As Single Dim nom(1 To 8) As String, score(1 To 8) As Integer Open App.Path & "\SCORES.TXT" For Input As #1

Chapter811 Arrays n In some applications, some arrays whose size was not know before the program was run. n ReDim statements allowed us to create an unknown size arrays n For example: Private Sub cmdShow_Click() Dim numStudents As Integer, nTemp As String, sTemp As Integer Dim student As Integer, total As Integer, average As Single numStudents = 0 Open App.Path & "\SCORES.TXT" For Input As #1 Do While Not EOF(1) Input #1, nTemp, sTemp numStudents = numStudents + 1 Loop Close #1 ReDim nom(1 To numStudents) As String, score(1 To numStudents) As Integer

Chapter812 Arrays n Some applications required form level arrays whose size is not known in advance n Dim statements cannot use variables or expressions to specify the subscript range. n The solution would be Dim arrayName() As var Type n For example, Dim team() As string Dim teamName() As string

Chapter813 Arrays n An entire array can be passed to another procedure. n For example: Private Sub cmdDisplay_Click() 'Pass array to a Sub procedure and a Function procedure Dim score(1 To 10) As Integer Call FillArray(score()) picAverage.Cls picAverage.Print "The average score is"; Sum(score()) / 10 End Sub Private Sub FillArray(s() As Integer) End Sub

Chapter814 Control Arrays n VB provides a means of constructing arrays of text boxes, labels, command button n Because text boxes, labels, and command buttons are referred to generically in VB as controls, arrays of this objects are called control arrays. n For example: txtBox(0).Text = value txtBox(1).Text = value n Explain p.339 example n Explain p.341 example 7.3.2

Chapter815 Exercise n P.282 Exercise 6.3 No. 13,15,17,23 n P.315 Exercise 7.1 No. 1, 3, 5, 17 n P.336 Exercise 7.2 No. 13 n P.349 Exercise 7.3 No. 35