Download presentation
Presentation is loading. Please wait.
Published byTimothy Horn Modified over 8 years ago
1
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 1 Unit 4 Decisions Chapter 4 Making Decisions and Working with Strings
2
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 2 Chapter 4 Topics This chapter covers the Visual Basic.NET decision statements If … Then ElseIf It also discusses the use of Radio Buttons Message Boxes
3
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 3 The Decision Structure The Decision Structure Allows a Program to Have More Than One Path of Execution
4
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 4 Order of Statement Execution Thus far, all of our code statements have been executed sequentially To write meaningful programs our code needs to be able to Execute code conditionally (this chapter) Repeat sections of code (next chapter)
5
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 5 The Decision Structure Flowchart of a typical decision structure Evaluate the condition Execute, or not some code Condition Conditional Code True False
6
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 6 The If…Then Statement The If…Then Statement Causes Other Statements to Execute Only Under a Certain Condition
7
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 7 If…Then Statement Syntax New keywords used above: If Then End If condition Then statement (more statements as needed) End If
8
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 8 Relational Operators Usually the condition is formed with a relational operator >Greater than <Less than =Equal to <>Not equal to >=Greater than or equal to <=Less than or equal to
9
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 9 Binary Operators Operators are classified as to how many operands each takes Relational operators are binary operators -- each has two operands, e.g. length > width size <= 10 Relational operators yield a True or False result
10
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 10 If…Then Examples If sales > 50000 Then getsBonus = True End If If sales > 50000 Then getsBonus = True commissionRate = 0.12 daysOff = daysOff + 1 End If
11
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 11 If…Then Rules The 'If' and the 'Then' must be on the same line Only a remark may follow the 'Then' The 'End If' must be on a separate line Only a remark may follow the 'End If'
12
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 12 If…Then Conventions The code between the 'If…Then' and the 'End If' is indented Visual Basic.NET does not require this It is a convention among programmers to aid in the readability of programs By default, the Visual Basic.NET editor will automatically do this indentation as you enter your program
13
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 13 Relational Operators with Math Operators Either or both of the relational operator operands may themselves be expressions Math operators are done before the relational operators If x + y > a - b Then lblMessage.Text = "It is true!" End If
14
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 14 Relational Operators with Function Calls Either or both of the relational operator operands may themselves be function calls If Val(txtInput.Text) < 100 Then lblMessage.Text = "It is true!" End If
15
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 15 Boolean Variables as Flags A flag is a Boolean variable that signals when some condition exists in the program They can be used as the condition If quotaMet Then lblMessage.Text = "You have met your sales quota" End If
16
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 16 What an 'If…Then' Really Tests For Visual Basic.NET first evaluates the conditional expression If the result is 0, the condition is regarded as being False If the result is anything else, the condition is regarded as being True
17
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 17 The If…Then…Else Statement The If...Then...Else Statement Executes One Group of Statements If the Condition Is True and Another Group of Statements If the Condition Is False
18
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 18 'If…Then' vs. 'If…Then…Else' The 'If…Then' construct will execute or not a group of statements (do something or do nothing) The 'If…Then…Else' construct will execute one group of statements or another group (do this or do that)
19
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 19 The 'If…Then…Else' Structure Condition Execute If True True False Execute If False
20
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 20 'If…Then…Else' Example If temperature < 40 Then lblMesage.Text = “A little cold, isn’t it?” Else lblMesage.Text = “Nice weather we’re having!” End If
21
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 21 The If…Then…ElseIf Statement The If...Then…Elseif Statement Is Like a Chain of If...Then...Else Statements They Perform Their Tests, One After the Other, Until One of Them Is Found to Be True
22
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 22 Two Mutually Exclusive Choices ('If…Then…Else') The 'If…Then…Else' statement has two choices The conditional statement will either be True or False Hence, exactly one of the two choices will be selected They are mutually exclusive
23
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 23 More Than Two Mutually Exclusive Choices The 'If…Then…ElseIf' statement provides a series of mutually exclusive choices In pseudo code: If it is very cold Then Wear a coat Elseif it is chilly Wear a light jacket Elseif it is windy Wear a windbreaker Elesif it is hot Wear no jacket
24
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 24 In Visual Basic.NET Syntax If condition 1 Then Statement(s) 1 Elseif condition 2 Then Statements(s) 2 Elseif condition 3 Then Statements 3 … End If
25
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 25 In Flowchart Form C1C1 C2C2 C3C3 Statement(s) 1 True Statement(s) 2 True Statement(s) 3 True False
26
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 26 Example of Usage If average < 60 Then lblGrade.Text = "F" ElseIf average < 70 Then lblGrade.Text = "D" ElseIf average < 80 Then lblGrade.Text = "C" ElseIf average < 90 Then lblGrade.Text = "B" ElseIf average <= 100 Then lblGrade.Text = "A" End If
27
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 27 Contrast the Preceding Code To This Code Without the Elses If average < 60 Then lblGrade.Text = "F" End If If average < 70 Then lblGrade.Text = "D" End If If average < 80 Then lblGrade.Text = "C" End If If average < 90 Then lblGrade.Text = "B" End If If average <= 100 Then lblGrade.Text = "A" End If
28
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 28 The (Optional) Trailing Else A sequence of ElseIfs may end with a plain else (called a trailing Else) If none of the conditions were True, the statement(s) after this Else will be executed Continuing with the preceding example on the next slide:
29
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 29 Use of a Trailing Else If average < 60 Then lblGrade.Text = "F" ElseIf average < 70 Then lblGrade.Text = "D" ElseIf average < 80 Then lblGrade.Text = "C" ElseIf average < 90 Then lblGrade.Text = "B" ElseIf average <= 100 Then lblGrade.Text = "A" Else lblGrade.Text = "Invalid" End If
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.