You are on page 1of 8

Lecture 9 :looping

Loop statement
Visual basic supports statement to perform loops. The loops statements could
• have different structures as follows:
• 1- Counter loop.
• 2- Conditional loop.
• 1- Counter loop:
• Loops apply programming statements for fixed number of times using counter
• (for… next) statement.
• The general form is:
• For variable = start value to end value step step value
• Statements
• Next variable
Example1: Write a program to print (hello)
five times.
• Sol:
• Dim i as integer
• Private Sub Command1_Click ()
• For i = 1 To 5
• Print "hello"
• Next i
• End Sub
Example2: Write a program to print even
numbers from 1 to 10.
• Sol:
• Dim i as integer
• Private Sub
Command1_Click ()
• For i = 2 To 10 step 2
• Print i
• Next i
• End Sub
Notes:
• 1-The variable's value that we use as counter must be integer value (integer,
long).
• 2- If we don’t determined the step value then the assumed value is 1.
• 3- If the final value smallest than the initial value, then the step value must be
• negative.
Write program as shown in the figure below,
1-when you press on the command wit name “add name”, the program will allow to you to
enter 30 names to the list box and if you want to exit from entering names enter special
character “1212”
2- when you press on the command with name “remove”, the program will enable you from
removing the name that you want to remove
Code
Running stage

You might also like