Vous êtes sur la page 1sur 36

COLLEGE OF COMPUTER STUDIES

PREPARED BY: P. FERNANDEZ, A.K. RACELIS


Computer Programming
2
LOOPS
Loops
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.

Programming languages provide various control structures that allow for more
complicated execution paths.
Loops
A loop statement allows us to execute a
statement or group of statements multiple times
and following is the general form of a loop
statement in most of the programming
languages:
Loops
A loop statement allows us to execute a
statement or group of statements multiple times
and following is the general form of a loop
statement in most of the programming
languages:
VB.Net provides following types of loops
Loop Type Description
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.
For...Next 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.
For It repeats a group of statements for each element in a collection. This
Each...Next loop is used for accessing and manipulating all elements in an array or
a VB.Net collection.
VB.Net provides following types of loops
Loop Type Description
While... End It executes a series of statements as long as a given condition is
While True.
With... End It is not exactly a looping construct. It executes a series of
With statements that repeatedly refer to a single object or structure.
Nested loops You can use one or more loops inside any another While, For or
Do loop.
Loop Control Statements:
Loop control statements change execution from its normal sequence. When execution
leaves a scope, all automatic objects that were created in that scope are destroyed.
Control Statement Description
Exit statement Terminates the loop or select case statement and transfers execution
to the statement immediately following the loop or select case.
Continue Causes the loop to skip the remainder of its body and immediately
statement
retest its condition prior to reiterating.
GoTo statement Transfers control to the labeled statement. Though it is not advised to
use GoTo statement in your program.
Loop Control Statements:
Loop control statements change execution from its normal sequence. When execution
leaves a scope, all automatic objects that were created in that scope are destroyed.
Control Statement Description
Exit statement Terminates the loop or select case statement and transfers execution
to the statement immediately following the loop or select case.
Continue Causes the loop to skip the remainder of its body and immediately
statement
retest its condition prior to reiterating.
GoTo statement Transfers control to the labeled statement. Though it is not advised to
use GoTo statement in your program.
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.
The syntax for this loop construct is:
Do { While | Until } condition Do
[ statements ] [ statements ]
[ Continue Do ] [ Continue Do ]
[ statements ] -or- [ statements ]
[ Exit Do ] [ Exit Do ]
[ statements ] [ statements ]
Loop Loop { While | Until } condition
The syntax for this loop construct is:
Do...UNTIL Loop:

A Do...Loop Until loop is a loop that runs until the loop's condition is true, the
condition being checked after each iteration of the loop.

Sample: Example shows the DO..Until loop:


' DO..UNTIL loop in VB.NET
Do Do Until i > 10
statements Console.WriteLine(i)
Loop Until condition i=i+1
Loop
The syntax for this loop construct is:
Do...Loop While loop:

A Do...Loop While loop runs until the loop's condition becomes false. Its
condition is checked after each iteration of the loop.

Sample: Example shows the DO..Until loop:


'DO..LOOP WHILE in VB.NET
Do Do
statements Console.WriteLine(i)
Loop While condition i=i+1
Loop While i < 10
The Do Loop statements have four different forms
a) Do While condition
Block of one or more VB statements
Loop
c) Do Until condition
b) Do Block of one or more VB statements
Block of one or more VB statements Loop
Loop While condition
d) Do
Block of one or more VB statements
Loop Until condition
Examples:
Module loops
Sub Main() value of a: 10
' local variable definition value of a: 11
Dim a As Integer = 10 value of a: 12
'do loop execution value of a: 13
Do value of a: 14
Console.WriteLine("value of a: {0}", a) value of a: 15
a=a+1 value of a: 16
Loop While (a < 20) value of a: 17
Console.ReadLine() value of a: 18
End Sub
value of a: 19
End Module
Examples:
Module loops
Sub Main() value of a: 10
' local variable definition value of a: 11
Dim a As Integer = 10 value of a: 12
'do loop execution value of a: 13
Do value of a: 14
Console.WriteLine("value of a: {0}", a) value of a: 15
a=a+1 value of a: 16
Loop Until (a = 20) value of a: 17
Console.ReadLine() value of a: 18
End Sub
value of a: 19
End Module
Examples:
Do while counter <=1000 Do
num.Text=counter num.Text=counter
counter =counter+1 counter=counter+1
Loop Loop until counter>1000

* The above example will keep on adding until counter >1000.


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.
For...Next Loop
FOR...Next loop

A For loop iterates a certain number of times, the value of the counter
variable changing each iteration.

A for loop has the following syntax:

For counter = start to end [step increment]


statements
Next
For...Next Loop
In executing a FOR..NEXT loop, Visual Basic completed the following step
Sets counter equal to start.
Tests to see if counter is greater than end, so it exit the loop if
counter is less than end. if it is, it exits the loop.
Executes the statement in block.
Repeats the statements
Examples:
Example shows the FOR loop: Example A
For counter=1 to 10
Dim i As Integer display.Text=counter
Next
For i = 1 To 10
Example B
Console.WriteLine(i) For counter=1 to 1000 step 10
counter=counter+1
Next Next

Example C
For counter=1000 to 5 step -5
counter=counter-10
Next
Examples:
Module loops
value of a: 10
Sub Main()
value of a: 11
Dim a As Byte
value of a: 12
' for loop execution
value of a: 13
For a = 10 To 20
value of a: 14
Console.WriteLine("value of a: {0}", a)
value of a: 15
Next
value of a: 16
Console.ReadLine()
value of a: 17
End Sub
value of a: 18
End Module
value of a: 19
Examples:
Module loops
value of a: 10
Sub Main()
value of a: 12
Dim a As Byte
value of a: 14
' for loop execution
value of a: 16
For a = 10 To 20 Step 2
value of a: 18
Console.WriteLine("value of a: {0}", a)
value of a: 20
Next
Console.ReadLine()
End Sub
End Module
Examples: Exit a Loop
For n=1 to 10
If n>6 then
Exit For
End If
Else
Print n
End If
Nested For...Next Loop
When you have a loop within a loop, then you have created a nested loop.

You can actually have as many loops as you want in a nested loop provided the
loops are not the never-ending type.

For a nested loop that consists of two loops, the first cycle of the outer loop will be
processed first, then it will process the whole repetitive process of the inner loop,
then the second cycle of the outer loop will be processed and again the whole
repetitive process of the inner loop will be processed.

The program will end when the whole cycle of the outer loop is processed.
The Structure of a nested loop
For counter1=startNumber to endNumber
(Step increment)
For counter2=startNumber to endNumber
(Step increment)
One or more VB statements
Next counter2
Next counter1
The Structure of a nested loop
Private Sub Form_Activate ( )
For firstCounter= 1to 5
Print Hello
For secondCounter=1 to 4
Print Welcome to the VB tutorial
Next secondCounter
Next firstCounter
Print Thank you
End Sub
For Each...Next Loop
It repeats a group of statements The syntax for this loop construct is:
for each element in a collection.
This loop is used for accessing For Each element [ As datatype ] In group
and manipulating all elements in [ statements ]
an array or a VB.Net collection. [ Continue For ]
[ statements ]
[ Exit For ]
[ statements ]
Next [ element ]
For Each...Next Loop
Example:
Output
Module loops
Sub Main() 1
Dim anArray() As Integer = {1, 3, 5, 7, 9} 3
Dim arrayItem As Integer
'displaying the values 5
For Each arrayItem In anArray
Console.WriteLine(arrayItem) 7
Next 9
Console.ReadLine()
End Sub
End Module
While... End While Loop
It executes a series of statements as long as a given condition is True.
The syntax for this loop construct is:
While condition Statement(s) may be a single statement or a block of
[ statements ] statements.
[ Continue While ] The condition may be any expression, and true is
[ statements ] logical true.
[ Exit While ] The loop iterates while the condition is true.
[ statements ] When the condition becomes false, program control
End While passes to the line immediately following the loop.
While... End While Loop
Here, key point of the While loop is that the
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.
While... End While Loop
The While...End While loop executes a block of statement as long as
condition is true.
The While loop has the following syntax:
While condition
statement-block
' While...End While loop in VB.NET
End While
While i < 10
Console.WriteLine(i)
i=i+1
End While
Example:
Module loops
value of a: 10
Sub Main()
Dim a As Integer = 10 value of a: 11
' while loop execution ' value of a: 12
While a < 20 value of a: 13
Console.WriteLine("value of a: {0}", a) value of a: 14
a=a+1 value of a: 15
End While value of a: 16
Console.ReadLine() value of a: 17
End Sub value of a: 18
End Module value of a: 19
With... End With Statement
It is not exactly a looping construct. It executes a series of statements that
repeatedly refers to a single object or structure.

The syntax for this loop construct is:


With object
[ statements ]
End With
Example:
Module loops Sub Main()
Public Class Book Dim aBook As New Book
Public Property Name As String With aBook
Public Property Author As String .Name = "VB.Net Programming"
Public Property Subject As String .Author = "Zara Ali"
End Class .Subject = "Information Technology"
End With
With aBook
Console.WriteLine(.Name)
Console.WriteLine(.Author)
Console.WriteLine(.Subject)
End With VB.Net Programming
Console.ReadLine()
Zara Ali
End Sub
End Module Information Technology
References:
http://www.dotnetheaven.com/article/loop-statements-in-vb.net
https://www.tutorialspoint.com/vb.net/vb.net_loops.htm
http://www.homeandlearn.co.uk/NET/nets3p2.html
http://www.vbtutor.net/lesson9.html

Vous aimerez peut-être aussi