56
Introduction to Introduction to Computing Computing Dr. Nadeem A Khan Dr. Nadeem A Khan

Introduction to Computing Dr. Nadeem A Khan. Lecture 4

  • View
    220

  • Download
    4

Embed Size (px)

Citation preview

Introduction to Introduction to ComputingComputing

Dr. Nadeem A KhanDr. Nadeem A Khan

Lecture 4Lecture 4

Local vs Form VariablesLocal vs Form Variables

Not only +,-,*,^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=3MOD operator e.g: 15.2 MOD 6=3

=> First round to closest integers before => First round to closest integers before operationoperation

More on OperatorsMore on Operators

More on Operators More on Operators (Contd.)(Contd.)

►Operator PrecedenceOperator Precedence

1. ^1. ^2.- operator (indicating a negative 2.- operator (indicating a negative value)value)3. * and / operator3. * and / operator4. \ operator4. \ operator5. MOD operator5. MOD operator6. + and - operator6. + and - operator

Built-in Functions Built-in Functions (Contd.)(Contd.)

► Format$Format$

Format$(Format$(num, fmtnum, fmt) ) num:num: number, numeric expression, string of number, numeric expression, string of a numbera numberfmt: fmt: format stringformat string

Result => String consisting of a formatted Result => String consisting of a formatted version of the numberversion of the number

Built-in Functions Built-in Functions (Contd.)(Contd.)

► FunctionFunction String ValueString Value

Format$(12345.628, “Standard”)Format$(12345.628, “Standard”) 12,345.6312,345.63Format$(12345.628, “Currency”)Format$(12345.628, “Currency”) $12,345.63$12,345.63

Format$(-1234, “Standard”)Format$(-1234, “Standard”) -1,234.00-1,234.00 Format$(-1234, “Currency”)Format$(-1234, “Currency”) ($1,234.00)($1,234.00)

=>=> Result: Result: two digits after decimal;two digits after decimal;Commas every three places to the Commas every three places to the

left of left of the decimal point; the decimal point;leading ‘$’ sign for currencyleading ‘$’ sign for currency

Built-in Functions Built-in Functions (Contd.)(Contd.)

► FunctionFunction String ValueString Value

Format$(1/4, “Standard”)Format$(1/4, “Standard”) ??Format$(“.2”, “Currency”)Format$(“.2”, “Currency”) ??

Format$(-1234.8, “Currency”)Format$(-1234.8, “Currency”) ??

Built-in Functions Built-in Functions (Contd.)(Contd.)

► FunctionFunction String ValueString Value

Format$(1/4, “Standard”)Format$(1/4, “Standard”) 0.250.25Format$(“.2”, “Currency”)Format$(“.2”, “Currency”) $0.20$0.20

Format$(-1234.8, “Currency”)Format$(-1234.8, “Currency”)($1,234.80)($1,234.80)

Built-in Functions Built-in Functions (Contd.)(Contd.) Other functionsOther functions

► FunctionFunction String ValueString Value

Format$(12345.628, “#,0”)Format$(12345.628, “#,0”) 12,34612,346Format$(-3.6, “#,0”)Format$(-3.6, “#,0”) -4-4Format$(12345.678, “Percent”)Format$(12345.678, “Percent”) 1234567.80%1234567.80%Format$(12345.678, “Scientific”)Format$(12345.678, “Scientific”) 1.23E+041.23E+04Format$(2/3/03, “Long Date”)Format$(2/3/03, “Long Date”) Monday Monday February 3, 2003February 3, 2003Format$(2/3/03, “Medium Date”)Format$(2/3/03, “Medium Date”) 03-Feb-0303-Feb-03

=>More examples on following slides=>More examples on following slides

Built-in Functions Built-in Functions (Contd.)(Contd.)

► FunctionFunction String ValueString Value

Format$(12345.628, “#,0”)Format$(12345.628, “#,0”) 12,34612,346Format$(-3.6, “#,0”)Format$(-3.6, “#,0”) -4-4

=>=> Result: Result: Rounded;Rounded;

Commas every three places to Commas every three places to the left of the left of the decimal pointthe decimal point

Built-in Functions Built-in Functions (Contd.)(Contd.)

► FunctionFunction String ValueString Value

Format$(123.82, “#,0”)Format$(123.82, “#,0”) ??Format$(-3.2, “#,0”)Format$(-3.2, “#,0”) ??

Built-in Functions Built-in Functions (Contd.)(Contd.)

► FunctionFunction String ValueString Value

Format$(12345.628, “Percent”)Format$(12345.628, “Percent”)1234562.80%1234562.80%

=>=> Result: Result: Multiplied by 100;Multiplied by 100;trailing % sign;trailing % sign;two digits after decimal two digits after decimal

Built-in Functions Built-in Functions (Contd.)(Contd.)

► FunctionFunction String ValueString Value

Format$(.06265, “Percent”)Format$(.06265, “Percent”) ??Format$(1/8, “Percent”)Format$(1/8, “Percent”) ??

Built-in Functions Built-in Functions (Contd.)(Contd.)

► FunctionFunction String ValueString Value

Format$(.06265, “Percent”)Format$(.06265, “Percent”) 6.27%6.27%Format$(1/8, “Percent”)Format$(1/8, “Percent”) 12.50%12.50%

Built-in Functions Built-in Functions (Contd.)(Contd.)

► FunctionFunction String ValueString Value

Format$(12345.628, “Scientific”)Format$(12345.628, “Scientific”) 1.23E+041.23E+04

=>=> Result: Result: first number between 1 and 9.99 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 Exponent value preceded by E and

sign; sign;

Built-in Functions Built-in Functions (Contd.)(Contd.)

► FunctionFunction String ValueString Value

Format$(-600.228, “Scientific”)Format$(-600.228, “Scientific”) ??Format$(1/8, “Scientific”)Format$(1/8, “Scientific”) ??

Built-in Functions Built-in Functions (Contd.)(Contd.)

► FunctionFunction String ValueString Value

Format$(-600.228, “Scientific”)Format$(-600.228, “Scientific”) --6.00E+026.00E+02Format$(1/8, “Scientific”)Format$(1/8, “Scientific”) -1.25E-01-1.25E-01

Built-in Functions Built-in Functions (Contd.)(Contd.)

Formatting dates:Formatting dates:

► FunctionFunction String ValueString Value

Format$(“7/4/96”, “Long Date”)Format$(“7/4/96”, “Long Date”) Thursday, July 4, Thursday, July 4, 19961996

Format$(“7/4/96”, “Medium Date”) Format$(“7/4/96”, “Medium Date”) 04-Jul-9604-Jul-96

Built-in Functions Built-in Functions (Contd.)(Contd.)

Fixed length string formatting:Fixed length string formatting:

► FunctionFunction String ValueString Value

Format$(1234567890, “@@@@@@@@@@”)Format$(1234567890, “@@@@@@@@@@”) ??Format$(123, “@@@@@@@@@@”)Format$(123, “@@@@@@@@@@”) ??Format$(“1234.56”, “@@@@@@@@@@”)Format$(“1234.56”, “@@@@@@@@@@”) ??Format$(“$1,234.56”, “@@@@@@@@@@”)Format$(“$1,234.56”, “@@@@@@@@@@”) ??Format$(1/4, “@@@@@@@@@@”)Format$(1/4, “@@@@@@@@@@”) ??

Generating Random Generating Random NumbersNumbers

The function: RndThe function: Rnd

► Generates a random number from 0 up to but not Generates a random number from 0 up to but not including 1including 1

Picture1.Print RndPicture1.Print Rnd ‘print a different number each ‘print a different number each timetime

Let numvar= RndLet numvar= Rnd ‘a random value is assigned ‘a random value is assigned

Generating Random Numbers Generating Random Numbers (Contd.)(Contd.)

The function: RndThe function: Rnd

Display numbers from the set Display numbers from the set {1,2,3,4,5,6} randomly!{1,2,3,4,5,6} randomly!

Generating Random Numbers Generating Random Numbers (Contd.)(Contd.)

The statement: Randomize Timer?The statement: Randomize Timer?

Generating Random Numbers Generating Random Numbers (Contd.)(Contd.)

The statement: Randomize TimerThe statement: Randomize Timer

Sub Command1_Click ( )Sub Command1_Click ( )Rem Display a lottery numberRem Display a lottery numberPicture1.ClsPicture1.ClsRandomize TimerRandomize TimerPicture1.Print Int(10*Rnd);Picture1.Print Int(10*Rnd);Picture1.Print Int(10*Rnd);Picture1.Print Int(10*Rnd);Picture1.Print Int(10*Rnd)Picture1.Print Int(10*Rnd)

End Sub End Sub

Sub Text1_KeyPress(KeyAscii as Integer)Sub Text1_KeyPress(KeyAscii as Integer)

statementsstatements

End SubEnd Sub

► Text1_KeyPress event will occur when Text1_KeyPress event will occur when

Text1 has the focus and a key is pressedText1 has the focus and a key is pressed

The Keypress Event The Keypress Event ProcedureProcedure

Sub Text1_KeyPress(KeyAscii as Integer)Sub Text1_KeyPress(KeyAscii as Integer) statementsstatementsEnd SubEnd Sub

► Keyascii Keyascii is a variable (of type Integer) is a variable (of type Integer) gets the ANSI value of the pressed keygets the ANSI value of the pressed key value is used to display the value is used to display the

corresponding character in the Text1 at corresponding character in the Text1 at the end of this procedure the end of this procedure

The Keypress Event The Keypress Event Procedure Procedure (Contd.)(Contd.)

The ANSI (ASCII) CodeThe ANSI (ASCII) Code

► A 7 bit code representing A 7 bit code representing one of the 95 characters one of the 95 characters (including space) (including space)

► Normally one byte is used to Normally one byte is used to store this codestore this code

What will happen in these cases?What will happen in these cases?

Sub Text1_KeyPress(KeyAscii as Integer)Sub Text1_KeyPress(KeyAscii as Integer)

Let KeyAscii =65Let KeyAscii =65

End SubEnd Sub

Sub Text1_KeyPress(KeyAscii as Integer)Sub Text1_KeyPress(KeyAscii as Integer)

Let KeyAscii =0Let KeyAscii =0

End SubEnd Sub

The Keypress Event The Keypress Event Procedure Procedure (Contd.)(Contd.)

Read Chapter 4 Read Chapter 4 completely completely

DecisionsDecisions

Decision Decision Structure:Structure:FlowchartFlowchart Process

Step (s) 2

Is Condition True

Process Step (s)

1

IF BLOCKSIF BLOCKS

IF IF conditioncondition Then Thenaction1action1

ElseElse action 2action 2

End IfEnd If

IF BLOCK IF BLOCK (Contd.)(Contd.)► Complete the program: Identifies and displays Complete the program: Identifies and displays

the larger value and its variable (assume the larger value and its variable (assume unequal values)unequal values)

Sub Command1_ClickSub Command1_ClickDim a As Single, b As Single, largerVal As SingleDim a As Single, b As Single, largerVal As SingleLet a=4Let a=4Let b=5Let b=5......Picture1. Print “Its value is:”;largerVal Picture1. Print “Its value is:”;largerVal End SubEnd Sub

IF BLOCK IF BLOCK (Contd.)(Contd.)Sub Command1_ClickSub Command1_Click

Dim a As Single, b As Single, largerVal As SingleDim a As Single, b As Single, largerVal As SingleLet a=4Let a=4Let b=5Let b=5If a>b ThenIf a>b Then

Picture1.Print “a has the larger value”Picture1.Print “a has the larger value”largerVal=alargerVal=a

ElseElsePicture1.Print “b has the larger value”Picture1.Print “b has the larger value”largerVal=blargerVal=b

End IfEnd IfPicture1. Print “Its value is:”;largerVal Picture1. Print “Its value is:”;largerVal

End SubEnd Sub

IF BLOCK IF BLOCK (Contd.)(Contd.)Result:Result:

b has the larger valueb has the larger valueIts value is: 5Its value is: 5

IF BLOCK IF BLOCK (Contd.)(Contd.)

What the following program will do?What the following program will do?

IF BLOCK IF BLOCK (Contd.)(Contd.)Sub Command1_ClickSub Command1_Click

Dim a As Single, b As Single, largerVal As SingleDim a As Single, b As Single, largerVal As SingleLet a=4Let a=4Let b=4Let b=4If (a>b) Or (a=b) ThenIf (a>b) Or (a=b) Then

Picture1.Print “a has the larger value”Picture1.Print “a has the larger value”largerVal=alargerVal=a

ElseElsePicture1.Print “b has the larger value”Picture1.Print “b has the larger value”largerVal=blargerVal=b

End IfEnd IfPicture1. Print “Its value is:”;largerVal Picture1. Print “Its value is:”;largerVal

End SubEnd Sub

IF BLOCK IF BLOCK (Contd.)(Contd.)Result:Result:

a has the larger valuea has the larger valueIts value is: 4Its value is: 4

IF BLOCK EXTENDEDIF BLOCK EXTENDED

IF IF conditioncondition 1 Then 1 Then

action1action1

ElseIf ElseIf conditioncondition 2 Then 2 Then

action 2action 2

ElseIf ElseIf conditioncondition 3 Then 3 Then

action 3action 3

End IfEnd If

IF BLOCK EXTENDEDIF BLOCK EXTENDED(Contd.)(Contd.)

What the following program will do?What the following program will do?

IF BLOCK EXTENDEDIF BLOCK EXTENDED(Contd.)(Contd.)Sub Command1_ClickSub Command1_Click

Dim a As Single, b As SingleDim a As Single, b As SingleLet a=4Let a=4Let b=5Let b=5If (a>b) ThenIf (a>b) ThenPicture1.Print “a has the larger value”Picture1.Print “a has the larger value”ElseIf (a<b) ThenElseIf (a<b) ThenPicture1.Print “b has the larger value”Picture1.Print “b has the larger value”Else Else Picture1.Print “a and b have same value”Picture1.Print “a and b have same value”End IfEnd If

End SubEnd Sub

IF BLOCK EXTENDED IF BLOCK EXTENDED (Contd.)(Contd.)

Result:Result:

b has the larger valueb has the larger value

ConditionsConditions

Conditions: ExamplesConditions: Examples

► ExpressionExpression True/FalseTrue/False

2 < 52 < 5 ??-5 > -2.5-5 > -2.5 ??1 < 11 < 1 ??1 = 11 = 1 ??3.5 <= 3.53.5 <= 3.5 ??-9 >= -35-9 >= -35 ??-2 <> -3-2 <> -3 ??

Conditions: Examples Conditions: Examples (Contd.)(Contd.)

► ExpressionExpression True/FalseTrue/False

2 < 52 < 5 TrueTrue-5 > -2.5-5 > -2.5 FalseFalse1 < 11 < 1 FalseFalse1 = 11 = 1 TrueTrue3.5 <= 3.53.5 <= 3.5 TrueTrue-9 >= -35-9 >= -35 TrueTrue-2 <> -3-2 <> -3 TrueTrue

Conditions: Examples Conditions: Examples (Contd.)(Contd.)

► ExpressionExpression True/FalseTrue/False

““cat” < “dog”cat” < “dog” ??““cart” < “cat”cart” < “cat” ??““cat” < “catalog”cat” < “catalog” ??““9W” < “bat”9W” < “bat” ??““Dog” < “cat” Dog” < “cat” ??““Sales-99” <= “Sales-retail”Sales-99” <= “Sales-retail” ??

Conditions: Examples Conditions: Examples (Contd.)(Contd.)

►For strings use the ANSI (or ASCI) tableFor strings use the ANSI (or ASCI) table

CharactersCharacters ANSI(ASCI) ValueANSI(ASCI) Value

Digits (0-9)Digits (0-9) 48-5748-57Upper Case (A-Z)Upper Case (A-Z) 65-9065-90Lower Case (a-z)Lower Case (a-z) 97-12297-122

► Compare two strings character by characterCompare two strings character by character

Conditions: Examples Conditions: Examples (Contd.)(Contd.)

► ExpressionExpression True/FalseTrue/False

““cat” < “dog”cat” < “dog” TrueTrue““cart” < “cat”cart” < “cat” TrueTrue““cat” < “catalog”cat” < “catalog” TrueTrue““9W” < “bat”9W” < “bat” TrueTrue““Dog” < “cat” Dog” < “cat” TrueTrue““Sales-99 <= “Sales-retail”Sales-99 <= “Sales-retail” TrueTrue

Conditions: Examples Conditions: Examples (Contd.)(Contd.)

► Assume a= 4; b= 3; c=“hello”; Assume a= 4; b= 3; c=“hello”; d=“bye” d=“bye”

►ExpressionExpression True/FalseTrue/False

(a + b) < 2*a(a + b) < 2*a ??(Len(c) - b) = (a/2)(Len(c) - b) = (a/2) ??c < “good” & dc < “good” & d ??

Conditions: Examples Conditions: Examples (Contd.)(Contd.)

► Assume a= 4; b= 3; c=“hello”; Assume a= 4; b= 3; c=“hello”; d=“bye” d=“bye”

►ExpressionExpression True/FalseTrue/False

(a + b) < 2*a(a + b) < 2*a TrueTrue(Len(c) - b) = (a/2)(Len(c) - b) = (a/2) TrueTruec < “good” & dc < “good” & d

FalseFalse

Conditions Conditions (Contd.)(Contd.)►Condition is an Condition is an expressionexpression involving involving

relational operators; it is either True or relational operators; it is either True or FalseFalse

►Relational operators:Relational operators:=; <>;<;>;<=;>==; <>;<;>;<=;>=

Conditions Conditions (Contd.)(Contd.)►Complex Conditions: Conditions joined Complex Conditions: Conditions joined

by Logical Operatorsby Logical Operators

cond1cond1 And And cond2cond2

cond1cond1 Or Or cond2cond2

Not Not cond1cond1

Conditions: Examples Conditions: Examples (Contd.)(Contd.)

► Assume n= 4; answ=“Y” Assume n= 4; answ=“Y”

►ExpressionExpression True/FalseTrue/False

(2<n)And(n<6) (2<n)And(n<6) ??Not(n<6)Not(n<6) ??(answ=“Y”) Or (answ=“y”)(answ=“Y”) Or (answ=“y”) ??

Conditions: Examples Conditions: Examples (Contd.)(Contd.)

► Assume n= 4; answ=“Y” Assume n= 4; answ=“Y”

►ExpressionExpression True/FalseTrue/False

(2<n)And(n<6) (2<n)And(n<6) TrueTrueNot(n<6)Not(n<6)

FalseFalse(answ=“Y”) Or (answ=“y”)(answ=“Y”) Or (answ=“y”) TrueTrue

Conditions Conditions (Contd.)(Contd.)►Operator hierarchy: Operator hierarchy:

In decreasing order of priority:In decreasing order of priority: ArithemeticArithemetic RelationalRelational LogicalLogical

►Logical operator hierarchy: Logical operator hierarchy: In decreasing order of priority:In decreasing order of priority: NotNot AndAnd OrOr

Conditions Conditions (Contd.)(Contd.)

=>=>

- Tie: Left most operator is first- Tie: Left most operator is first

- Use parantheses- Use parantheses