What are the different types of loops? ◦ Do….While Performs statements within loop while a...

40
Looping in VB

Transcript of What are the different types of loops? ◦ Do….While Performs statements within loop while a...

Page 1: What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.

Looping in VB

Page 2: What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.

What are the different types of loops?◦ Do….While

Performs statements within loop while a condition is true◦ Do….Until

Performs statements within loop until a condition is true◦ For….Next

Performs statements within the loop a pre-determined number of times

Within these there are two styles◦ Post-test

Code is executed at least once◦ Pre-test

Code may not get execute.

Just to Review

Page 3: What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.

Can be top controlled or bottom controlled Top Controlled syntax

Do While condition Body of loopLoop

Bottom Controlled syntaxDoBody of loopLoop while condition

Looping in VB ~ Do…While

Page 4: What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.

Top-Controlled Do While Loops

Page 5: What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.

Bottom-Controlled Do While Loop

Page 6: What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.

Much like the Do…While can be top or bottom controlled

Top ControlledDo until conditionBody of loopLoop

Bottom ControlledDoBody of loopLoop until condition

Loop in VB ~ Do….Until

Page 7: What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.

Repeating a Process Using the For…Next Loop You can use a For...Next loop when a section

of code is to be executed an exact number of times

Page 8: What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.

Repeating a Process Using the For…Next Loop

Page 9: What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.

Priming the Loop Starting a loop with a preset value in the

variable(s) tested in the condition is called priming the loop

Page 10: What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.

10

Using a DataTip with Breakpoints Resolving defects in code is called

debugging Breakpoints allow us to stop the code where

ever we want so that we can examine what is going on with our variables.

While in break mode, you can examine the values in all

Page 11: What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.

Chapter 6: Loop Structures 11

Accumulators & Counters

A variable that contains an accumulated value such as the total of all the speeds is called an accumulator

A variable that always is incremented by a constant value is called a counter◦ How many vehicle speeds the user has entered

Page 12: What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.

Chapter 6: Loop Structures 12

Compound Operators

A compound operator allows you to add, subtract, multiply, divide, use modulus or exponents, or concatenate strings, storing the result in the same variable

Page 13: What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.

Chapter 6: Loop Structures 13

Examples

Page 14: What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.

Chapter 6: Loop Structures 14

Examples

Page 15: What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.

15

MenuStrip Object A menu bar is a strip across the top of a

window that contains one or more menu names

A menu is a group of commands, or items, presented in a list

Page 16: What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.

Chapter 6: Loop Structures 16

MenuStrip Object

Page 17: What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.

Chapter 6: Loop Structures 17

Event Handlers for Menu Items Work the same for buttons – double click on

the menu item and the code window should pop up

Page 18: What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.

18

Standard Items for a Menu Action Tag allows you to create a full standard

menu bar commonly provided in Windows programs

Can specify a set of actions, called smart actions, for an object as you design a form

To do this place a new MenuStrip object on your form

Click the Action Tag on the MenuStrip object Click Insert Standard Items on the MenuStrip Tasks

menu Click File on the menu bar to view the individual

menu items and their associated icons on the File menu

Page 19: What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.

19

Standard Items for a Menu

Page 20: What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.

20

InputBox Function The InputBox function displays a dialog box,

that asks the user for some type of input. The User can either click on OK or Cancel When the user enters the text the InputBox

function returns this text as a string If the user clicks the Cancel button, the

function returns a null string ("")

Page 21: What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.

Chapter 6: Loop Structures 21

InputBox Object Default Value The InputBox object can be assigned a

default value

Page 22: What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.

22

Example

Page 23: What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.

23

List Boxes List boxes can be used to display items for a

user to pick from OR receive output from a program

Page 24: What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.

24

Adding Items During Design Click the Items property in the Properties window Click the ellipsis button in the right column of the

Items property Click in the String Collection Editor window. Type

in whatever values you want and click on the OK button

Page 25: What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.

Selected Item Property When we use a list box to display a set of

values for the user to pick from, when they pick their item, we need to bring it in to the code

Page 26: What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.

ListBox Items.Count Property This property returns an integer with the

number of entries stored in the Items property

Example of use:

The number of entries in the list can be assigned to an integer variable

If lstEmployees.Items.Count = 0 ThenMessageBox.Show("The list has no items!")

End If

numEmployees = lstEmployees.Items.Count

Page 27: What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.

Item Indexing The Items property values can be accessed

from your VB code Each item value is given a sequential index

◦ The first item has an index of 0◦ The second item has an index of 1, etc.

Example:

name = lstCustomers.Items(2)' Access the 3rd item value

Page 28: What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.

ListBox SelectIndex Property

The SelectIndex property returns an integer with the index of the item selected by the user

If no item is selected, the value is set to -1 (an invalid index value)

Can use SelectIndex to determine if an item has been selected by comparing to -1

Example:If lstLocations.SelectedIndex <> -1 Then

location = lstLocations.Items(lstLocations.SelectedIndex)End If

Page 29: What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.

ListBox SelectedItem Property Instead of using the SelectedIndex property

as follows:

The SelectedItem property can be used to retrieve the value of a selected item as follows:

If lstMonths.SelectedIndex <> -1 Thenmonth = lstMonths.Items(lstMonths.SelectedIndex)

End If

If lstMonths.SelectedIndex <> -1 Thenmonth = lstMonths.SelectedItem.ToString)

End If

Page 30: What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.

ListBox Sorted Property Sorted is a boolean property When set to true, values in the Items

property are displayed in alphabetical order When set to false, values in the Items

property are displayed in the order they were added

Page 31: What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.

ListBox Items.Add Method Items can be added to the end of a ListBox

list in your VB code using the Add method Format is ListBox.Items.Add(Item)

ListBox is the name of the control Item is a string value to add to the Items

property Example:

lstStudents.Items.Add("Sharon")

Page 32: What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.

ListBox Items.Insert Method Items can be added at a specific position of

a ListBox in VB code using the Insert methodListBox.Items.Insert(Index, Item)

Index specifies position where Item is placed

Index is zero based similar to SelectedIndex property

Items that follow are “pushed” down Example inserting "Jean“ as the 3rd itemlstStudents.Items.Insert(2, "Jean")

Page 33: What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.

ListBox Methods to Remove Items ListBox.Items.RemoveAt(Index)

◦ Removes item at the specified index ListBox.Items.Remove(Item)

◦ Removes item with value specified by Item ListBox.Items.Clear()

◦ Removes all items in the Items property Examples:lstStudents.Items.RemoveAt(2) ‘remove 3rd itemlstStudents.Items.Remove(“Jean”) ‘remove item JeanlstStudents.Items.Clear() ‘remove all items

Page 34: What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.

34

Using a DataTip with Breakpoints Right-click where ever you wish to put your

breakpoint, point to breakpoint on the short cut menu.

To run and test the program with the breakpoint, click the Start Debugging button on the Standard toolbar

To see the value of the variable hover your mouse over the variable declaration and you will see the current value.

Page 35: What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.

Using a DataTip with Breakpoints

Page 36: What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.

36

Using a DataTip with Breakpoints To remove a breakpoint, right-click the

statement containing the breakpoint, and then point to Breakpoint on the shortcut menu

Click Delete Breakpoint on the Breakpoint submenu

Page 37: What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.

37

Creating the Executable

After you complete your application, how do we create the executable or deploy it?

Deploying a project means placing an executable version of the program on your hard disk, on a Web server, or on a network server

You can create a deployed program by using ClickOnce Deployment

The deployed version of the program you create can be installed and executed on any computer that has the .NET framework installed

Page 38: What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.

38

Publishing an Application

With the program open, click Build on the menu bar

Click Publish Radar on the Build menu Change the default location from publish\ to a

file location. Click the Next button. If necessary, click the

From a CD-ROM or DVDROM radio button Click the Next button. If necessary, click the

The application will not check for updates radio button

Click on next and then finish

Page 39: What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.

Chapter 6: Loop Structures 39

Installing Your Software

To install the application, double-click the setup file

After installation, the program will run. To run the installed application again, click the Start button on the Windows taskbar. Point to All Programs, click Radar on the All Programs menu, and then click Radar on the Radar submenu

Page 40: What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.

40

Creating the Executable