Introduction to Computing - PowerPoint PPT Presentation

1 / 63
About This Presentation
Title:

Introduction to Computing

Description:

works the same way as Picture1.Print. More on Print method. Output to the Printer: ... Picture1.Print 'a and b have same value' End If. End Sub. IF BLOCK ... – PowerPoint PPT presentation

Number of Views:56
Avg rating:3.0/5.0
Slides: 64
Provided by: surajL
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Computing


1
Introduction to Computing
  • Dr. Nadeem A Khan

2
Lecture 7
3
More on Print method
  • Using commas Example
  • Picture1.Print North, to, the, future
  • Picture1.Print No, 6,

4
More on Print method (Contd.)
  • Using commas Result
  • North to the future
  • No 6 (6 displayed 1 pos. further in
    zone)

5
More on Print method
  • Using commas Rules
  • Line of a Picture Box divided in zones
  • A Zone 14 positions
  • Print Items displayed in consecutive zones

6
More on Print method
  • Using the Tab function Example
  • Picture1.Print HelloTab(10) World
    Tab(20)!
  • Picture1.Print NoTab(5)5

7
More on Print method (Contd.)
  • Using the Tab function Result
  • Hello World ! (World at 10th position)
  • (! at
    20th position)
  • No 5 (5 at 6th position)

8
More 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

9
More on Print method
  • Using the Spc function Example
  • Picture1.Print HelloSpc(3) World
  • Picture1.Print NoSpc(3)5

10
More on Print method (Contd.)
  • Using the Spc function Result
  • Hello World (World after 3 spaces)
  • No 5 (5 after 4 spaces)

11
More on Print method
  • Using the Spc functions Rules
  • Spc(n) (where n is a positive integer)
  • gtFollowing Item printed after n spaces

12
More on Print method
  • Output to the Printer
  • Printer.Print expr
  • gtsends expression expr to the printer
  • gtworks the same way as Picture1.Print

13
More on Print method (Contd.)
  • Output to the Printer
  • Let Printer.FontName Script
  • Let Printer.FontBold True
  • Let Printer.FontSize 12
  • gt set properties

14
Decisions
15
Decision StructureFlowchart
Is Condition True
Process Step (s) 2
Process Step (s) 1
16
IF BLOCKS
  • IF condition Then
  • action1
  • Else
  • action 2
  • End If

17
IF 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

18
IF 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
19
IF BLOCK (Contd.)
Result b has the larger value Its value is
5
20
IF BLOCK (Contd.)
What the following program will do?
21
IF 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
22
IF BLOCK (Contd.)
Result a has the larger value Its value is
4
23
IF BLOCK EXTENDED
  • IF condition 1 Then
  • action1
  • ElseIf condition 2 Then
  • action 2
  • ElseIf condition 3 Then
  • action 3
  • End If

24
IF BLOCK EXTENDED(Contd.)
What the following program will do?
25
IF 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
26
IF BLOCK EXTENDED (Contd.)
Result b has the larger value
27
Conditions
28
Conditions 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 ?

29
Conditions 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

30
Conditions 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 ?

31
Conditions 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

32
Conditions 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

33
Conditions 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 ?

34
Conditions 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

35
Conditions (Contd.)
  • Condition is an expression involving relational
    operators it is either True or False
  • Relational operators
  • ltgtltgtltgt

36
Conditions (Contd.)
  • Complex Conditions Conditions joined by Logical
    Operators
  • cond1 And cond2
  • cond1 Or cond2
  • Not cond1

37
Conditions Examples (Contd.)
  • Assume n 4 answY
  • Expression True/False
  • (2ltn)And(nlt6) ?
  • Not(nlt6) ?
  • (answY) Or (answy) ?

38
Conditions Examples (Contd.)
  • Assume n 4 answY
  • Expression True/False
  • (2ltn)And(nlt6) True
  • Not(nlt6) False
  • (answY) Or (answy) True

39
Conditions (Contd.)
  • Operator hierarchy
  • In decreasing order of priority
  • Arithemetic
  • Relational
  • Logical
  • Logical operator hierarchy
  • In decreasing order of priority
  • Not
  • And
  • Or

40
Conditions (Contd.)
gt - Tie Left most operator is
first - Use parantheses
41
Reading 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

42
Reading 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

43
Reading 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

44
Reading Data from File (Contd.)
  • The result
  • Today is September 26

45
Writing 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

46
Writing 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

47
Writing Data to File (Contd.)
  • The result
  • Hello
  • Ahmad,123.4
  • 1946,HassanAhmad

48
The Message Box for Output
  • MsgBox message, , Caption
  • A Message Box will appear
  • displaying the string message
  • Caption will appear in the title bar

49
The 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

50
Input/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

51
Inclusion of Comments
  • Rem text
  • .. text
  • e.g
  • Rem Compute weekly pay
  • Dim hrs as Single hours worked

52
Loop structure General Format
  • Do While condition
  • statements
  • Loop

53
Flow chart loop structure
No
Is condition true ?
Yes
Process Step(s)
54
Do Loops
  • Example 1
  • Sub Command1_Click ( )
  • Dim num As Integer
  • Let num1
  • Do While numlt10
  • Picture1.Print num
  • Let numnum1
  • Loop
  • End Sub

55
Do Loops
  • The result
  • 1 2 3 4 5 6 7 8 9 10

56
Do 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

57
Do 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

58
Do-Loop-Until General Format
  • Do
  • statements
  • Loop Until condition
  • Flow chart?

59
Do-Loop-Until General Format
  • Modify example 2 to Do-loop-Until Format

60
Do-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

61
Do-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

62
Do-Loop Another Example
  • When will the following program come to its end?

63
Do-Loop Another Example
  • Sub Command1_Click ( )
  • Dim num As Single
  • num 7
  • Do While num ltgt 0
  • numnum-2
  • Loop
  • End Sub
Write a Comment
User Comments (0)
About PowerShow.com