Lecture 1...
Visual Basic Ali Jassim Mohammed Ali
Programming concepts
A computer system does nothing until directed to do so by human.
A program, which consists of instruction to the computer, is how we tell
a computer to perform certain operations.
Programmers use a variety of programming language, such as
COBOL and VISUAL BASIC, to communicate instructions to the computer.
Writing programs
The following steps are followed for each programming project:
Step 1: Describe the problem
Step 2: Analyze the problem
Step 3: Design the general logic of the program
Step 4: Design the detailed logic of the program
Step 5: Code the program
Step 6: Test and debug the program
Step 7: Document the program
1
Lecture 1... Visual Basic Ali Jassim Mohammed Ali
Program design techniques
A number of techniques are available to help programmers analyze
a problem and design the program. The most popular techniques is flow
charting and pseudocode.
1. Flow charting
Flow charts are used to illustrate data information and work flow
by the interconnection of specialized symbols with flow lines.
Computer process
Input / output
Decision
Begin and end
Connector
Flow lines
Loop (for)
Loop (next)
2
Lecture 1... Visual Basic Ali Jassim Mohammed Ali
2. Pseudocode
Another design technique used almost exclusively for program
design is called pseudocode. It represents the logic in program like
statements written in plain English.
Because pseudocode does not have any syntax guidelines (rules for
formulating instructing), you can concentrate solely on developing the
logic of your program.
Generations of programming languages
We talk to computers within the framework of a particular
programming languages. There are many different programming
languages, most of which have highly structured sets of rules.
The selection of a programming language depends on who is
involved and the nature of conversation.
The programming language have evolved in generations:
1. Low-level language
a. Machine language
Machine language is the first generation, and the instructions are coded
as a series of 1 and 0.
3
Lecture 1... Visual Basic Ali Jassim Mohammed Ali
b. Assembler language
The big difference between the two is the way the instructions are
represented by the programmer. Rather than a cumbersome series of 1
and 0, assembler language use easily recognized symbols, called
(mnemonics) for example: MUL to represent a (multiply).
2. High level language
a. Third generation
- Business (COBOL, RPG)
- Multipurpose (BASIC, PASCAL)
-Scientific (FORTRAN)
b. Fourth generation
- Prolog
- Modula-2
4
Lecture 2... Visual Basic Ali Jassim Mohammed Ali
The Project in the visual basic is made up of:
1. Forms - Windows that you create for user interface.
2. Controls - Graphical features drawn on forms to allow user interaction (text
boxes, labels, scroll bars, command buttons, etc.)
3. Properties - Every characteristic of a form or control is specified by a property.
Example properties include names, captions, size, color, position, and contents.
4. Methods - Built-in procedure that can be invoked to impart some action to a
particular object.
5. Event Procedures - Code related to some object. This is the code that is
executed when a certain event occurs.
6. General Procedures - Code not related to objects. This code must be invoked
by the application.
7. Modules - Collection of general procedures, variable declarations, and
constant definitions used by application.
Steps in Developing Application
1. Draw the user interface
2. Assign properties to controls
3. Attach code to controls
Drawing the User Interface and Setting Properties
• Visual Basic operates in three modes.
1. Design mode-used to build application
2. Run mode-used to run the application
3. Break mode-application halted and debugger is available
1
Lecture 2... Visual Basic Ali Jassim Mohammed Ali
Visual basic windows
• Seven windows appear when you start Visual Basic.
1. The Main Window consists of the title bar, menu bar, and toolbar. The title
bar indicates the project name, the current Visual Basic operating mode, and the
current form. The main window also shows the location of the current form
relative to the upper left corner of the screen (measured in twips) and the width
and length of the current form.
2. The Form Window is central to developing Visual Basic applications. It is
where you draw your application.
2
Lecture 2... Visual Basic Ali Jassim Mohammed Ali
3. The Tool box is the selection menu for controls used in your application.
4. The Properties Window is used to establish initial property values for objects.
Two views are available: Alphabetic and Categorized.
3
Lecture 2... Visual Basic Ali Jassim Mohammed Ali
5. The Form Layout Window shows where your form will be displayed relative
to your monitor’s screen:
6. The Project Window displays a list of all forms and modules making up your
application.
7. Code windows you can also obtain a view of the Form or Code windows
(window containing the actual basic coding) from the Project window.
4
Lecture 7 ... Visual Basic 6.0
Loops in Visual Basic
Loops are one of the most important topics in programming. This lecture will
introduce you to the implementation of loops in Visual Basic. Loop structure is
employed whenever the programmer tries to repeat a series of instructions and
statements over and over. There are several kinds of loops in Visual Basic. We have
three different kinds of loops, they are:
For Next loop
Do loop
Do While Loop
For … Next Loop:
The For … Next loop has the following general structure:
For counter = start To end [Step step]
[statement 1]
[statement 2]
[statement 3]
[statement N]
Next [counter]
Where
"counter" is an integer that keeps track of the loop operation,
the number for "start" should be smaller than the number for "end" in the case
of positive steps, and vice versa in the case of negative steps.
[Step step] is an optional argument, if missing the step is considered to be +1,
this argument becomes important when we want to increment using different
steps.
In executing the For loop, visual basic:
1. Sets counter equal to start.
2. Tests to see if counter is greater than end. If so, visual basic exits the loop (if
increment is negative, visual basic tests to see if counter is less than end).
3. Executes the statements.
4. Increments counter by 1 or by increment step, if it’s specified.
5. Repeats steps 2 through 4.
1
Lecture 7 ... Visual Basic 6.0
Visual Basic For Loop Flowchart Diagram
Example output
Private Sub Command1_Click() 1
Dim i As Integer 2
For i = 1 To 6 3
Print i 4
Next i 5
End Sub 6
1
Private Sub Command1_Click() 11
Dim i As Integer 21
For i = 1 To 70 Step 10 31
Print i 41
Next 51
End Sub 61
25
Private Sub Command1_Click() 20
Dim j As Integer 15
For j = 25 To 0 Step -5 10
Print j 5
Next j 0
End Sub
2
Lecture 7 ... Visual Basic 6.0
Private Sub Command1_Click() X=30
Dim i, x As Integer
x=0
For i = 10 To 2 Step -2
x=x+i
Next i
Print "X=" ; x
End Sub
Example: Design a form and write a program to find the summation of numbers
(from 0 to 100).
Solution:
Private Sub form_load()
Form1.show
Dim I As Integer, Total As Integer
Total =0
For I = 0 To 100
Total= Total + I
Next I
Print "Total=";Total
End Sub
Example: Design a form and write code to find the summation of even numbers
(from 0 to 100).
Solution:
Private Sub form_load ()
Form1.show
Dim I As Integer, Total As Integer
Total =0
For I = 0 To 100 step 2
Total= Total + I
Next I
Print "Total of EVEN numbers =";Total
End Sub
Example: Design a form and write code to find the summation of odd numbers
(from 0 to 100).
Solution A:
Private Sub form_load ()
Form1.show
Dim J As Integer, S As Integer
S=0
For J = 1 To 100 step 2
3
Lecture 7 ... Visual Basic 6.0
S= S+ J
Next J
Print "Total of ODD numbers="; S
End Sub
Solution B:
Private Sub form_load ()
Form1.show
Dim j As Integer, S As Integer
S =0
For j = 0 To 100
If j mod 2 =1 then S= S + j
Next j
Print "Total of ODD numbers="; S
End Sub
Example: Design a form and write code to find the summation of odd numbers
(from 0 to 100) and of even numbers (from 0 to 100) Separated.
Solution:
Private Sub form1_load ()
Form1.show
Dim i As Integer, SO As Integer , SE As Integer
SO =0
SE =0
For i = 0 To 100
If i mod 2 =1 then
SO= SO + i
Else
SE= SE + i
EndIf
Next i
Print "Total of EVEN numbers ="; SE
Print "Total of ODD numbers="; SO
End Sub
Example: Create a Visual Basic Project to find the value of the following series.
Solution:
Private Sub form_load ()
Form1.show
4
Lecture 7 ... Visual Basic 6.0
Dim i As Integer, y As Integer
y =0
For i = 1 To 10
y= y + i
Next i
Print "Summation ="; y
End Sub
Example: Create a Visual Basic Project to find the value of the following series.
Write the code program so that the value of constants (x) are entered into text
box. and calculate the value of series. Display the value of series (y) in a separate
text box.
Solution:
Private Sub Command1_Click()
Dim i As Integer, x as Integer, y As Integer
y =0
x = Val (Text1.text)
For i = 1 To 10
y= y + ( i * x )
Next i
Text2.text = y
End Sub
Example: Create a Visual Basic Project to find the value of the following series.
Write the code program so that the value of constants (n) is entered into text box,
and calculate the value of series. Display the value of series (y) in a separate text
box.
Solution:
Private Sub Command1_Click()
Dim i As Integer, n as Integer, y As double
y =0
n = Val (Text1.text)
For i = 1 To n
y= y + ( 1 / i )
Next i
Text2.text = y
End Sub
5
Lecture 7 ... Visual Basic 6.0
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 is :
For counter1 = StartNumber To EndNumber [Step step]
[One or more VB statements ]
Next [counter1]
[One or more VB statements ]
For counter2 = StartNumber To EndNumber [Step step]
[One or more VB statements ]
Next [counter2]
For counter1 = StartNumber To EndNumber [Step step]
For counter2 = StartNumber To EndNumber [Step step]
[One or more VB statements ]
Next [counter2]
Next [counter1]
Error For loop:
For counter1 = StartNumber To EndNumber [Step step]
For counter2 = StartNumber To EndNumber [Step step]
[One or more VB statements ]
Next [counter1]
Next [counter2]
6
Lecture 7 ... Visual Basic 6.0
Example:
Private Sub Form_load ( )
Dim i As Integer, j As Integer
For i = 1 to 5
Print "Hello"
For j =1 to 4
Print "Welcome to the VB tutorial"
Next j
Next i
Print "Thank you"
End Sub