vb.net loops: 3rd october 2016 · vb.net – loops: 3rd october ... • in general, statements are...

26
VB.Net – Loops: 3 rd October 2016 There may be a situation when you need to execute a block of code several number of times. In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on.

Upload: others

Post on 29-May-2020

11 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: VB.Net Loops: 3rd October 2016 · VB.Net – Loops: 3rd October ... • In general, statements are executed sequentially: The first statement in a function is executed first, followed

VB.Net – Loops: 3rd October 2016

• There may be a situation when you need to execute a block of code several number of times.

• In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on.

Page 2: VB.Net Loops: 3rd October 2016 · VB.Net – Loops: 3rd October ... • In general, statements are executed sequentially: The first statement in a function is executed first, followed

• A loop statement allows us to execute a statement or group of statements multiple times

• the general form of a loop statement in most of the programming languages is:

Page 3: VB.Net Loops: 3rd October 2016 · VB.Net – Loops: 3rd October ... • In general, statements are executed sequentially: The first statement in a function is executed first, followed

General Form of a Loop

Page 4: VB.Net Loops: 3rd October 2016 · VB.Net – Loops: 3rd October ... • In general, statements are executed sequentially: The first statement in a function is executed first, followed

Types of Loops in VB.Net

• Do Loop:

-It repeats the enclosed block of statements while a Boolean condition is True or until the condition becomes True.

-It could be terminated at any time with the Exit Do statement.

Page 5: VB.Net Loops: 3rd October 2016 · VB.Net – Loops: 3rd October ... • In general, statements are executed sequentially: The first statement in a function is executed first, followed

Do Loop: Syntax 1

Do { While | Until } condition

[ statements ]

[ Continue Do ]

[ statements ]

[ Exit Do ]

[ statements ]

[Loop]

Page 6: VB.Net Loops: 3rd October 2016 · VB.Net – Loops: 3rd October ... • In general, statements are executed sequentially: The first statement in a function is executed first, followed

Do Loop: Syntax 2

Do [ statements ]

[ Continue Do ]

[ statements ]

[ Exit Do ]

[ statements ]

Loop { While | Until } condition

• NB: This will always execute at least once. Why?

Page 7: VB.Net Loops: 3rd October 2016 · VB.Net – Loops: 3rd October ... • In general, statements are executed sequentially: The first statement in a function is executed first, followed

Example - Do … While Loop Module Module1 Sub Main() ' local variable definition Dim a As Integer = 10 'do loop execution Do Console.WriteLine("value of a: {0}", a) a = a + 1 Loop While (a < 20) Console.ReadLine() End Sub End Module

Page 8: VB.Net Loops: 3rd October 2016 · VB.Net – Loops: 3rd October ... • In general, statements are executed sequentially: The first statement in a function is executed first, followed

For …Next Loop

• It repeats a group of statements a specified number of times and

• a loop index counts the number of loop iterations as the loop executes

Page 9: VB.Net Loops: 3rd October 2016 · VB.Net – Loops: 3rd October ... • In general, statements are executed sequentially: The first statement in a function is executed first, followed

For…Next Syntax

For counter [ As datatype ] = start To end [ Step step ]

[ statements ]

[ Continue For ]

[ statements ]

[ Exit For ]

[ statements ]

Next [ counter ]

Page 10: VB.Net Loops: 3rd October 2016 · VB.Net – Loops: 3rd October ... • In general, statements are executed sequentially: The first statement in a function is executed first, followed

Flow Diagram

Page 11: VB.Net Loops: 3rd October 2016 · VB.Net – Loops: 3rd October ... • In general, statements are executed sequentially: The first statement in a function is executed first, followed

Example- For Next

Module Module1

Sub Main() Dim a As Integer ' for loop execution For a = 10 To 20 Console.WriteLine("value of a: {0}", a) Next Console.ReadLine() End Sub End Module

Page 12: VB.Net Loops: 3rd October 2016 · VB.Net – Loops: 3rd October ... • In general, statements are executed sequentially: The first statement in a function is executed first, followed

For each…next

• It repeats a group of statements for each element in a collection. This loop is used for accessing and manipulating all elements in an array or a VB.Net collection.

Page 13: VB.Net Loops: 3rd October 2016 · VB.Net – Loops: 3rd October ... • In general, statements are executed sequentially: The first statement in a function is executed first, followed

Syntax

For Each element [ As datatype ] In group

[ statements ]

[ Continue For ]

[ statements ]

[ Exit For ]

[ statements ]

Next [ element ]

Page 14: VB.Net Loops: 3rd October 2016 · VB.Net – Loops: 3rd October ... • In general, statements are executed sequentially: The first statement in a function is executed first, followed

Example Module Module1

Sub Main() Dim anArray() As Integer = {1, 3, 5, 7, 9} Dim arrayItem As Integer 'displaying the values For Each arrayItem In anArray Console.WriteLine(arrayItem) Next Console.ReadLine() End Sub End Module

Page 15: VB.Net Loops: 3rd October 2016 · VB.Net – Loops: 3rd October ... • In general, statements are executed sequentially: The first statement in a function is executed first, followed

While ….End While Loop

• It repeats the enclosed block of statements while a Boolean condition is True or until the condition becomes True.

• It could be terminated at any time with the Exit Do statement.

Page 16: VB.Net Loops: 3rd October 2016 · VB.Net – Loops: 3rd October ... • In general, statements are executed sequentially: The first statement in a function is executed first, followed

Syntax

While condition

[ statements ]

[ Continue While ]

[ statements ]

[ Exit While ]

[ statements ]

End While

Page 17: VB.Net Loops: 3rd October 2016 · VB.Net – Loops: 3rd October ... • In general, statements are executed sequentially: The first statement in a function is executed first, followed

• statement(s) may be a single statement or a block of statements.

• The condition may be any expression, and true is logical true.

• The loop iterates while the condition is true.

• When the condition becomes false, program control passes to the line immediately following the loop

• NB: While loop loop might not ever run. When the condition is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed

Page 18: VB.Net Loops: 3rd October 2016 · VB.Net – Loops: 3rd October ... • In general, statements are executed sequentially: The first statement in a function is executed first, followed

Example – While Loop

Module Module1 Sub Main() Dim a As Integer = 10 ' while loop execution ' While a < 20 Console.WriteLine("value of a: {0}", a) a = a + 1 End While Console.ReadLine() End Sub

End Module

Page 19: VB.Net Loops: 3rd October 2016 · VB.Net – Loops: 3rd October ... • In general, statements are executed sequentially: The first statement in a function is executed first, followed

Nested Loops

• VB.Net allows using one loop inside another loop (Nesting)

• Following section shows few examples to illustrate the concept:

Page 20: VB.Net Loops: 3rd October 2016 · VB.Net – Loops: 3rd October ... • In general, statements are executed sequentially: The first statement in a function is executed first, followed

Nested For Loop- Syntax

For counter1 [ As datatype1 ] = start1 To end1 [ Step step1 ]

For counter2 [ As datatype2 ] = start2 To end2 [ Step step2 ]

...

Next [ counter2 ]

Next [ counter 1]

Page 21: VB.Net Loops: 3rd October 2016 · VB.Net – Loops: 3rd October ... • In general, statements are executed sequentially: The first statement in a function is executed first, followed

Nested While Loop- Syntax

While condition1 While condition2

...

End While

End While

Page 22: VB.Net Loops: 3rd October 2016 · VB.Net – Loops: 3rd October ... • In general, statements are executed sequentially: The first statement in a function is executed first, followed

Nested Do While - Example

Do { While | Until } condition1 Do { While | Until } condition2

...

Loop

Loop

NB: you can put any type of loop inside of any other type of loop. For example, a for loop can be inside a while loop or vice versa.

Page 23: VB.Net Loops: 3rd October 2016 · VB.Net – Loops: 3rd October ... • In general, statements are executed sequentially: The first statement in a function is executed first, followed

Find prime numbers btwn 2 & 100 Module Module1 Sub Main() ' local variable definition Dim i, j As Integer For i = 2 To 100 For j = 2 To i ' if factor found, not prime If ((i Mod j) = 0) Then Exit For End If Next j If (j > (i \ j)) Then Console.WriteLine("{0} is prime", i) End If Next i Console.ReadLine() End Sub End Module

Page 24: VB.Net Loops: 3rd October 2016 · VB.Net – Loops: 3rd October ... • In general, statements are executed sequentially: The first statement in a function is executed first, followed

With…End With

• It is not a looping construct actually.

• It executes a series of statements that repeatedly refers to a single object or structure

The syntax is:

With object

[ statements ]

End With

Page 25: VB.Net Loops: 3rd October 2016 · VB.Net – Loops: 3rd October ... • In general, statements are executed sequentially: The first statement in a function is executed first, followed

Example - With…End With Module Module1 'class definition Public Class Book Public Property Name As String Public Property Author As String Public Property Subject As String End Class Sub Main() Dim aBook As New Book 'object declaration 'with block With aBook .Name = "VB.Net Programming" .Author = "Nyabiko" .Subject = "Information Technology" End With With aBook Console.WriteLine("Book Title :" + .Name) Console.WriteLine("Author Name :" + .Author) Console.WriteLine("Subject Area :" + .Subject) End With Console.ReadLine() End Sub