Download presentation
Presentation is loading. Please wait.
1
Introduction to Computing Dr. Nadeem A Khan
2
Lecture 6
3
Local vs Form Variables
4
Not only +,-,*,^ But also: But also: \ opeartor e.g: 5.1\2.04= 2 \ opeartor e.g: 5.1\2.04= 2 MOD operator e.g: 15.2 MOD 6=3 MOD operator e.g: 15.2 MOD 6=3 => First round to closest integers before operation More on Operators
5
More on Operators (Contd.) ► Operator Precedence 1. ^ 2.- operator (indicating a negative value) 3. * and / operator 4. \ operator 5. MOD operator 6. + and - operator
6
Built-in Functions ► Take one or more input values ► Return an output value ► Already seen: Val(..), Str$(..), Asc(..), Chr(..)
7
Built-in Numeric Functions ► Sqr: calculates square-root and returns the value as double data type e.g: e.g: Sqr(9) is 3 Sqr(0) is 0 Sqr(2) is 1.414214 Sqr(30*2+4) is 8
8
Built-in Numeric Functions ► Int: Greatest integer less than and equal to a number e.g: e.g: Int(2.7) is 2 Int(3) is 3 Int(-2.7) is -3
9
Built-in String Functions ► Left$, Mid$, Right$, UCase$, Trim$ Left$(“fanatic”, 3) is “fan”Right$(“fanatic”, 3) is “tic” Left$(“12/15/93”,2) is “12”Right$(“12/15/93”, 2) is “93” Mid$(“fanatic”, 5,1) is “t”Mid$(“12/15/93”,4, 2) is “15” Ucase(“Disk”) is “DISK”Ucase(“12two”) is “12TWO” Trim$(“ 1 2 ”) is “1 2”Trim$(“-12 ”) is “-12”
10
Built-in Functions (Contd.) ► Len Len(“Shenandoah”) is ? Len(“Just a moment”) is ? Len(“m”) is ?
11
Built-in Functions (Contd.) ► Len Len(“Shenandoah”) is 10 Len(“Just a moment”) is 13 Len(“m”) is 1
12
Built-in Functions (Contd.) ► Instr Instr(“Shenandoah”, “nand”) is ? Instr(“Just a moment”, “ ”) is ? Instr(“Croissant”, “ist”) is ?
13
Built-in Functions (Contd.) ► Instr Instr(“Shenandoah”, “nand”) is 4 Instr(“Just a moment”, “ ”) is 5 Instr(“Croissant”, “ist”) is 0
14
Built-in Functions (Contd.) Abs(10) is 10 Abs(-10) is 10 Sgn(10) is 1 Sgn(0) is 0 Sgn(-10) is -1
15
Built-in Functions (Contd.) ► Format$ Format$(num, fmt) num: number, numeric expression, string of a number fmt: format string Result => String consisting of a formatted version of the number
16
Built-in Functions (Contd.) ► FunctionString Value Format$(12345.628, “Standard”)12,345.63 Format$(12345.628, “Currency”)$12,345.63 Format$(-1234, “Standard”)-1,234.00 Format$(-1234, “Standard”)-1,234.00 Format$(-1234, “Currency”)($1,234.00) Format$(-1234, “Currency”)($1,234.00) =>Result: two digits after decimal; Commas every three places to the left of the decimal point; leading ‘$’ sign for currency
17
Built-in Functions (Contd.) ► FunctionString Value Format$(1/4, “Standard”)? Format$(“.2”, “Currency”)? Format$(-1234.8, “Currency”)?
18
Built-in Functions (Contd.) ► FunctionString Value Format$(1/4, “Standard”)0.25 Format$(“.2”, “Currency”)$0.20 Format$(-1234.8, “Currency”)($1,234.80)
19
Built-in Functions (Contd.) Other functions Other functions ► FunctionString Value Format$(12345.628, “#,0”)12,346 Format$(-3.6, “#,0”)-4 Format$(12345.678, “Percent”)1234567.80% Format$(12345.678, “Scientific”)1.23E+04 Format$(2/3/03, “Long Date”)Monday February 3, 2003 Format$(2/3/03, “Medium Date”)03-Feb-03 =>More examples on following slides
20
Built-in Functions (Contd.) ► FunctionString Value Format$(12345.628, “#,0”)12,346 Format$(-3.6, “#,0”)-4 =>Result: Rounded; =>Result: Rounded; Commas every three places to the left of the decimal point
21
Built-in Functions (Contd.) ► FunctionString Value Format$(123.82, “#,0”)? Format$(-3.2, “#,0”)?
22
Built-in Functions (Contd.) ► FunctionString Value Format$(12345.628, “Percent”)1234562.80% =>Result: Multiplied by 100; trailing % sign; two digits after decimal
23
Built-in Functions (Contd.) ► FunctionString Value Format$(.06265, “Percent”)? Format$(1/8, “Percent”)?
24
Built-in Functions (Contd.) ► FunctionString Value Format$(.06265, “Percent”)6.27% Format$(1/8, “Percent”)12.50%
25
Built-in Functions (Contd.) ► FunctionString Value Format$(12345.628, “Scientific”)1.23E+04 =>Result: first number between 1 and 9.99 and of two digits after decimal; and of two digits after decimal; Exponent value preceded by E and sign; Exponent value preceded by E and sign;
26
Built-in Functions (Contd.) ► FunctionString Value Format$(-600.228, “Scientific”)? Format$(1/8, “Scientific”)?
27
Built-in Functions (Contd.) ► FunctionString Value Format$(-600.228, “Scientific”)-6.00E+02 Format$(1/8, “Scientific”)-1.25E-01
28
Built-in Functions (Contd.) Formatting dates: Formatting dates: ► FunctionString Value Format$(“7/4/96”, “Long Date”)Thursday, July 4, 1996 Format$(“7/4/96”, “Medium Date”) 04-Jul-96
29
Built-in Functions (Contd.) Fixed length string formatting: Fixed length string formatting: ► FunctionString Value Format$(1234567890, “@@@@@@@@@@”)? Format$(123, “@@@@@@@@@@”)? Format$(“1234.56”, “@@@@@@@@@@”)? Format$(“$1,234.56”, “@@@@@@@@@@”)? Format$(1/4, “@@@@@@@@@@”)?
30
Generating Random Numbers The function: Rnd The function: Rnd ► Generates a random number from 0 up to but not including 1 Picture1.Print Rnd‘print a different number each time Let numvar= Rnd‘a random value is assigned
31
Generating Random Numbers (Contd.) The function: Rnd The function: Rnd Display numbers from the set {1,2,3,4,5,6} randomly!
32
Generating Random Numbers (Contd.) The statement: Randomize Timer? The statement: Randomize Timer?
33
Generating Random Numbers (Contd.) The statement: Randomize Timer The statement: Randomize Timer Sub Command1_Click ( ) Rem Display a lottery number Picture1.Cls Randomize Timer Picture1.Print Int(10*Rnd); Picture1.Print Int(10*Rnd) End Sub
34
Sub Text1_KeyPress(KeyAscii as Integer) statements statements End Sub ► Text1_KeyPress event will occur when Text1 has the focus and a key is pressed Text1 has the focus and a key is pressed The Keypress Event Procedure
35
Sub Text1_KeyPress(KeyAscii as Integer) statements statements End Sub ► Keyascii is a variable (of type Integer) gets the ANSI value of the pressed key value is used to display the corresponding character in the Text1 at the end of this procedure The Keypress Event Procedure (Contd.)
36
The ANSI (ASCII) Code ► A 7 bit code representing one of the 95 characters (including space) ► Normally one byte is used to store this code
37
What will happen in these cases? Sub Text1_KeyPress(KeyAscii as Integer) Let KeyAscii =65 Let KeyAscii =65 End Sub Sub Text1_KeyPress(KeyAscii as Integer) Let KeyAscii =0 Let KeyAscii =0 End Sub The Keypress Event Procedure (Contd.)
38
Read Chapter 4 completely
39
Decisions
40
Decision Structure: Flowchart Process Step (s) 2 Is Condition True Process Step (s) 1
41
IF BLOCKS IF condition Then action1Else action 2 action 2 End If
42
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
43
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
44
IF BLOCK (Contd.) Result: b has the larger value Its value is: 5
45
IF BLOCK (Contd.) What the following program will do?
46
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
47
IF BLOCK (Contd.) Result: a has the larger value Its value is: 4
48
IF BLOCK EXTENDED IF condition 1 Then action1 ElseIf condition 2 Then action 2 action 2 ElseIf condition 3 Then action 3 End If
49
IF BLOCK EXTENDED(Contd.) What the following program will do?
50
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
51
IF BLOCK EXTENDED (Contd.) Result: b has the larger value
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.