Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Computing Dr. Nadeem A Khan. Lecture 7.

Similar presentations


Presentation on theme: "Introduction to Computing Dr. Nadeem A Khan. Lecture 7."— Presentation transcript:

1 Introduction to Computing Dr. Nadeem A Khan

2 Lecture 7

3 Using commas: Example Using commas: Example ► Picture1.Print “North”, “to”, “the”, “future” ► Picture1.Print “No:”, 6, More on Print method

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

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

6 Using the Tab function: Example Using the Tab function: Example ► Picture1.Print “Hello”;Tab(10); “World”; Tab(20);”!” ► Picture1.Print “No:”;Tab(5);5 More on Print method

7 Using the Tab function: Result Using the Tab function: Result ► HelloWorld!(‘World’ at 10th position) (‘!’ at 20th position) (‘!’ at 20th position) ► No: 5 (‘5’ at 6th position) More on Print method (Contd.)

8 Using the Tab functions: Rules Using the Tab functions: Rules ► Tab(n); (where n is a positive integer) =>Following Item (if possible) displayed beginning at the nth position of the line =>Following Item (if possible) displayed beginning at the nth position of the line More on Print method

9 Using the Spc function: Example Using the Spc function: Example ► Picture1.Print “Hello”;Spc(3); “World” ► Picture1.Print “No:”;Spc(3);5 More on Print method

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

11 Using the Spc functions: Rules Using the Spc functions: Rules ► Spc(n); (where n is a positive integer) =>Following Item printed after n spaces =>Following Item printed after n spaces More on Print method

12 Output to the Printer: Output to the Printer: ► Printer.Print expr =>sends expression expr to the printer =>works the same way as Picture1.Print More on Print method

13 Output to the Printer: Output to the Printer: Let Printer.FontName = “Script” Let Printer.FontBold = True Let Printer.FontSize = 12 => set properties More on Print method (Contd.)

14 Decisions

15 Decision Structure: Flowchart Process Step (s) 2 Is Condition True Process Step (s) 1

16 IF BLOCKS IF condition Then action1Else action 2 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 a=4 Let b=5... Picture1. Print “Its value is:”;largerVal End Sub

18 IF BLOCK (Contd.) Sub Command1_Click Dim a As Single, b As Single, largerVal As Single Let a=4 Let b=5 If a>b Then Picture1.Print “a has the larger value” largerVal=aElse Picture1.Print “b has the larger value” largerVal=b End If Picture1. Print “Its value is:”;largerVal 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 a=4 Let b=4 If (a>b) Or (a=b) Then Picture1.Print “a has the larger value” largerVal=aElse Picture1.Print “b has the larger value” largerVal=b End If Picture1. Print “Its value is:”;largerVal 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 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 a=4 Let b=5 If (a>b) Then Picture1.Print “a has the larger value” ElseIf (a<b) 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 ► ExpressionTrue/False 2 < 5? -5 > -2.5? 1 < 1? 1 = 1? 3.5 <= 3.5? -9 >= -35? -2 <> -3?

29 Conditions: Examples (Contd.) ► ExpressionTrue/False 2 < 5True -5 > -2.5False 1 < 1False 1 = 1True 3.5 <= 3.5True -9 >= -35True -2 <> -3True

30 Conditions: Examples (Contd.) ► ExpressionTrue/False “cat” < “dog”? “cart” < “cat”? “cat” < “catalog”? “9W” < “bat”? “Dog” < “cat” ? “Sales-99” <= “Sales-retail”?

31 Conditions: Examples (Contd.) ► For strings use the ANSI (or ASCI) table CharactersANSI(ASCI) Value CharactersANSI(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.) ► ExpressionTrue/False “cat” < “dog”True “cart” < “cat”True “cat” < “catalog”True “9W” < “bat”True “Dog” < “cat” True “Sales-99 <= “Sales-retail”True

33 Conditions: Examples (Contd.) ► Assume a= 4; b= 3; c=“hello”; d=“bye” ► ExpressionTrue/False (a + b) < 2*a? (Len(c) - b) = (a/2)? c < “good” & d?

34 Conditions: Examples (Contd.) ► Assume a= 4; b= 3; c=“hello”; d=“bye” ► ExpressionTrue/False (a + b) < 2*aTrue (Len(c) - b) = (a/2)True c < “good” & dFalse

35 Conditions (Contd.) ► Condition is an expression involving relational operators; it is either True or False ► Relational operators: =; <>; ; =

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; answ=“Y” ► ExpressionTrue/False (2<n)And(n<6) ? Not(n<6)? (answ=“Y”) Or (answ=“y”)?

38 Conditions: Examples (Contd.) ► Assume n= 4; answ=“Y” ► ExpressionTrue/False (2<n)And(n<6) True Not(n<6)False (answ=“Y”) Or (answ=“y”)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.) => - Tie: Left most operator is first - Use parantheses

41 ► 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 Reading Data from File

42 ► DATA.TXT file has two lines: “September”26 => items separated by commas or line-breaks; data item of same type as the data to be data item of same type as the data to be assigned to the corresponding variable assigned to the corresponding variable Reading Data from File (Contd.)

43 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 Reading Data from File (Contd.)

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

45 ► 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 Writing Data from File

46 Example: Sub Command1_Click() Dim name1 As String, num As Single name1=“Ahmad”num=123.4 Open “DATA.TXT” For Output As #1 Write #1, “Hello” Write #1, name1, num Write #1, 14*139, “Hassan” & name1 Close #1 End Sub Writing Data from File (Contd.)

47 The result: “Hello”“Ahmad”,123.41946,“HassanAhmad” Writing Data to File (Contd.)

48 ► MsgBox message,, “Caption” ► A Message Box will appear  displaying the string message  ‘Caption’ will appear in the title bar The Message Box for Output

49 ► Let stringVar = InputBox$(message) ► =>An InputBox will appear displaying the string message; Assigns the typed user response to stringVar after Enter is pressed or OK is clicked The Input Box for Input

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

51 ► Rem text ► ……………….. ‘text e.g: Rem Compute weekly pay Dim hrs as Single ‘hours worked Inclusion of Comments

52 Loop structure: General Format ► Do While condition statementsLoop

53 Flow chart: loop structure No Process Step(s) Is condition true ? Yes

54 ► Example 1: Sub Command1_Click ( ) Dim num As Integer Let num=1 Do While num<=10 Picture1.Print num; Let num=num+1 Loop End Sub Do Loops

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

56 ► Example 2: Sub Command1_Click ( ) Dim passWord as String, info As String If Ucase(Text1.Text) = “SECRET.TXT” Then Let passWord=“” Do While passWord <> “SHAHID” Let passWord = InputBox$(“Enter passWord”) Let passWord = Ucase(passWord) Loop End If ‘ Remaining statements on next slide Do Loops (Contd.)

57 ► 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 Do Loops (Contd.)

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 ► 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 Do-Loop-Until (Contd.)

61 ► 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 Do-Loop-Until (Contd.)

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 <> 0 num=num-2Loop End Sub


Download ppt "Introduction to Computing Dr. Nadeem A Khan. Lecture 7."

Similar presentations


Ads by Google