Title: Data Types
1Lecture 2
- Data Types
- Assignment Statements
- Control Structures
2Visual Basic Data Types
3Numeric Datatypes
- ,-,/,,
- double (dbl)
- integer (int)
4Strings
- String (str)
- Dim interestRate As Double
- Dim phrase As String
- Dim variable name As variable type
- Assignment Statements c a b
5What are control structures
- Control structures control the flow of execution
in a program or function. - Visual Basic control structures allow you to
combine individual instructions into a single
logical unit with one entry point and one exit
point.
6Types of control structures
- Instructions are organised into three kinds of
control structures to control execution flow
sequence, selection and repetition.
7Types of control structures (1) Sequence
- Until now we have only been using sequential flow
8Types of control structures (2) Selection
- A selection control structure chooses which
alternative to execute. - A program chooses between alternative suggestions
by testing the value of key variables. - A condition is an expression that is either false
or true. It establishes a criterion for either
executing or skipping a group of statements.
9Relational Operators
- Satisfies a condition
- equal to identical to
- ltgt unequal to different from
- lt less than precedes alphabetically
- gt greater than follows alphabetically
- lt less than or equal to precedes alphabetically
or is equal to - gt greater than or equal to precedes
alphabetically or is equal to
10Logical Operators
- cond1 And cond2
- cond1 Or cond2
- Not cond1
11The if statement
The if statement is the primary selection control
structure.
F
T
num1 gt num2
Display The larger number is num1
Display The larger number is num2
12Syntax
If condition Then action 1 Else action
2 End If
If (num1 gt num2) Then picOutput.Print the
larger number is num1 Else picOutput.Print
the larger number is num2 End If
13Class Assignment
Write a program to allow the user to input two
numbers and to write the larger number.
14Syntax
If condition1 Then action 1 Elseif
condition2 Then action 2 Elseif
condition3 Then action 3 Else action
4 End If
15Syntax
if (num1 gt num2) Then picOutput.Print the
larger number is num1 Elseif (num2 gt
num1) Then picOutput.Print the larger number
is num2 Else picOutput.Print the numbers
are equal End If
16Class Assignment
Write the interface and code to allow the user to
input a Shape ID Write a selection structure to
check what ID was entered and write the following
output into a picture box. If POINT entered
write This shape has one vertex If LINE
entered write This shape has two vertices If
POLYGON entered write This shape has more than
three vertices
17Syntax
If condition1 And condition 2
Then action End If
If condition1 Or condition 2
Then action End If
18Syntax
If (num1 0) Or (num2 0) Then
picOutput.Print "Incorrect input" ElseIf
num1 gt num2 Then picOutput.Print
"The larger number is " txtFirst.Text
ElseIf num1 gt num2 Then
picOutput.Print "The larger number is "
txtSecond.Text Else
picOutput.Print "The numbers are equal" End If
19Class Assignment
Modify the code written for the final assignment
to check for illogical input by the user.
20Class Assignment
Using a combo box, Rewrite the code in the
previous class assignment to output the following
into a picture box. If the shape ID is either a
LINE or POLYGON write This shape has more than
one vertex Else write This shape has one
vertex
21Types of control structures (3) Repetition
A repetition structure allows the programmer to
specify that an action is to be repeated until
some terminating condition occurs.
The repetition of steps in a program is called a
loop.
22No
Any steps repeated
No loop required
Yes
Know in advance how many times to repeat
No
Do loops
Yes
Use a for loop
23Do Loops
Design
Do while condition statements Loop
Do statements Loop Until Condition
Write a program to print out the numbers between
1 and 10
24Nested Do Loops
Do While (no_iterations lt 5) Do While
(num lt 10) picNumbers.Print num
num num 1 Loop num
1 no_iterations no_iterations 1
picNumbers.Print "Number of iterations "
no_iterations Loop
25Do Loops and Logical Operators
Do While (num lt 10) And (num ltgt 0)
picNumbers.Print num num num 1
Loop picNumbers.Print "Number entered is
zero"
26For.next loops
- When we know exactly how many times a loop should
be executed as opposed to until a condition is
met as in the do loop
27Syntax
For i m to n Step s statements Next m
Example Write a program that requests a number
n, then calculate the sum of the numbers from 1
to n. Repeat the above example with a step size
of 3.
28Class Assignment
Example Write a program that requests the user
to enter a number n, from 1 to 30 and one of the
letters S or P. Then calculate the sum of
product of the numbers from 1 to n depending upon
whether S or P was selected.