0% found this document useful (0 votes)
24 views49 pages

LOOPS

notes

Uploaded by

gladys temwo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views49 pages

LOOPS

notes

Uploaded by

gladys temwo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 49

REPETITION STRUCTURES IN

VB
DEFINITION

• Visual Basic loop structures allow you to run one or


more lines of code repetitively. You can repeat the
statements in a loop structure until a condition is True,
until a condition is False, a specified number of times, or
once for each element in a collection.

• The loop repeats until some specified condition at the


beginning or end of the loop is met
While/wend repetition control
structure
• This is a repetition control structure that allows
programmer to specify that an action is to be
repeated based on a true or false condition.

• The condition given may be true or false. If the


condition is false the action stops being executed,
but if the condition is true the statements will be
repeated until the condition turns false.
While..Wend loop CONT’

• In a While..Wend loop, if the condition is True, all


statements are executed until Wend keyword is
encountered.
• If the condition is false, the loop is exited and the control
jumps to very next statement after Wend keyword.
Syntax

• While condition(s)
• [statements 1]
• [statements 2] ...
• [statements n]
• Wend
• OR
• While condition
• [expression or statements]
• Wend
FLOW CHART
Example 1 Printing numbers from 1
to 1000
PROGRAM EXAMPLE

• Private Sub Command1_Click()


• Dim x As Integer
•x=1
• While x <= 10
• Print x
• x = x +1
• Wend
• End Sub
EXAMPLE 2:The following
While...Wend counts from 1 to 10
• Private Sub Command1_Click()
• Dim number As Integer

• number = 1
• While number <= 10
• Print number
• number = number + 1
• Wend
• End Sub
Do until/loop repetition control
structure

•The Do until loop repetition control


structure repeats until a condition turns
false. The statements in the body of a
loop are repeatedly executed as long as
the loop continuation evaluates to false
DO-UNTIL CONT’

• Unlike the Do While...Loop and While...Wend repetition structures,


the Do Until... Loop structure tests a condition for falsity. Statements
in the body of a Do Until...Loop are executed repeatedly as long as
the loop-continuation test evaluates to False.
SYNTAX

• Do
• Block of one or more VB statements
• Loop Until condition
FLOW CHART-DO UNTIL
example

• Private Sub Command1_Click()


• Dim i As Integer
• i = 10
• Do Until i > 15 'Condition is False.Hence loop will be executed
• i=i+1
• ' MsgBox ("The value of i is : " & i)
• Print i
• Loop
• End Sub
• Private Sub Command1_Click()
• Dim number As Long
• number = 0
• Do Until number > 10

• number = number + 1
• Print number
• Loop
• End Sub
Alternate Syntax

• The alternate syntax for Do...Until loop which checks the condition at
the end of the loop.

• Do
• [statement (s)]
• Loop Until condition

• It checks the condition at the end of the loop. The statements inside
the loop are executed at least once, even if the condition is True.
Flow chart
Example

• Private Sub Command1_Click()


• Dim i As Integer
• i = 10
• Do
• i=i+1
• 'MsgBox "The value of i is : " & i
• Print i
• Loop Until i < 15 'Condition is True.Hence loop is executed once.

• End Sub
Do------while loop

• the do-while loop is same as the while loop,


but the only difference is while loop will
execute the statements only when the
defined condition returns true, the do-while
loop will execute the statements at least
once because first it will execute the block of
statements and then it will checks the
condition.
do-while loop

•in Visual Basic Do and While keywords


are useful to create a do...while loop.
•A do-while loop in Visual Basic
programming language is used to
execute the block of statements till the
defined condition evaluates as false.
• The statements of the Do-While loop will execute first; after that, the
boolean_expression will be evaluated. If boolean_expression returns
true, the statements inside the Do-While loop will execute again.
• If boolean_expression is false, the Do-While loop will stop the execution
of statements, and the program execution will come out of the loop.
DO-WHILE LOOP

• Do while <conditions>
• Statements
• Loop

• Its equivalent to IF---then selection structure


• Its executed as long as the condition is true
• If condition is false then the next statement after the loop is executed
Do---while loop flow chart
• Private Sub Command1_Click()
• Dim number As Integer
• number = 1
• Do While number <= 20
• Print number
• number = number + 1
• Loop
• End Sub
Do...Loop While Statement

• The Do...Loop While statement first executes the statements and


then test the condition after each execution

• Syntax
Do
Statements(s)
While (condition)
Do loop while
example

• Private Sub Command1_Click()


• Dim counter As Integer
• counter = 1
• Do
• Print counter
• counter = counter + 1
• Loop While counter >= 10

• End Sub
With Statement

• The With statement is not a loop, but can be as useful as a loop.


• syntax
• With object
• [Statements]
• End With
example

• Private Sub Command1_Click()

• With Text1
• .Width = 2500
• .Height = 800
• .ForeColor = vbRed
• .Text = "NotesforMsc"
• End With

• End Sub
•For next loop
For next loop

• Used to execute one or more statements for predetermined number


of times

• For Counter = Start To End


• Statement(s)
• Next Counter
For - next

• The expression begins counting at the Start


point. Then it examines whether the current
value (after starting to count) is less than or
equal to End. If that's the case, it then
executes the Statement (s). Next, it
increments the value of Counter by 1 and
examines the condition again. This process
goes on until the value of Counter becomes
greater than the End value then the looping
stop
Example program without for –next
loop
• Private Sub Command1_Click()
• Print "Hello"
• Print "Hello"
• Print "Hello"
• Print "Hello"
• Print "Hello"
• End Sub
Example with for-next loop
Dim x As Integer

'this will help to display results on form


Form1.Show 'the For..loop
For x = 1 To 5
Print "hello"
Next x
End Sub
Print the number from 50 to 100

• Private Sub Command1_Click()


• Dim x As Integer
• For x = 50 To 100
• Print x
• Next x

• End Sub
Find the summation of number from 1 to 10

• Private Sub Command1_Click()


• Dim x As Integer, sum As Integer
• sum = 0
• For x = 1 To 10
• sum = sum + x
• List1.AddItem x
• Next x
• Print "Sum of 1 to 10 is:"; sum
• End Sub
 The above coding will display numbers vertically on the form. In order to display numbers
horizontally the following method can be used.

• Private Sub Command1_Click()


• Dim x As Integer
• For x = 1 To 10
• Print x & Space$(2);
• Next
• End Sub
• To increase the space between the numbers increase the value inside
the brackets after the & Space$.
In the output instead of number 4
you will get the "This is number 4".
• Private Sub Command1_Click()
• Dim number
• For number = 1 To 10
• If number = 4 Then
• Print "This is number 4“
• Exit for
• Else
• Print number
• End If
• Next
• End Sub
Loop that exits after several outputs

• Private Sub Command1_Click()


• Dim number
• For number = 1 To 10
• If number = 4 Then
• Print "This is number 4"

• Exit For

• End If
• Print number
• Next
USING STEP KEYWORD

• The use of STEP in For.. Next loop


STEP allows you to skip a loop
Dim x As Integer
Form1.Show
For x = 1 To 10 Step 2
Print x
Next x
PROGRAM TO ADD ITEMS INTO A
LIST BOX
Private Sub Command1_Click()
Dim counter As Integer
For counter = 1 To 10
List1.AddItem counter
Next
End Sub
PROGRAM TO ADD ITEMS IN A FOR
LOOP
• Private Sub Command1_Click()
• Dim COUNTER As Integer, SUM As Integer
• SUM = 0
• For COUNTER = 1 To 10 Step 2

• SUM = SUM + COUNTER


• ‘If COUNTER > 6 Then
• ‘Exit For
• ‘Else
• Print COUNTER
• ‘End If
• Next
• Print "SUM IS", SUM
• End Sub
Program to print even numbers
using a for loop
Private Sub Command1_Click()

Dim x As Integer, sum As Integer


For x = 1 To 10
If x Mod 2 = 0 Then
Print x
End If

Next

End Sub
Sum of even numbers

Private Sub Command1_Click()

Dim x As Integer, sum As Integer


sum = 0
For x = 1 To 10
If x Mod 2 = 1 Then
Print x
End If
sum = sum + x

Next

Print "sum is :", sum


End Sub
A program to find sum of three
numbers entered by the user.
• Private Sub Command1_Click()
• Dim I As Integer, sum As Integer, num As Integer
• sum = 0
• For I = 1 To 3
• num = InputBox("enter numbers", "numbers", 7)
• sum = sum + num
• Print num
• Next I

• Print "sum is", sum


• End Sub
Nested loop
This allows you to have one loop or more running inside another

Private Sub Command1_Click()


Dim i As Integer, j As Integer
For i = 1 To 4
For j = 1 To 2
Print j;
Next
Print i
Next

End Sub
Program that steps the for loop

• Private Sub Command1_Click()


• Dim i As Integer, sum As Integer, j As Integer
• For i = 1 To 3
• For j = 3 To i Step -1
• Print j;
• Next
• Print
• Next
• End Sub
Nested for to produce a pattern
• Private Sub Form_Load()
• Form1.Show
• Dim x, y As Integer
• For x = 1 To 10 Step 2
• For y = 1 To x
• Picture1.Print "*";
• Next y
• Picture1.Print
• Next x
• End Sub

You might also like