Chapter 5 - Visual Basic Schneider1 Chapter 5 Decisions.

Slides:



Advertisements
Similar presentations
3 Decision Making: Equality and Relational Operators A condition is an expression that can be either true or false. Conditions can be formed using the.
Advertisements

Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:
Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks.
Introduction to Computing Dr. Nadeem A Khan. Lecture 21.
Decisions (Conditional Programming) Chapter 5 (Sec. 5.1 & 5.2)
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 5 - VB.Net by Schneider
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
Selection Structures Relational and Logical Operators; If Statements.
Making Decisions In Python
Chapter 4: Control Structures: Selection
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
Chapter 51 Select Case block A decision-making structure that simplifies choosing among several actions. Avoids complex nested If constructs. If blocks.
T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
Review for Exam 2 School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 10, Friday 3/21/2003) - IF Blocks - Do Loops - Select.
Computer Science Selection Structures.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
Chapter 5 - VB 2005 by Schneider1 Chapter 5 – Decisions 5.1 Relational and Logical Operators 5.2 If Blocks.
Chapter 6 - Visual Basic Schneider 1 Chapter 6 Repetition.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -
Chapter Making Decisions 4. Relational Operators 4.1.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Review for Final (Part 2) School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 15, Friday 5/2/2003)
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
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.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
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.
Select Case Blocks School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 8, Monday 3/03/2003)
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Chapter 4 - VB 2008 by Schneider
Decisions Chapter 4.
Chapter 4: Making Decisions.
EGR 2261 Unit 4 Control Structures I: Selection
Topics The if Statement The if-else Statement Comparing Strings
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4: Control Structures I (Selection)
Chapter 5 - Visual Basic Schneider
Visual Basic – Decision Statements
Selection Statements.
Chapter 4: Control Structures I (Selection)
VB Decisions & Conditions
Chapter 5 Decisions.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Presentation transcript:

Chapter 5 - Visual Basic Schneider1 Chapter 5 Decisions

Chapter 5 - Visual Basic Schneider2 Outline and Objective n Relational and Logical Operators n If Blocks n Select Case Blocks

Chapter 5 - Visual Basic Schneider3 Relational Operators n The execution of the block If is controlled by a Boolean Expression n A Boolean Expression can be either True or False n Boolean Expressions are formed by using six Relational operators and three Logical operators

Chapter 5 - Visual Basic Schneider4 Relational Operators

Chapter 5 - Visual Basic Schneider5 Using Relational Operators on Strings n Computer uses a special coding system to compare character strings n Usually ANSI (also called ASCII) coding system is used on PC. n The association between characters and values of characters are given in Appendix A.

Chapter 5 - Visual Basic Schneider6 Example of Comparing Character Strings “Chase” < “Chaz” True “ Cat” < “Cat” True “Pay” < “Pay “ True “Jones” <> “James” True “Hope” < “Hopeful” True

Chapter 5 - Visual Basic Schneider7 Logical Operators n The result of a logical operator is also True or False n The three Logical Operators are: Not And Or

Chapter 5 - Visual Basic Schneider8 Not n Not: Negates a single expression n Example: Suppose answ = “Y” Not ( answ = “y”) is True

Chapter 5 - Visual Basic Schneider9 And n Combines two expressions; each expression must be True for the entire expression to be True. n Example: Suppose answ = “Y” ä (answ = “Y”) And (answ = “y”) is False

Chapter 5 - Visual Basic Schneider10 Or n Combines two expressions; either expression (or both expressions) must be True for the entire expression to be True. n Example: Suppose answ = “Y” ä (answ = “Y”) Or (answ = “y”) is True

Chapter 5 - Visual Basic Schneider11 Determine whether the following conditions are true or false? n (“Y” <> “X”) And ( < ) n ( 0 = 14) Or (6 ^ <= 4 / 2 + 8) n Not ( 6 = 7) And ( 44 > 33)

Chapter 5 - Visual Basic Schneider12 Control Structures n Allows the programmer to alter the normal flow of statement execution. n Types of control structures: ä Loop ä Decision

Chapter 5 - Visual Basic Schneider13 Types of Decision Structures n The Block If Statement ä Simple alternative if ä Compound alternative if….else n Select Case Statement

Chapter 5 - Visual Basic Schneider14 Single Alternative Decision n An action is taken if the condition is true, otherwise the control goes to the next statement.

Chapter 5 - Visual Basic Schneider15 Single Alternative Decision n Syntax If condition Then action(s) End If If condition is true, action(s) is executed;otherwise action(s) is skipped

Chapter 5 - Visual Basic Schneider16 Example of single-alternative decision If (grade > = 90) Then picOutput.Print “Excellent Student” letterGrade = “A” picOutput.Print “Your grade is “; letterGrade End If

Chapter 5 - Visual Basic Schneider17 Compound Alternative Decision n Syntax If condition Then action1 Else action 2 End If

Chapter 5 - Visual Basic Schneider18 Example of Compound If statement (find the larger of two numbers) Private Sub cmdFindLarger_Click() Dim largerNum As Single picResult.Cls If Val(txtFirstNum.Text) > Val(txtSecondNum.Text) Then largerNum = Val(txtFirstNum.Text) Else largerNum = Val(txtSecondNum.Text) End If picResult.Print "The larger number is"; largerNum End Sub

Chapter 5 - Visual Basic Schneider19 Important Points in Using If Statement n Use indentation n In the nested If statement, each If must have its own End If statement n Care should be taken to make If blocks easy to understand

Chapter 5 - Visual Basic Schneider20 Example If cond1 Then If cond2 Then action(s) End If If cond1 And cond2 Then action(s) End If This is easier to understand A confusing If Block

Chapter 5 - Visual Basic Schneider21 Compound Alternative Decision n Syntax If condition1 Then action1 ElseIf condition2 Then action 2 ElseIf condittion3 Then action3 Else action4 End If

Chapter 5 - Visual Basic Schneider22 Example (find the larger of two numbers, and report if the two numbers are equal) Private Sub cmdFindLarger_Click() picResult.Cls If Val(txtFirstNum.Text) > Val(txtSecondNum.Text) Then picResult.Print "The larger number is"; txtFirstNum.Text ElseIf Val(txtSecondNum.Text) > Val(txtFirstNum.Text) Then picResult.Print "The larger number is "; txtSecondNum.Text Else picResult.Print "The two numbers are equal." End If End Sub

Chapter 5 - Visual Basic Schneider23 Example (find cost of phone call from NY to LA) Private Sub cmdDisplay_Click() Dim length As Single Call InputLength (length) Call DisplayCost(length) End Sub

Chapter 5 - Visual Basic Schneider24 Sub DisplayCost & InputLenght Private Sub DisplayCost (length As Single) ' Display the cost of a call PicOutput.Print "Cost of call: " ; FormatCurrency ( Cost (length)) End Sub Private Sub InputLength(length As Single) ' Request the length of a phone call length = Val (InputBox("Duration of the call in minutes? ")) End Sub

Chapter 5 - Visual Basic Schneider25 Function Cost Private Function Cost (length As Single) As Single If length < 1 Then Cost =.46 Else Cost =.46 + (length –1) *.36 End If End Function

Chapter 5 - Visual Basic Schneider26 The Select Case Block n Similar to If statement n Used instead of nested If statement n An action to be selected from a list of alternatives n Avoids confusion of deeply nested If blocks

Chapter 5 - Visual Basic Schneider27 Select Case Block (Syntax) Select Case selector Case value-list-1 action1 Case value-list-2 action2 ….. Case Else action of last resort End Select

Chapter 5 - Visual Basic Schneider28 Select Case Block Each value-list contains one or more of the following types of items separated by a comma n a constant n a variable n an expression n an inequality sign preceded by Is and followed by a constant, variable, or expression n a range expressed in the form a To b, where a and b are constants, variables, or expressions.

Chapter 5 - Visual Basic Schneider29 Example of Select Case letterGrade = txtGrade.text Select Case letterGrade Case “A”, “B” picOutput.Print “ Good Work” Case “C” picOutput.Print “Average Work” Case Else picOutputPrint “Poor Work” End Select

Chapter 5 - Visual Basic Schneider30 Example of If statement letterGrade = txtGrade.Text If ( letterGrade = “A”) Or (letterGrade = “B”) Then picOutput.print “Good Work” ElseIf (letterGrade = “C”) Then picOutput.Print “ Average Work” Else picOutput.Print “Poor Work” End If

Chapter 5 - Visual Basic Schneider31 Several different types of value-list n Private Sub cmdInterpret_Click() Dim x As Integer, y As Integer, num As Integer ' One, Two, Buckle My Shoe picPhrase.Cls x = 2 y = 3 num = Val(txtNumber.Text) Select Case num Case y - x, x picPhrase.Print "Buckle my shoe." Case Is <= 4 picPhrase.Print "Shut the door." Case x + y To x * y picPhrase.Print "Pick up sticks." Case 7, 8 picPhrase.Print "Lay them straight." Case Else picPhrase.Print "Start all over again." End Select End Sub

Chapter 5 - Visual Basic Schneider32 Selector Is a String Variable Private Sub cmdInterpret_Click() Dim firstName As String firstName = txtAnswer.Text Select Case firstName Case "Thomas" picSolution.Print "Correct." Case "Woodrow" picSolution.Print "Sorry, his full name was" picSolution.Print "Thomas Woodrow Wilson." Case "President" picSolution.Print "Are you for real?" Case Else picSolution.Print "Nice try, but no cigar." End Select End Sub

Chapter 5 - Visual Basic Schneider33 Summary Points n How to use If statements n Select Case statements n How to use relational operators n How to use Not, And, and Or in Boolean expressions