Title: Introduction to Computing
1Introduction to Computing
2Lecture 7
3More on Print method
- Using commas Example
- Picture1.Print North, to, the, future
- Picture1.Print No, 6,
4More on Print method (Contd.)
- Using commas Result
- North to the future
- No 6 (6 displayed 1 pos. further in
zone)
5More on Print method
- Using commas Rules
- Line of a Picture Box divided in zones
- A Zone 14 positions
- Print Items displayed in consecutive zones
6More on Print method
- Using the Tab function Example
- Picture1.Print HelloTab(10) World
Tab(20)! - Picture1.Print NoTab(5)5
7More on Print method (Contd.)
- Using the Tab function Result
- Hello World ! (World at 10th position)
- (! at
20th position) - No 5 (5 at 6th position)
8More on Print method
- Using the Tab functions Rules
- Tab(n) (where n is a positive integer)
- gtFollowing Item (if possible) displayed
beginning at the nth position of the line
9More on Print method
- Using the Spc function Example
- Picture1.Print HelloSpc(3) World
- Picture1.Print NoSpc(3)5
10More on Print method (Contd.)
- Using the Spc function Result
- Hello World (World after 3 spaces)
- No 5 (5 after 4 spaces)
11More on Print method
- Using the Spc functions Rules
- Spc(n) (where n is a positive integer)
- gtFollowing Item printed after n spaces
12More on Print method
- Output to the Printer
- Printer.Print expr
-
- gtsends expression expr to the printer
- gtworks the same way as Picture1.Print
13More on Print method (Contd.)
- Output to the Printer
- Let Printer.FontName Script
- Let Printer.FontBold True
- Let Printer.FontSize 12
-
- gt set properties
14Decisions
15Decision StructureFlowchart
Is Condition True
Process Step (s) 2
Process Step (s) 1
16IF BLOCKS
- IF condition Then
- action1
- Else
- action 2
- End If
17IF BLOCK (Contd.)
- Complete the program Identifies and displays the
larger value and its variable (assume unequal
values) - Sub Command1_Click
- Dim a As Single, b As Single, largerVal As
Single - Let a4
- Let b5
- .
- .
- .
- Picture1. Print Its value islargerVal
- End Sub
18IF BLOCK (Contd.)
Sub Command1_Click Dim a As Single, b As Single,
largerVal As Single Let a4 Let b5 If agtb
Then Picture1.Print a has the larger
value largerVala Else Picture1.Print b
has the larger value largerValb End
If Picture1. Print Its value islargerVal
End Sub
19IF BLOCK (Contd.)
Result b has the larger value Its value is
5
20IF BLOCK (Contd.)
What the following program will do?
21IF BLOCK (Contd.)
Sub Command1_Click Dim a As Single, b As Single,
largerVal As Single Let a4 Let b4 If (agtb)
Or (ab) Then Picture1.Print a has the larger
value largerVala Else Picture1.Print b
has the larger value largerValb End
If Picture1. Print Its value islargerVal
End Sub
22IF BLOCK (Contd.)
Result a has the larger value Its value is
4
23IF BLOCK EXTENDED
- IF condition 1 Then
- action1
- ElseIf condition 2 Then
- action 2
- ElseIf condition 3 Then
- action 3
- End If
24IF BLOCK EXTENDED(Contd.)
What the following program will do?
25IF BLOCK EXTENDED(Contd.)
Sub Command1_Click Dim a As Single, b As
Single Let a4 Let b5 If (agtb)
Then Picture1.Print a has the larger
value ElseIf (altb) Then Picture1.Print b has
the larger value Else Picture1.Print a and
b have same value End If End Sub
26IF BLOCK EXTENDED (Contd.)
Result b has the larger value
27Conditions
28Conditions Examples
- Expression True/False
- 2 lt 5 ?
- -5 gt -2.5 ?
- 1 lt 1 ?
- 1 1 ?
- 3.5 lt 3.5 ?
- -9 gt -35 ?
- -2 ltgt -3 ?
29Conditions Examples (Contd.)
- Expression True/False
- 2 lt 5 True
- -5 gt -2.5 False
- 1 lt 1 False
- 1 1 True
- 3.5 lt 3.5 True
- -9 gt -35 True
- -2 ltgt -3 True
30Conditions Examples (Contd.)
- Expression True/False
- cat lt dog ?
- cart lt cat ?
- cat lt catalog ?
- 9W lt bat ?
- Dog lt cat ?
- Sales-99 lt Sales-retail ?
-
31Conditions Examples (Contd.)
- For strings use the ANSI (or ASCI) table
- Characters ANSI(ASCI) Value
- Digits (0-9) 48-57
- Upper Case (A-Z) 65-90
- Lower Case (a-z) 97-122
- Compare two strings character by character
-
32Conditions Examples (Contd.)
- Expression True/False
- cat lt dog True
- cart lt cat True
- cat lt catalog True
- 9W lt bat True
- Dog lt cat True
- Sales-99 lt Sales-retail True
-
33Conditions Examples (Contd.)
- Assume a 4 b 3 chello dbye
- Expression True/False
- (a b) lt 2a ?
- (Len(c) - b) (a/2) ?
- c lt good d ?
-
34Conditions Examples (Contd.)
- Assume a 4 b 3 chello dbye
- Expression True/False
- (a b) lt 2a True
- (Len(c) - b) (a/2) True
- c lt good d False
-
35Conditions (Contd.)
- Condition is an expression involving relational
operators it is either True or False - Relational operators
- ltgtltgtltgt
-
-
36Conditions (Contd.)
- Complex Conditions Conditions joined by Logical
Operators - cond1 And cond2
- cond1 Or cond2
- Not cond1
-
37Conditions Examples (Contd.)
- Assume n 4 answY
- Expression True/False
- (2ltn)And(nlt6) ?
- Not(nlt6) ?
- (answY) Or (answy) ?
-
38Conditions Examples (Contd.)
- Assume n 4 answY
- Expression True/False
- (2ltn)And(nlt6) True
- Not(nlt6) False
- (answY) Or (answy) True
-
39Conditions (Contd.)
- Operator hierarchy
- In decreasing order of priority
- Arithemetic
- Relational
- Logical
- Logical operator hierarchy
- In decreasing order of priority
- Not
- And
- Or
-
40Conditions (Contd.)
gt - Tie Left most operator is
first - Use parantheses
41Reading Data from File
-
- Open filespec For Input As n
- Opens a file for reading and assigns the
reference number n - Input n, var
- Next available item is read from the file and
assigned to variable var - Close n
- Closes the file
42Reading Data from File (Contd.)
- DATA.TXT file has two lines
- September
- 26
- gt items separated by commas or line-breaks
- data item of same type as the data to be
- assigned to the corresponding variable
43Reading Data from File (Contd.)
- Example
- Sub Command1_Click()
- Dim month As String, date As Single
- Picture1.Cls
- Open DATA.TXT For Input As 1
- Input 1, month
- Input 1, date
- Picture1.Print Today is month date
- Close 1
- End Sub
-
44Reading Data from File (Contd.)
- The result
- Today is September 26
45Writing Data from File
-
- Open filespec For Output As n
- Opens a file for writing and assigns the
reference number n - Write n, expression list separated by commas
- Expression list with each expression separated by
comma is written on the next line in the file - Close n
- Closes the file
46Writing Data from File (Contd.)
- Example
- Sub Command1_Click()
- Dim name1 As String, num As Single
- name1Ahmad
- num123.4
- Open DATA.TXT For Output As 1
- Write 1, Hello
- Write 1, name1, num
- Write 1, 14139, Hassan name1
- Close 1
- End Sub
-
47Writing Data to File (Contd.)
- The result
- Hello
- Ahmad,123.4
- 1946,HassanAhmad
48The Message Box for Output
-
- MsgBox message, , Caption
- A Message Box will appear
- displaying the string message
- Caption will appear in the title bar
49The Input Box for Input
-
- Let stringVar InputBox(message)
- gtAn InputBox will appear displaying the string
message Assigns the typed user response to
stringVar after Enter is pressed or OK is
clicked
50Input/Output Example
- Example
- Sub Command1_Click()
- Dim first As String, middle As String, last As
String - Open D\Names.txt For Input As 1
- Input 1, first, last
- middle Inputbox(enter your middle name)
- MsgBox first middle last, ,
The full name - Close 1
- End Sub
-
51Inclusion of Comments
- Rem text
- .. text
- e.g
- Rem Compute weekly pay
- Dim hrs as Single hours worked
52Loop structure General Format
- Do While condition
- statements
- Loop
53Flow chart loop structure
No
Is condition true ?
Yes
Process Step(s)
54Do Loops
- Example 1
- Sub Command1_Click ( )
- Dim num As Integer
- Let num1
- Do While numlt10
- Picture1.Print num
- Let numnum1
- Loop
- End Sub
55Do Loops
- The result
- 1 2 3 4 5 6 7 8 9 10
56Do Loops (Contd.)
- Example 2
- Sub Command1_Click ( )
- Dim passWord as String, info As String
- If Ucase(Text1.Text) SECRET.TXT Then
- Let passWord
- Do While passWord ltgt SHAHID
- Let passWord InputBox(Enter passWord)
- Let passWord Ucase(passWord)
- Loop
- End If
- Remaining statements on next slide
57Do Loops (Contd.)
- Example 2 (Contd.)
- Continues from the previous slide
- Open Text1.Text For Input As 1
- Input 1, info
- Picture1.Print info
- Close 1
- End Sub
58Do-Loop-Until General Format
- Do
- statements
- Loop Until condition
- Flow chart?
59Do-Loop-Until General Format
- Modify example 2 to Do-loop-Until Format
-
60Do-Loop-Until (Contd.)
- Example 1
- Sub Command1_Click ( )
- Dim passWord as String, info As String
- If Ucase(Text1.Text) SECRET.TXT Then
-
- Do
- Let passWord InputBox(Enter passWord)
- Let passWord Ucase(passWord)
- Loop Until passWord SHAHID
- End If
- Remaining statements on next slide
61Do-Loop-Until (Contd.)
- Example 1 (Contd.)
- Continues from the previous slide
- Open Text1.Text For Input As 1
- Input 1, info
- Picture1.Print info
- Close 1
- End Sub
62Do-Loop Another Example
- When will the following program come to its end?
-
63Do-Loop Another Example
- Sub Command1_Click ( )
- Dim num As Single
- num 7
- Do While num ltgt 0
- numnum-2
- Loop
- End Sub
-