Basic Loop Structure : Code (Program)
Basic Loop Structure : Code (Program)
MODULE 3
BASIC LOOP STRUCTURE ► used to execute statements until a certain condition is met. A variable
number is initialized to 1 and then the Loop starts. First, the condition is tested; if condition is
True, then the statements are executed. When it gets to the Loop it goes back to the Do and
tests condition again.
EX: Develop an application that will display all numbers from 1 to the number read from the
user.
INPUT
OUTPUT
(Display)
CODE (PROGRAM):
Private Sub Text1_KeyPress(KeyAscii As Integer)
Dim x As Integer
Dim y As Integer
If KeyAscii = 13 Then KeyAscii = 13 is equivalent to the enter key.
List1.Clear
y = Val (Text1.Text) End count designated by the input number in textbox.
For x = 1 To y
List1.AddItem (x) Range
Next x
End If
End Sub
ARRAYS ► Local arrays are declared in a procedure using Dim or Static. Array must be
declared explicitly with keyword "As". There are two types of arrays in Visual Basic namely:
Fixed-size array. The size of array always remains the same-size doesn't change during the
program execution.
EX: Make an application that is capable of getting 10 numbers. The application must be able to
display these numbers in a listbox in reverse order on how they are read.
TRIGGER
5
4
3 OUTPUT
2 (Display)
1
CODE (PROGRAM): Starting Count
Option Base 1
Dim no (5) As Integer End Count
Private Sub Command1_Click()
For x = 1 to 5 Number of Inputs
Getting the input
no (x) = InputBox(“Enter Number” & x, “Read Number”,0) numbers (Any number
Next x
will do)
List1.clear
For x = 5 to 1 Step -1 Numbers are displayed from
List1.AddItem ( no (x) )
last input to first input since the
Next x
End Sub negative (-) sign is used
EX: Design an application that will get a number of values from the user. The number of values
to be read depends on the array size read from the user.
Input
Display
Output