Lecture 07 - GEOCITIES.ws
Download
Report
Transcript Lecture 07 - GEOCITIES.ws
Lecture 7 – Decision & Conditions
Decisions 決定
Relational and Logical Operators
If 假如,如果
1
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
txtFirstName
txtLastName
Peter
Chan
txtResult
Write code to join the texts so that txtResult will look like as
below?
txtResult
My Name is Peter Chan.
txtResult.text = “My Name is ”
Revision Exercise: Symbol
Which 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
txtName
1. Write code to assign “Peter” into a textbox?
txtName
Peter
=
2. Write code to end a program?
3. Write code to reset the insertion point in a textbox?
Relational and
Logical Operators
Example:
If the room is cold, I will bring more clothes.
Condition 條件
Decisions 決定
• Result 結果 of the condition is Boolean –True or
False.
Room = True OR Room = False
• Condition is an expression 詞句
假如我愛她,我會給她一束鮮花﹒
8
Relational Operators in
VB.Net
<
<=
>
>=
=
<>
less than
less than or equal to
greater than
greater than or equal to
equal to
not equal to
9
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
10
Another Example
a = 4 b = 3 c = "hello" d = "bye“
字串 “c” 的長
度
( c.Length – b ) = ( a / 2 )
5–3=2
4/2=2
True because 2 equals 2
11
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
12
Relational Operator Notes
• Result of a relational expression will
always be Boolean
(True/False)
• They are evaluated from left to right
13
Logical Operators (1)
• Used for joining 連接 Boolean expressions
Example:
Boolean expressions / Conditions
If the room is cold AND I have clothes,
I will more enjoy the lesson.
Decisions 決定
假如我愛她而且她也喜歡我,我們可以快快樂樂一起…
14
Logical Operators (2)
• And – will result a True if and only if both
expressions are True
AND Gate: Truth Table
T/F?
A
B
T
T
T
F
F
T
F
F
Output
T/F?
If the room is cold AND I have clothes, I will more enjoy
the lesson.
15
Logical Operators (3)
• Or – will result a True if one or the other
or both expressions are True
OR Gate: Truth Table
T/F?
A
B
T
T
T
F
F
T
F
F
Output
T/F?
If today is holiday OR today is weekend, I will be happy.
愛情不是遊戲,幸福怎能單靠一方的愛
16
Logical Operators (4)
• Not – makes a False condition True and a True
condition False
NOT Gate: Truth Table
A
Output
T
F
T/F?
If it is not raining, I will not bring umbrella.
17
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.
18
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
19
Exercise
n = 4, answ = “Y” Are the following
True/False
conditions true or false?
Not (n < 6)
(answ = "Y") Or (answ = "y")
(answ = "Y") And (answ = "y")
Not(answ = "y")
20
Order of Operations
The order of operations for evaluating
Boolean expressions is:
1. Arithmetic operators +,-,*,/
2. Relational operators <,<=,>,>=,=,<>
3. Logical operators AND,OR,NOT
21
Logical Order of Operations
1. Not
2. And
3. Or
22
Exercise
Determine whether the following conditions are
true or false.
Let a = 2, b = 3
True/False
1.
(5 – a) * b < 7
2.
NOT (a < b)
3.
(a * a < b) OR NOT (a * a < a)
23
Common Error in Boolean
Expressions
Let n falls between 3 and 5:
•
•
•
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 >
24
Exercise
Determine whether the following conditions are
true or false.
Let a = 2, b = 3
True/False
1.
“Inspector” < “gadget”
2.
a <> b
3.
((a=b) OR NOT (b < a)) AND ((a < b) OR (b = a + 1))
25
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
26
If Block
The program will take a course of action
based on whether a condition is true.
If condition Then
Will be executed if
action1
condition is true
Else
Will be executed if
action2
condition is false
End If
27
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
28
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
2
3
4
5
6
Exercise: Find the larger number (2)
• In VS.NET 2003, which area should you put the code inside?
7
Line 3
8
Line 4-123
Don’t change!
2
1
3
4
5
9
6
Line 144
Exercise: Find the larger number (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
_________ = num1
Else
largerNum = ______
End If
txtResult.Text = _The larger number is " __ __________
txtNum1
txtNum2
txtResult
Answer: Find the larger number
Dim num1, num2, largerNum As Decimal
num1 = CDec(txtNum1.Text)
num2 = CDec(txtNum2.Text)
If num1 > num2 Then
largerNum = num1
Else
largerNum = num2
End If
txtResult.Text = "The larger number is " & largerNum
32
Exercise:
Determine what output will be displayed in the text box when
the button is clicked.
Private Sub btnDisplay_Click (…) Handles btnDisplay.Click
Dim gpa As Double = 3.49
txtOutput.Clear()
If gpa >=3.5 Then
txtOutput.Text = “Good”
End If
txtOutput.Text = txtOutput.Text & “ Student”
End Sub
What is the output result of the text box?
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 If
Pass
0
40
Indentation
85
100
34
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
0
40
85
100
35
Simplified Nested If Statement
If cond1 Then
If cond2 Then
action
End If
End If
Nested
If
If cond1 And cond2 Then
action
End If
Less
Confusing
36
Testing Radio Buttons &
Check Boxes
Private Sub btuDisplay_Click()
If chkRed.Checked=True Then
txtResult.ForeColor = ForeColor.Red
End If
End Sub
Place the IF code in the Click
event for a Button, such as
btnDisplay
37
Compound Conditions
Example # 1
If radMale.Checked = True And CInt(txtAge.Text) < 21 Then
… ‘Will the program continue to run
…
End If
Situation
radMale not checked
txtAge greater than 21
radMale not checked, txtAge less than 21
radMale checked, txtAge 21 or greater
radMale checked, txtAge less than 21
True/False
38
Compound Conditions
Example # 2
If radJunior.Checked = True Or radSenior.Checked = True Then
… ’Will the program continue to run
…
End If
Situation
radJunior.Value=True
radSenior.Value=True
radJunior.Value=False, radSenior.Value=True
radJunior.Value=True, radSenior.Value=False
radJunior.Value=False, radSenior.Value=False
True/False
39
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.
40