400 likes | 411 Views
Lecture 7 – Decision & Conditions. Decisions 決定 Relational and Logical Operators If 假如 , 如果. e.g. txtName, lblName. Naming Rules: Name Property. How the programmer refers to a control in code Name must begin with a letter Must be less than __________long
E N D
Lecture 7 – Decision & Conditions Decisions 決定 Relational and Logical Operators If 假如,如果
e.g. txtName, lblName 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 _______
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 txtLastName txtFirstName 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. • Result結果of the condition is Boolean –True or False. Room = True OR Room = False • Condition is an expression 詞句 Decisions 決定 Condition 條件 假如我愛她,我會給她一束鮮花﹒
Relational Operators in VB.Net < less than <= less than or equal to > greater than >= greater than or equal to = equal to <> not equal to
Example When a = 3, b = 4 (a + b) < 2 * a 2 * 3 = 6 3 + 4 = 7 7 is NOT less than 6 and the result of the expression is False
Another Example a = 4 b = 3 c = "hello" d = "bye“ ( c.Length – b ) = ( a / 2 ) 字串 “c” 的長度 5 – 3 = 2 4 / 2 = 2 True because 2 equals 2
Comparison Tips • Negative numbers (e.g. -1,-2) are always less thanpositive 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
Relational Operator Notes • Result of a relational expression will always be Boolean (True/False) • They are evaluated from left to right
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 決定 假如我愛她而且她也喜歡我,我們可以快快樂樂一起…
Logical Operators (2) • And – will result a True if and only if both expressions are True AND Gate: Truth Table T/F? T/F? If the room is cold AND I have clothes, I will more enjoy the lesson.
Logical Operators (3) • Or – will result a True if one or the other or both expressions are True OR Gate: Truth Table T/F? T/F? If today is holiday OR today is weekend, I will be happy. 愛情不是遊戲,幸福怎能單靠一方的愛
Logical Operators (4) • Not – makes a False condition True and a True condition False NOT Gate: Truth Table T/F? If it is not raining, I will not bring umbrella.
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.
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
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
Order of Operations The order of operations for evaluating Boolean expressions is: • Arithmetic operators +,-,*,/ • Relational operators <,<=,>,>=,=,<> • Logical operators AND,OR,NOT
Logical Order of Operations • Not • And • Or
Exercise Determine whether the following conditions are true or false. Let a = 2, b = 3 • (5 – a) * b < 7 • NOT (a < b) • (a * a < b) OR NOT (a * a < a) True/False
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:
Exercise Determine whether the following conditions are true or false. Let a = 2, b = 3 • “Inspector” < “gadget” • a <> b • ((a=b) OR NOT (b < a)) AND ((a < b) OR (b = a + 1)) True/False
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
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
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 2 3 4 6 5
Exercise: Find the larger number (2) • In VS.NET 2003, which area should you put the code inside? 7 Line 3 Line 4-123 Don’t change! 8 2 1 3 4 5 9 Line 144 6
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
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 85 100 0 40 Indentation
Nested IfsExample: 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 100 0 40
Simplified Nested If Statement If cond1 Then If cond1 And cond2 Then If cond2 Then action action End If End If End If Less Confusing Nested If
Testing Radio Buttons & Check Boxes • Private Sub btuDisplay_Click() • If chkRed.Checked=True Then • txtResult.ForeColor = ForeColor.Red • EndIf • End Sub Place the IF code in the Click event for a Button, such as btnDisplay
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 21 radMale not checked, txtAge less than 21 radMale checked, txtAge 21 or greater radMale checked, txtAge less than 21
Compound Conditions Example # 2 If radJunior.Checked = True Or radSenior.Checked = True Then … ’Will the program continue to run … End If Situation True/False radJunior.Value=True radSenior.Value=True radJunior.Value=False, radSenior.Value=True radJunior.Value=True, radSenior.Value=False radJunior.Value=False, radSenior.Value=False
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.