1 lecture 7 – decision & conditions decisions 決定 +relational and logical operators +if...

Post on 01-Jan-2016

229 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

1

Lecture 7 – Decision & Conditions

Decisions 決定 Relational and Logical OperatorsIf 假如 , 如果

Naming Rules: Name Property• How the programmer refers to a control in

code • Name must begin with a letter• Must be less than __________long• May include numbers and the underscore ( )• Use appropriate __character naming _______

e.g. txtName, lblName

IntelliSense 編碼輔助Automatically 自動 pops up to give the programmer help.

Intellisense anticipates 預測 your needs 需要 during coding and displays prompts 提示 to assist 幫助 you in coding

Revision Exercise: Control

Which control will you use to display your name?____________

Which control will you use to get user’s click action?____________

Which control will you use for user to choose the kinds of fruit he/she likes, e.g. apple, banana, orange?____________

Revision Exercise: Concatenation

Write code to join the texts so that txtResult will look like as below?

Peter Chan

txtFirstName txtLastName

txtResult

My Name is Peter Chan.txtResult

txtResult.text = “My Name is ”

Revision Exercise: SymbolWhich symbol can be used to enclose a text string in code, e.g. “Hello World” and then assign it to a textbox?____________

Which symbol can be used to write a non-executable comment?____________

Which symbol will you use to break a long statement into different lines?____________

Revision Exercise: Code

1. Write code to assign “Peter” into a textbox?

txtName

Peter

=

txtName

2. Write code to end a program?

3. Write code to reset the insertion point in a textbox?

8

Relational and Logical Operators

• Result 結果 of the condition is Boolean –True or False.

Room = True OR Room = False• Condition is an expression 詞句

Example:

If the room is cold, I will bring more clothes.

假如我愛她 , 我會給她一束鮮花﹒

Condition 條件

Decisions 決定

9

Relational Operators in VB.Net

< less than

<= less than or equal to

> greater than

>= greater than or equal to

= equal to

<> not equal to

10

Example

When a = 3, b = 4

(a + b) < 2 * a

3 + 4 = 7 2 * 3 = 6

7 is NOT less than 6 and the result of the expression is False

11

Another Example

a = 4 b = 3 c = "hello" d = "bye“

( c.Length – b ) = ( a / 2 )

5 – 3 = 2 4 / 2 = 2

True because 2 equals 2

字串 “ c” 的長度

12

Comparison Tips• Negative numbers (e.g. -1,-2) are always less than

positive numbers (e.g. 1,2,3)• Strings can be compared also (don't forget to enclose

the strings in quotes) see Page 150 for ASCII Chart)• JOAN is less than JOHN• HOPE is less than HOPELESS• Joan does not equal JOAN

• Numbers are always less than letters• 300ZX is less than Porsche

13

Relational Operator Notes

• Result of a relational expression will always be Boolean (True/False)

• They are evaluated from left to right

14

Logical Operators (1)• Used for joining 連接 Boolean expressions

Example:

If the room is cold AND I have clothes, I will more enjoy the lesson.

Boolean expressions / Conditions

Decisions 決定

假如我愛她而且她也喜歡我 , 我們可以快快樂樂一起…

15

Logical Operators (2)• And – will result a True if and only if both

expressions are True

A B Output

T T

T F

F T

F F

AND Gate: Truth Table

If the room is cold AND I have clothes, I will more enjoy the lesson.

T/F? T/F?

16

Logical Operators (3)• Or – will result a True if one or the other

or both expressions are True

A B Output

T T

T F

F T

F F

OR Gate: Truth Table

If today is holiday OR today is weekend, I will be happy.

T/F? T/F?

愛情不是遊戲 , 幸福怎能單靠一方的愛

17

Logical Operators (4)• Not – makes a False condition True and a

True condition False

A Output

T

F

NOT Gate: Truth Table

If it is not raining, I will not bring umbrella.

T/F?

18

Example

Let n falls between 2 and 5:

(2 < n ) And ( n < 5 )

A complete relational expression must be on either side of the logical operators And and Or.

19

Syntax error

The following is NOT a valid way to test if n falls between 2 and 5:

Invalid: (2 < n < 5 )

Valid: 2 < n AND n < 5

20

Exercise

n = 4, answ = “Y” Are the following conditions true or false?

Not (n < 6)

(answ = "Y") Or (answ = "y")

(answ = "Y") And (answ = "y")

Not(answ = "y")

True/False

21

Order of Operations

The order of operations for evaluating Boolean expressions is:

1. Arithmetic operators +,-,*,/

2. Relational operators <,<=,>,>=,=,<>

3. Logical operators AND,OR,NOT

22

Logical Order of Operations

1. Not

2. And

3. Or

23

ExerciseDetermine whether the following conditions are

true or false.

Let a = 2, b = 3

1. (5 – a) * b < 7

2. NOT (a < b)

3. (a * a < b) OR NOT (a * a < a)

True/False

24

Common Error in Boolean Expressions

• A common error is to replace the condition Not ( 2 < n ) by the condition

( 2 > n )

• The correct replacement is ( 2 >= n )

• Because >= is the opposite of <, just as <= is the opposite of >

Let n falls between 3 and 5:

25

ExerciseDetermine whether the following conditions are

true or false.

Let a = 2, b = 3

1. “Inspector” < “gadget”

2. a <> b

3. ((a=b) OR NOT (b < a)) AND ((a < b) OR (b = a + 1))

True/False

26

If …Then…Else

• Used to make decisions

• Always indent 縮排 for readability

• Then must be on same line as If

• End If and Else must appear alone on a line

• Notice that End If is 2 words

• Always End with End If

27

If Block

The program will take a course of action based on whether a condition is true.

If condition Then

action1

Else

action2

End If

Will be executed if condition is true

Will be executed if condition is false

28

Another example If block

If condition Then

action1

End If

Statement2

Statement3

Regardless of whether

the condition in the

If statement is True or

False, these statements

will be executed

Exercise: Find the larger number (1)• This application is used to find the bigger number. • After you have entered 2 numbers into the first two textboxes and have clicked the button, the

bigger number will then be displayed in the last textbox. • In VS.NET 2003, which area should I double click and put the code inside?

1

3

2

4

65

Exercise: Find the larger number (2)• In VS.NET 2003, which area should you put the code inside?

2

1

3

4

5

6

7

8

9

Line 4-123 Don’t change!

Line 144

Line 3

Fill in the blanks:Hints: Label1, Label1.text, txtNum1, txtNum1.Text, txtNum2, txtNum2.Text,txtResult, txtResult.Text, num1, num2, largerNum, CInt, CDec, Interger,Double, Decimal, Date

Dim num1, num2, largerNum As ____________num1 = CDec(____________)num2 = CDec(txtNum2.Text)If num1 > num2 Then

_________ = num1Else

largerNum = ______End IftxtResult.Text = _The larger number is " __ __________

Exercise: Find the larger number (3)

txtNum1

txtNum2

txtResult

32

Answer: Find the larger number

Dim num1, num2, largerNum As Decimalnum1 = CDec(txtNum1.Text)num2 = CDec(txtNum2.Text)If num1 > num2 Then

largerNum = num1Else

largerNum = num2End IftxtResult.Text = "The larger number is " & largerNum

Exercise:Determine what output will be displayed in the text box when the button is clicked.

Private Sub btnDisplay_Click (…) Handles btnDisplay.ClickDim gpa As Double = 3.49txtOutput.Clear()

If gpa >=3.5 ThentxtOutput.Text = “Good”

End IftxtOutput.Text = txtOutput.Text & “ Student”

End Sub

What is the output result of the text box?

34

Example: Find your grade (1)Dim mark As Integer

mark = txtMath.Text

If mark >= 85 And mark <= 100 Then

txtResult.Text = "Distinction"

End If

If mark >= 40 And mark < 85 Then

txtResult.Text = "Pass"

End If

If mark >= 0 And mark < 40 Then

txtResult.Text = "Fail"

End IfIndentation

Pass

85 100400

35

Nested Ifs Example: Find your grade (2)

Dim mark As Integer

mark = CInt(txtMath.Text)

If mark >= 85 And mark <= 100 Then

txtResult.Text = "Distinction"

Else

If mark >= 40 Then

txtResult.Text = "Pass"

Else

txtResult.Text = "Fail"

End If

End If

Indentation

Pass

85 100400

36

Simplified Nested If StatementIf cond1 Then If cond1 And cond2 Then

If cond2 Then action

action End If

End If

End If

NestedIf

LessConfusing

37

Testing Radio Buttons & Check Boxes

Place the IF code in the Click

event for a Button, such as

btnDisplay

Private Sub btuDisplay_Click()If chkRed.Checked=True Then

txtResult.ForeColor = ForeColor.RedEnd If

End Sub

38

Compound Conditions Example # 1

If radMale.Checked = True And CInt(txtAge.Text) < 21 Then… ‘Will the program continue to run…

End If

Situation True/False radMale not checked txtAge greater than 21radMale not checked, txtAge less than 21radMale checked, txtAge 21 or greaterradMale checked, txtAge less than 21

39

Compound Conditions Example # 2

If radJunior.Checked = True Or radSenior.Checked = True Then… ’Will the program continue to run…

End If

Situation True/FalseradJunior.Value=TrueradSenior.Value=TrueradJunior.Value=False, radSenior.Value=TrueradJunior.Value=True, radSenior.Value=FalseradJunior.Value=False, radSenior.Value=False

40

Comments

• When one If block is contained inside another If block, the structure is referred to as nested If blocks.

• Care should be taken to make If blocks easy to understand.

top related