VB Decisions, Conditions & If

Slides:



Advertisements
Similar presentations
Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks.
Advertisements

An Introduction to Programming with C++ Fifth Edition Chapter 5 The Selection Structure.
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
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.
1 Selection Structures. 2 Making Decisions Sample assignment statements to figure worker pay with possible overtime PayAmount = Hours * Rate PayAmount.
C++ for Engineers and Scientists Third Edition
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
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
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.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks.
Selection Control Structures. Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses.
Chapter 5 - VB 2005 by Schneider1 Chapter 5 – Decisions 5.1 Relational and Logical Operators 5.2 If Blocks.
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Chapter 51 Decisions Relational and Logical Operators If Blocks Select Case Blocks.
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.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
Computer Programming TCP1224 Chapter 5 The Selection Structure.
Review for Final (Part 2) School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 15, Friday 5/2/2003)
COMPUTER PROGRAMMING I 5.04 Apply Decision Making Structures.
110 F-1 Decisions and Conditions Chapter 4: We can design a form We can calculate To make our programs more powerful, we need to be able to make choices.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
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.
TUTORIAL 4 Visual Basic 6.0 Mr. Crone. Pseudocode Pseudocode is written language that is part-code part- English and helps a programmer to plan the layout.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
 Type Called bool  Bool has only two possible values: True and False.
Chapter 4: Decisions and Conditions
Control Structures I Chapter 3
5.04 Apply Decision Making Structures
Chapter 4: Decisions and Conditions
Chapter 4 - VB 2008 by Schneider
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
The Selection Structure
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4: Decision Structures and Boolean Logic
Chapter 4: Making Decisions.
Chapter 4 – Decisions 4.1 Relational and Logical Operators
An Introduction to Programming with C++ Fifth Edition
Topics The if Statement The if-else Statement Comparing Strings
Objectives After studying this chapter, you should be able to:
Comparing Strings Strings can be compared using the == and != operators String comparisons are case sensitive Strings can be compared using >, =, and.
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
VB Decisions & Conditions
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Chapter 5 Decisions.
Chapter 3: Selection Structures: Making Decisions
The Selection Structure
Chapter 3: Selection Structures: Making Decisions
3.0 - Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Presentation transcript:

VB Decisions, Conditions & If Chapter 5.1 & 5.2 12/30/2018 Prepared By: Deborah Becker

Prepared By: Deborah Becker Covered Tipics Relational & Logical Operators Using And, Or, and Not If Blocks Read & Create Flowcharts (Logic) 12/30/2018 Prepared By: Deborah Becker

Prepared By: Deborah Becker A Condition Definition Condition is an expression involving relational operators (such as > or =) that is either true or false. Example: “Frog” = “Frog” 12 > 8 = 12/30/2018 Prepared By: Deborah Becker

Prepared By: Deborah Becker Conditions > greater than < less than = equal to <> not equal to >= greater than or equal to <= less than or equal to 12/30/2018 Prepared By: Deborah Becker

Mathematical Relational Operators 12/30/2018 Prepared By: Deborah Becker

Prepared By: Deborah Becker Is the Condition True? 1 <= 1 1 < 1 “car” < “cat” “Dog” < “dog” 2. False < means less than. No number can be less than itself True Characters of a string are compared one at a time, working from left to right. The first two characters match and the third decides if the condition is true. R precedes T in the alphabet. True Uppercase letters precede lowercase letters in the ANSI table. 12/30/2018 Prepared By: Deborah Becker

Are These Conditions True? Let a = 4 and b = 3 Let c = “hello” and d = “bye” (a + b) < (2 * a) (Len(c) – b) = (a / 2) C < (“good” & d) 1. True 4 + 3 = 7 and 2 * 4 = 8 2. True Len(c) = 5 – 3 = 2 and (4 / 2) = 2 False H follows G in the ANSI table 12/30/2018 Prepared By: Deborah Becker

Prepared By: Deborah Becker Logical Operators The three main logical operators are And Or Not Some programming conditions require more complicated comparison then just true or false 12/30/2018 Prepared By: Deborah Becker

Logical Operators allow us to.. Evaluate condition1 and condition2 based on the following condition1 and condition2 condition1 or condition2 condition1 not condition2 12/30/2018 Prepared By: Deborah Becker

Are these conditions T/F? Dim answ as String = “Y” (answ = “Y”) or (answ = “y”) (answ = “Y”) and (answ = “y”) Not (answ = “y”) True the condition become “Y” = “Y” False the condition would be false for every condition fo answ. You can not set a variable = to two separate values simultaneously True because “Y” = “y” is false 12/30/2018 Prepared By: Deborah Becker

Prepared By: Deborah Becker Using Parentheses Using parentheses can improve readability. Place parentheses in this equation to help evaluate it. a < b + c OR d < e AND f = g 12/30/2018 Prepared By: Deborah Becker

Is this easier to understand? ((a < (b + c)) Or ((d < c) And (F = g)) A condition involving numeric variables is different from an algebraic truth. The condition (a + b) < 2 * a is not a valid algebraic truth because it is not true for all values of a and b VB will evaluate it as true if it is correct for the current values of the variables 12/30/2018 Prepared By: Deborah Becker

Prepared By: Deborah Becker The IF Statements If School Day False True Stay In Bed Go to Class Action Action The if block allows a program to decide on a course of action. In the If statement You are always testing the condition to determine if it is True or False If no Else statement is included then the statement is only executed if the condition is true. 12/30/2018 Prepared By: Deborah Becker

If Statement Examples in VB If … Then (all on one line) If strName = "Jamie" Then lblMessage.text = "Hello Jamie" Block If … Then … End If If strName = "Sam" Then lblMessage.text = "Hello Sam" login(strName) End If In the If Statement, when the condition is true, the Then clause is executed; when the condition is false, only the Else clause, if present, is executed. The if block must contain an End If. It is always two words Time 12/30/2018 Prepared By: Deborah Becker

Block If…Then…Else…End If If strName = "Connie" Then lblMessage.text = "Hello Connie" Login(strName) Else lblMessage.text = "Hello” msgbox(“Not a valid User”) End If The if then else clause can contain an else condition If today is cloudy then go to class Else go to beach End if 12/30/2018 Prepared By: Deborah Becker

If…then…ElseIf…then…end if If School Day True False If Sunny Stay In Bed False True Play Tennis Go to Class 12/30/2018 Prepared By: Deborah Becker

Prepared By: Deborah Becker Example If costs = revenue Then lblResult.text = “Break even” ElseIf costs < revenue Then profit = revenue – costs lblResult.text = “Profit is “; _ FormatCurrency(profit) Else loss = costs – revenue lblResult.text = “Loss is “; _ FormatCurrency(loss) End if Elseif is written as one word End if is always two words. 12/30/2018 Prepared By: Deborah Becker

If Block and Logic Operators The If Block can be combined with logic operators to compare more than one condition If (answer >= .5) and (answer <= 1) Then lblSolution.text = “true”; Else lblSolution.text = “false” End if 12/30/2018 Prepared By: Deborah Becker

Nesting Ifs—MultipleValidations If you needed to check several input fields using nested ifs can eliminate the need to display multiple message statements Error messages are only displayed for the conditions that are false (Else) 12/30/2018 Prepared By: Deborah Becker

Additional Information It is not necessary to indent If Blocks We indent to improve readability When constructs contain If Blocks inside other If Blocks, they are referred to as Nested Ifs Some times the use of relational operators can simplify the If Block structure making it easier to understand If condition1 Then If condition2 Then action End if If condition1 and condition2 then 12/30/2018 Prepared By: Deborah Becker

Prepared By: Deborah Becker How can we simplify If condition1 Then If condition2 Then action End if If condition1 and condition2 then action End if 12/30/2018 Prepared By: Deborah Becker

Prepared By: Deborah Becker Flowcharting the Ifs Flowcharts are a good tool to evaluate If Blocks We will use some example problems to see how flowcharting can add clarification to analyzing a problem 12/30/2018 Prepared By: Deborah Becker

Prepared By: Deborah Becker The Problem In New Jersey the state income tax is based on a persons base income at three different interval levels The tax on Income <= $20,000 is taxed at 2% of income The tax on Income > $20,000 but <= $50,000 is $400 plus 2.5% of (income -$20,000) The tax on income > $50,000 is $1150 plus 3.5% of (income - $50,000) 12/30/2018 Prepared By: Deborah Becker

Prepared By: Deborah Becker 12/30/2018 Prepared By: Deborah Becker

Prepared By: Deborah Becker The End That’s All Folks 12/30/2018 Prepared By: Deborah Becker