Chapter 5 - VB 2005 by Schneider1 Chapter 5 – Decisions 5.1 Relational and Logical Operators 5.2 If Blocks.

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Chapter 51 ElseIf clause If condition1 Then action1 ElseIf condition2 Then action2 ElseIf condition3 Then action3 Else action4 End If.
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.
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.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
Chapter 5 - VB.Net by Schneider
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
CSC110 Fall Chapter 5: Decision Visual Basic.NET.
Chapter 5 - Visual Basic Schneider1 Chapter 5 Decisions.
Visual C++ Programming: Concepts and Projects
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
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)
CS0004: Introduction to Programming Relational Operators, Logical Operators, and If Statements.
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
Computer Science Selection Structures.
Chapter 5 – Decisions 5.1 Relational and Logical Operators 5.2 If Blocks 5.3 Select Case Blocks.
Lecture Set 5 Control Structures Part A - Decisions Structures.
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.
Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do While and Do Until Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks.
CONTROL STRUCTURE The if, elseif, and else & switch Statements 1.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Lesson - 5. Introduction While programming, we usually need to decide the path of the program flow according to the parameters and conditions. Actually.
Conditional Expression One of the most useful tools for processing information in an event procedure is a conditional expression. A conditional expression.
Copyright © 2012 Pearson Education, Inc. 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.
Chapter 51 Logical Operators Used with Boolean expressions Not – makes a False expression True and vice versa And – will yield a True if and only if both.
Chapter 51 Decisions Relational and Logical Operators If Blocks Select Case Blocks.
Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Chapter 4 Making Decision Csc 125 C++ programming language Fall 2005.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Introduction to Computer CC111 Week 10 Visual Basic 3 1.
Chapter 5 - VB 2008 by Schneider1 Chapter 5 – Repetition 5.1 Do Loops 5.2 Processing Lists of Data with Do Loops 5.3 For...Next Loops.
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.
Chapter 4 - VB 2008 by Schneider
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4 – Decisions 4.1 Relational and Logical Operators
Chapter 4 – Decisions 4.1 Relational and Logical Operators
Topics The if Statement The if-else Statement Comparing Strings
Objectives After studying this chapter, you should be able to:
VB Decisions, Conditions & If
Chapter 5 - Visual Basic Schneider
Visual Basic – Decision Statements
VB Decisions & Conditions
Chapter 3: Selection Structures: Making Decisions
Relational Operators.
Chapter 5 Decisions.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
The Selection Structure
Presentation transcript:

Chapter 5 - VB 2005 by Schneider1 Chapter 5 – Decisions 5.1 Relational and Logical Operators 5.2 If Blocks

Chapter 5 - VB 2005 by Schneider2 5.1 Relational and Logical Operators Relational Operators Logical Operators Boolean Data Type

Chapter 5 - VB 2005 by Schneider3 Relational Operators <less than <=less than or equal to >greater than >=greater than or equal to =equal to <>not equal to ANSI values are used to decide order for strings.

Chapter 5 - VB 2005 by Schneider4 Boolean Data Type An expression or variable that evaluates to either True or False is said to have Boolean data type. Example: The statement txtBox.Text = (2+3)<6 displays True in the text box.

Chapter 5 - VB 2005 by Schneider5 Example When a = 3, b = 4 (a + b) < 2 * a = 7 2 * 3 = 6 7 is NOT less than 6 and the value of the expression is False

Chapter 5 - VB 2005 by Schneider6 Relational Operator Notes Relational operators are binary – they require an operand on both sides of the operator Value of a relational expression will always be True or False Expressions are evaluated from left to right with no order of operations

Chapter 5 - VB 2005 by Schneider7 Logical Operators Used with Boolean expressions Not – makes a False expression True and vice versa And – will yield a True if and only if both expressions are True Or – will yield a True if at least one of both expressions are True

Chapter 5 - VB 2005 by Schneider8 Example To test if 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.

Chapter 5 - VB 2005 by Schneider9 Syntax error The following is NOT a valid way to test if n falls between 2 and 5: (2 < n < 5 )

Chapter 5 - VB 2005 by Schneider10 Order of Operations The order of operations for evaluating Boolean expressions is: 1.Arithmetic operators 2.Relational operators 3.Logical operators

Chapter 5 - VB 2005 by Schneider11 Arithmetic Order of Operations 1.Parenthesis 2.Exponentiation 3.Division and multiplication 4.Addition and subtraction

Chapter 5 - VB 2005 by Schneider12 Relational Order of Operations They all have the same precedence

Chapter 5 - VB 2005 by Schneider13 Logical Order of Operations 1. Not 2. And 3. Or

Chapter 5 - VB 2005 by Schneider14 Condition A condition is an expression involving relational and/or logical operators Result of the condition is Boolean – that is, True or False

Chapter 5 - VB 2005 by Schneider If Blocks If Block ElseIf Clause

Chapter 5 - VB 2005 by Schneider16 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

Chapter 5 - VB 2005 by Schneider17 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

Chapter 5 - VB 2005 by Schneider18 Pseudocode and Flowchart for an If Block

Chapter 5 - VB 2005 by Schneider19 Example 1: Form txtFirstNum txtSecondNum txtResult

Chapter 5 - VB 2005 by Schneider20 Example 1: Code Private Sub btnFindLarger_Click(...) _ Handles btnFindLarger.Click Dim num1, num2, largerNum As Double num1 = CDbl(txtFirstNum.Text) num2 = CDbl(txtSecondNum.Text) If num1 > num2 Then largerNum = num1 Else largerNum = num2 End If txtResult.Text = "The larger number is " & largerNum End Sub

Chapter 5 - VB 2005 by Schneider21 Example 1: Output

Chapter 5 - VB 2005 by Schneider22 Example 2: Form

Chapter 5 - VB 2005 by Schneider23 Example 2: Partial Code If costs = revenue Then txtResult.Text = "Break even" Else If costs < revenue Then profit = revenue - costs txtResult.Text = "Profit is " & _ FormatCurrency(profit) Else loss = costs - revenue txtResult.Text = "Loss is " & _ FormatCurrency(loss) End If

Chapter 5 - VB 2005 by Schneider24 Example 2: Output

Chapter 5 - VB 2005 by Schneider25 Example 3: Form txtAnswer txtSolution

Chapter 5 - VB 2005 by Schneider26 Example 3: Code Private Sub btnEvaluate_Click(...) _ Handles btnEvaluate.Click Dim answer As Double answer = CDbl(txtAnswer.Text) If (answer >= 0.5) And (answer <= 1) Then txtSolution.Text = "Good, " Else txtSolution.Text = "No, " End If txtSolution.Text &= "it holds about 3/4 of" _ & " a gallon." End Sub

Chapter 5 - VB 2005 by Schneider27 Example 3: Output

Chapter 5 - VB 2005 by Schneider28 ElseIf clause If condition1 Then action1 ElseIf condition2 Then action2 ElseIf condition3 Then action3 Else action4 End If

Chapter 5 - VB 2005 by Schneider29 Example 5: Form txtFirstNum txtSecondNum txtResult

Chapter 5 - VB 2005 by Schneider30 Example 5: Code Private Sub btnFindLarger_Click(...) _ Handles btnFindLarger.Click Dim num1, num2 As Double num1 = CDbl(txtFirstNum.Text) num2 = CDbl(txtSecondNum.Text) If (num1 > num2) Then txtResult.Text = "Larger number is " & num1 ElseIf (num2 > num1) Then txtResult.Text = "Larger number is " & num2 Else txtResult.Text = "The two are equal." End If End Sub

Chapter 5 - VB 2005 by Schneider31 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.

Chapter 5 - VB 2005 by Schneider32 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