1 Lecture 7 – Decision & Conditions Decisions 決定 +Relational and Logical Operators +If 假如, 如果.

Slides:



Advertisements
Similar presentations
1.
Advertisements

Chapter 4 Decisions and Conditions Programming In Visual Basic.NET.
Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks.
Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Chapter 5 - VB.Net by Schneider
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
CSC110 Fall Chapter 5: Decision Visual Basic.NET.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
Decision Structures and Boolean Logic
2440: 211 Interactive Web Programming Expressions & Operators.
Computer Science Selection Structures.
Chapter 5 – Decisions 5.1 Relational and Logical Operators 5.2 If Blocks 5.3 Select Case Blocks.
Chapter 4 - VB 2008 by Schneider1 Chapter 4 – Decisions 4.1 Relational and Logical Operators 4.2 If Blocks 4.3 Select Case Blocks.
Chapter 5 - VB 2005 by Schneider1 Chapter 5 – Decisions 5.1 Relational and Logical Operators 5.2 If Blocks 5.3 Select Case Blocks 5.4 A Case Study: Weekly.
Revision Exercise: Control Which control will you use to display your name? ____________ Which control will you use to get user’s click action? ____________.
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
3 - Variables Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks.
Chapter 5 - VB 2005 by Schneider1 Chapter 5 – Decisions 5.1 Relational and Logical Operators 5.2 If Blocks.
Fundamentals of GUI Programming. Objectives: At the end of the session, you should be able to: describe the guidelines that are used for creating user-friendly.
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
Lecture 4 Programming Technique Programming Appreciation.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
22/11/ Selection If selection construct.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
Chapter Making Decisions 4. Relational Operators 4.1.
Chapter 3 Control Structures. The If…Then Statement The If…Then statement is a Decision statement = that executes a set of statements when a condition.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
1 Chapter 4 – Decisions 4.1 Relational and Logical Operators 4.2 If Blocks 4.3 Select Case Blocks 4.4 Input via User Selection.
Chapter 4 Getting Started with VBA. Subroutines Subroutine is the logical section of code that performs a particular task. Subroutine is also called a.
31/01/ Selection If selection construct.
Decision Statements, Short- Circuit Evaluation, Errors.
Controlling Program Flow with Decision Structures.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Computer Science Up Down Controls, Decisions and Random Numbers.
Chapter 4 - VB 2008 by Schneider1 Chapter 4 – Decisions 4.1 Relational and Logical Operators 4.2 If Blocks 4.3 Select Case Blocks.
1 Chapter 5 – Decisions 5.1 Relational and Logical Operators 5.2 If Blocks 5.3 Select Case Blocks 5.4 A Case Study: Weekly Payroll.
Week 4 Relational and Logical Operators Dr. Jennifer Cunningham.
 Type Called bool  Bool has only two possible values: True and False.
Chapter 4: Decisions and Conditions
A variable is a name for a value stored in memory.
Chapter 4: Decisions and Conditions
Chapter 4 - VB 2008 by Schneider
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4: Making Decisions.
Javascript Conditionals.
Chapter 4 – Decisions 4.1 Relational and Logical Operators
Topics The if Statement The if-else Statement Comparing Strings
Conditions and Ifs BIS1523 – Lecture 8.
Chapter 3: Introduction to Problem Solving and Control Statements
Chapter 5 - Visual Basic Schneider
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Chapter 5 Decisions.
Presentation transcript:

1 Lecture 7 – Decision & Conditions Decisions 決定 +Relational and Logical Operators +If 假如, 如果

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? PeterChan txtFirstName txtLastName txtResult My Name is Peter Chan. txtResult 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 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 = TrueORRoom = 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 = 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 = 24 / 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 ABOutput TT TF FT FF AND Gate: Truth Table If the room is cold AND I have clothes, I will more enjoy the lesson. T/F?

16 Logical Operators (3) Or – will result a True if one or the other or both expressions are True ABOutput TT TF FT FF OR Gate: Truth Table If today is holiday OR today is weekend, I will be happy. T/F? 愛情不是遊戲, 幸福怎能單靠一方的愛

17 Logical Operators (4) Not – makes a False condition True and a True condition False AOutput 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 Exercise Determine 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 Let n falls between 3 and 5:

25 Exercise Determine 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?

Exercise: Find the larger number (2) In VS.NET 2003, which area should you put the code inside? Line 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 _________ = num1 Else largerNum = ______ End If txtResult.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 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?

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 If Indentation Pass

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 Indentation Pass

36 Simplified Nested If Statement If cond1 Then If cond1 And cond2 Then If cond2 Then action action End If End If Nested If Less Confusing

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.Red End 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 21 radMale not checked, txtAge less than 21 radMale checked, txtAge 21 or greater radMale 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/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

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.