Download Lecture 6 - Suraj @ LUMS

Document related concepts
no text concepts found
Transcript
Introduction to
Computing
Dr. Nadeem A Khan
Lecture 4
Local vs Form Variables
More on Operators
Not only +,-,*,^
But also:
\ opeartor
e.g: 5.1\2.04= 2
MOD operator e.g: 15.2 MOD 6=3
=> First round to closest integers before operation
More on Operators (Contd.)
► Operator
Precedence
1. ^
2.- operator (indicating a negative value)
3. * and / operator
4. \ operator
5. MOD operator
6. + and - operator
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
Built-in Functions (Contd.)
►
Function
String Value
Format$(12345.628, “Standard”)
Format$(12345.628, “Currency”)
12,345.63
$12,345.63
Format$(-1234, “Standard”)
Format$(-1234, “Currency”)
=>
Result:
-1,234.00
($1,234.00)
two digits after decimal;
Commas every three places to the left of
the decimal point;
leading ‘$’ sign for currency
Built-in Functions (Contd.)
►
Function
String Value
Format$(1/4, “Standard”)
Format$(“.2”, “Currency”)
?
?
Format$(-1234.8, “Currency”)
?
Built-in Functions (Contd.)
►
Function
String Value
Format$(1/4, “Standard”)
Format$(“.2”, “Currency”)
0.25
$0.20
Format$(-1234.8, “Currency”)
($1,234.80)
Built-in Functions (Contd.)
Other functions
►
Function
String Value
Format$(12345.628, “#,0”)
Format$(-3.6, “#,0”)
Format$(12345.678, “Percent”)
Format$(12345.678, “Scientific”)
Format$(2/3/03, “Long Date”)
Format$(2/3/03, “Medium Date”)
12,346
-4
1234567.80%
1.23E+04
Monday February 3, 2003
03-Feb-03
=>More examples on following slides
Built-in Functions (Contd.)
►
Function
String Value
Format$(12345.628, “#,0”)
Format$(-3.6, “#,0”)
12,346
-4
=> Result:
Rounded;
Commas every three places to the left of
the decimal point
Built-in Functions (Contd.)
►
Function
String Value
Format$(123.82, “#,0”)
Format$(-3.2, “#,0”)
?
?
Built-in Functions (Contd.)
►
Function
String Value
Format$(12345.628, “Percent”)
1234562.80%
=>
Result:
Multiplied by 100;
trailing % sign;
two digits after decimal
Built-in Functions (Contd.)
►
Function
String Value
Format$(.06265, “Percent”)
Format$(1/8, “Percent”)
?
?
Built-in Functions (Contd.)
►
Function
String Value
Format$(.06265, “Percent”)
Format$(1/8, “Percent”)
6.27%
12.50%
Built-in Functions (Contd.)
►
Function
String Value
Format$(12345.628, “Scientific”)
1.23E+04
=>
Result:
first number between 1 and 9.99
and of two digits after decimal;
Exponent value preceded by E and
sign;
Built-in Functions (Contd.)
►
Function
Format$(-600.228, “Scientific”)
Format$(1/8, “Scientific”)
String Value
?
?
Built-in Functions (Contd.)
►
Function
String Value
Format$(-600.228, “Scientific”)
Format$(1/8, “Scientific”)
-6.00E+02
-1.25E-01
Built-in Functions (Contd.)
Formatting dates:
►
Function
Format$(“7/4/96”, “Long Date”)
Format$(“7/4/96”, “Medium Date”)
String Value
Thursday, July 4, 1996
04-Jul-96
Built-in Functions (Contd.)
Fixed length string formatting:
►
Function
Format$(1234567890, “@@@@@@@@@@”)
Format$(123, “@@@@@@@@@@”)
Format$(“1234.56”, “@@@@@@@@@@”)
Format$(“$1,234.56”, “@@@@@@@@@@”)
Format$(1/4, “@@@@@@@@@@”)
String Value
?
?
?
?
?
Generating Random Numbers
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
Generating Random Numbers
(Contd.)
The function: Rnd
Display numbers from the set {1,2,3,4,5,6}
randomly!
Generating Random Numbers
(Contd.)
The statement: Randomize Timer?
Generating Random Numbers
(Contd.)
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);
Picture1.Print Int(10*Rnd)
End Sub
The Keypress Event Procedure
Sub Text1_KeyPress(KeyAscii as Integer)
statements
End Sub
►
Text1_KeyPress event will occur when
Text1 has the focus and a key is pressed
The Keypress Event Procedure
(Contd.)
Sub Text1_KeyPress(KeyAscii as Integer)
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 ANSI (ASCII) Code
A 7 bit code representing one of
the 95 characters (including
space)
►
Normally one byte is used to
store this code
►
The Keypress Event Procedure
(Contd.)
What will happen in these cases?
Sub Text1_KeyPress(KeyAscii as Integer)
Let KeyAscii =65
End Sub
Sub Text1_KeyPress(KeyAscii as Integer)
Let KeyAscii =0
End Sub
Read Chapter 4 completely
Decisions
Is
Condition
True
Decision
Structure:
Flowchart
Process
Step (s)
2
Process
Step (s)
1
IF BLOCKS
IF condition Then
action1
Else
action 2
End If
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
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=a
Else
Picture1.Print “b has the larger value”
largerVal=b
End If
Picture1. Print “Its value is:”;largerVal
End Sub
IF BLOCK (Contd.)
Result:
b has the larger value
Its value is: 5
IF BLOCK (Contd.)
What the following program will do?
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=a
Else
Picture1.Print “b has the larger value”
largerVal=b
End If
Picture1. Print “Its value is:”;largerVal
End Sub
IF BLOCK (Contd.)
Result:
a has the larger value
Its value is: 4
IF BLOCK EXTENDED
IF condition 1 Then
action1
ElseIf condition 2 Then
action 2
ElseIf condition 3 Then
action 3
End If
IF BLOCK EXTENDED(Contd.)
What the following program will do?
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
IF BLOCK EXTENDED (Contd.)
Result:
b has the larger value
Conditions
Conditions: Examples
►
Expression
True/False
2<5
-5 > -2.5
1<1
1=1
3.5 <= 3.5
-9 >= -35
-2 <> -3
?
?
?
?
?
?
?
Conditions: Examples (Contd.)
►
Expression
True/False
2<5
-5 > -2.5
1<1
1=1
3.5 <= 3.5
-9 >= -35
-2 <> -3
True
False
False
True
True
True
True
Conditions: Examples (Contd.)
►
Expression
“cat” < “dog”
“cart” < “cat”
“cat” < “catalog”
“9W” < “bat”
“Dog” < “cat”
“Sales-99” <= “Sales-retail”
True/False
?
?
?
?
?
?
Conditions: Examples (Contd.)
► For
►
strings use the ANSI (or ASCI) table
Characters
ANSI(ASCI) Value
Digits (0-9)
Upper Case (A-Z)
Lower Case (a-z)
48-57
65-90
97-122
Compare two strings character by character
Conditions: Examples (Contd.)
►
Expression
“cat” < “dog”
“cart” < “cat”
“cat” < “catalog”
“9W” < “bat”
“Dog” < “cat”
“Sales-99 <= “Sales-retail”
True/False
True
True
True
True
True
True
Conditions: Examples (Contd.)
►
Assume a= 4; b= 3; c=“hello”; d=“bye”
► Expression
(a + b) < 2*a
(Len(c) - b) = (a/2)
c < “good” & d
True/False
?
?
?
Conditions: Examples (Contd.)
►
Assume a= 4; b= 3; c=“hello”; d=“bye”
► Expression
(a + b) < 2*a
(Len(c) - b) = (a/2)
c < “good” & d
True/False
True
True
False
Conditions (Contd.)
► Condition
is an expression involving
relational operators; it is either True or False
► Relational
operators:
=; <>;<;>;<=;>=
Conditions (Contd.)
► Complex
Conditions: Conditions joined by
Logical Operators
 cond1 And cond2
 cond1 Or cond2
 Not cond1
Conditions: Examples (Contd.)
►
Assume n= 4; answ=“Y”
► Expression
True/False
(2<n)And(n<6)
Not(n<6)
(answ=“Y”) Or (answ=“y”)
?
?
?
Conditions: Examples (Contd.)
►
Assume n= 4; answ=“Y”
► Expression
True/False
(2<n)And(n<6)
Not(n<6)
(answ=“Y”) Or (answ=“y”)
True
False
True
Conditions (Contd.)
► Operator
hierarchy:
In decreasing order of priority:
 Arithemetic
 Relational
 Logical
► Logical
operator hierarchy:
In decreasing order of priority:
 Not
 And
 Or
Conditions (Contd.)
=>
- Tie: Left most operator is first
- Use parantheses
Related documents