VB Decisions & Conditions

Slides:



Advertisements
Similar presentations
Visual Basic Statements Chapter 5. Relational Operators  OperationSymbol  Equal  =  Less than  <  Greater than  >  Not equal   Less than.
Advertisements

Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
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.
Slide 1 VB Program Flow Control. Slide 2 Making Decisions v Decision Statement: control the execution of parts of the program based on conditions. v The.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Comparing Numeric Values If Val(Text1.Text) = MaxPrice Then (Is the current numeric value stored in the Text property of Text1 equal to the value stored.
CSC110 Fall Chapter 5: Decision Visual Basic.NET.
Chapter 5 - Visual Basic Schneider1 Chapter 5 Decisions.
JavaScript, Third Edition
1 Selection Structures. 2 Making Decisions Sample assignment statements to figure worker pay with possible overtime PayAmount = Hours * Rate PayAmount.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
CIS162AD - C# Decision Statements 04_decisions.ppt.
CS0004: Introduction to Programming Relational Operators, Logical Operators, and If Statements.
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.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
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 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.
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.
CS285 Visual Basic 2 Department of Computing UniS 1 Statements in Visual Basic A statement is the fundamental syntactical element of a program smallest.
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.
1 Scripting Languages VBScript - Recognized mainly by Internet Explorer only - Netscape does have a plug-in JavaScript - Recognized by Internet Explorer.
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.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
Decision Structures, String Comparison, Nested Structures
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.
Controlling Program Flow with Decision Structures.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Chapter 4 - VB 2008 by Schneider1 Chapter 4 – Decisions 4.1 Relational and Logical Operators 4.2 If Blocks 4.3 Select Case Blocks.
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.
 Type Called bool  Bool has only two possible values: True and False.
Chapter 4: Decisions and Conditions
VISUAL BASIC 6.0 Designed by Mrinal Kanti Nath.
5.04 Apply Decision Making Structures
Chapter 4: Decisions and Conditions
Chapter 4 The If…Then Statement
Chapter 4: Making Decisions.
Chapter 3 Branching Statements
The Selection Structure
Decision Making in Code Logical Tests & Truth Logical Expressions
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4: Making Decisions.
JavaScript: Control Statements I
Making Decisions in a Program
Topics The if Statement The if-else Statement Comparing Strings
Decision Structures, String Comparison, Nested Structures
Chapter 8 JavaScript: Control Statements, Part 2
Objectives After studying this chapter, you should be able to:
VB Decisions, Conditions & If
Decisions and Conditions
Chapter 5 - Visual Basic Schneider
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Chapter 5 Decisions.
Chapter 3: Selection Structures: Making Decisions
Presentation transcript:

VB Decisions & Conditions 2/21/2019 Prepared By: Deborah Becker

Prepared By: Deborah Becker Covered Tipics Conditions defined Relational operators Logical Operators If Blocks Read & Create Flowcharts (Logic) 2/21/2019 Prepared By: Deborah Becker

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

Relational Conditions > greater than < less than = equal to <> not equal to >= greater than or equal to <= less than or equal to Expressions containing comparison operators always result in an answer that is either true or false. Example expressions using Visual Basic controls: txtName.text <> "Frockmeister" Val(txtAge.Text) > 25 optPrintForm.Value = True chkDiscardStyles.Value = False txtInterestRate.Text / 12 <= 0.05 2/21/2019 Prepared By: Deborah Becker

Examples of Conditions txtName.text <> "Frockmeister" Val(txtAge.Text) > 25 optPrintForm.Value = True chkDiscardStyles.Value = False (Val(txtInterestRate.Text) / 12) <= 0.05 2/21/2019 Prepared By: Deborah Becker

Mathematical Relational Operators 2/21/2019 Prepared By: Deborah Becker

Prepared By: Deborah Becker Is the Condition True 1 <= 1 1 < 1 “car” < “cat” “Dog” > “dog” Remember the ASCII Table 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. 2/21/2019 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) * The function Len() returns a numeric value that indicated the number of characters in the string, inside the function. 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 2/21/2019 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 2/21/2019 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 2/21/2019 Prepared By: Deborah Becker

With Relational and Logical Conditions We Can Comparing Numeric Variables & Constants Comparing Strings Comparing text property of text boxes Uppercase and lowercase character comparisons Compound conditions When comparing values from text boxes with numeric values, be sure to first convert the text box data to a value. For example: If txtPrice.Text = curRetailPrice Then ... can result in an inaccurate comparison of a string and a currency value. Better to do this: If Val(txtPrice.Text) = curRetailPrice Then ... Also, if the text box is empty, then Val(textbox.text) is equal to zero. That can be very handy. It eliminates one possible error when you use a value in calculations When comparing strings, make sure you “negate” any potential problems with case not matching. Generally, you simply want the spelling to match. Compare these two slightly different conditions: If txtLastName.Text = “Smith” or If Lcase(txtLastName.Text) = “smith” You can use the logical operators of And, Or, and Not to form compound expressions such as: If (Ucase(txtLastName) = “SMITH” And Val(txtAge) > 25) Then... If (Val(txtAge.Text) = 0 Or txtLastName.Text = “”) Then ... Using parentheses around the conditional statement is optional but desirable 2/21/2019 Prepared By: Deborah Becker

Comparing Numbers & Strings If txtPrice.Text = curRetailPrice Then ... If Val(txtPrice.Text) = curRetailPrice Then ... Compare these two slightly different conditions: If txtLastName.Text = “Smith” If Lcase(txtLastName.Text) = “smith” 2/21/2019 Prepared By: Deborah Becker

Are these conditions T/F? Let the string variable answ = “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 2/21/2019 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 Not f = g 2/21/2019 Prepared By: Deborah Becker

Prepared By: Deborah Becker Answer a < (b + c) Or (d < c) And (Not 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 2/21/2019 Prepared By: Deborah Becker

Prepared By: Deborah Becker Compound Conditions If a1 <= a2 or a3 > a4 and a5 < a6 Then And has precedence over Or All comparison operators have precedence over all logical operators Use parentheses () to alter the order of evaluation Parentheses alter the order of evaluation and are good for explicitly documenting groupings or are good if you are unsure of precedence. If (curValue > 100) and (curValue < 200) Then lblMessage.Caption = "Value between 101 and 199" End If The following is a project called SimpleSample.vbp to illustrate the example on pages 137-138 2/21/2019 Prepared By: Deborah Becker

Prepared By: Deborah Becker Parentheses Parentheses alter the order of evaluation, are good for explicitly documenting groupings, and are good if you are unsure of precedence. If (curValue > 100) and (curValue < 200) Then lblMessage.Caption = "Value between 101 and 199" End If 2/21/2019 Prepared By: Deborah Becker

IF Statement Structure (1) Sleepy? Go to bed True False Form: If condition Then statement(s) End If 2/21/2019 Prepared By: Deborah Becker

IF Statements Structure (2) 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. 2/21/2019 Prepared By: Deborah Becker

Prepared By: Deborah Becker If Examples in VB If … Then (all on one line) If strName = "Jamie" Then lblMessage.Caption _ = "Hello Jamie" Block If … Then … End If If strName = "Sam" Then lblMessage.Caption = "Hello Sam" [another statement] 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 2/21/2019 Prepared By: Deborah Becker

Block If…Then…Else…End If If strName = "Connie" Then lblMessage.Caption = "Hello Connie" [another statement] Else lblMessage.Caption = "Hello Stranger" [another statement] 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 2/21/2019 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 2/21/2019 Prepared By: Deborah Becker

Prepared By: Deborah Becker Example of Nested IF If costs = revenue Then picResult.Print = “Break even” Else If costs < revenue Then profit = revenue – costs picResult.Print “Profit is “; _ FormatCurrency(profit) Else loss = costs – revenue picResult.Print “Loss is “; _ FormatCurrency(loss) End if Elseif is written as one word End if is always two words. 2/21/2019 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 picSolution.Print “Good”; Else picSolution.Print “No” End if 2/21/2019 Prepared By: Deborah Becker

Additional Information It is not necessary to indent If Blocks We indent to improve readability Some times the use of relational operators make the If Block structure easier to understand If condition1 Then If condition2 Then action End if If condition1 and condition2 then 2/21/2019 Prepared By: Deborah Becker

Prepared By: Deborah Becker The Select Case Block Executes a statement based on the value of a variable Select Case bytMonth Case 1, 3, 5, 7, 8, 10, 12 number_of_days = 31 Case 2 number_of_days = 28 Case 4, 6, 9, 11 number_of_days = 30 End Select If more branches are required then a ‘Select Case’ command can be used. Notice that in the following example, several values can trigger the same case statement. For example, the months 1, 3, 5, 7, 8, 10 and 12 all are 31 days long. Instead of writing 12 separate case statements, all these values can be included in one case by using commas. The ‘End Select’ statement defines the end of the Case block. To ‘trap’ any values not picked up by earlier cases used the ‘Case Else’ construct. Case Else MsgBox "ERROR: Month must be between 1 and 12!“ End Select 2/21/2019 Prepared By: Deborah Becker

Prepared By: Deborah Becker Looking for a Number Select Case Score Case is = 0 ' Score is still 0 lblMsgbox ("Keep trying!") Case 1To 4 ' Score is between 1 and 4. lblMsgbox ("Making a good start!") Case 5, 6,7 ' Score is either 5, 6, or 7. lblMsgbox ("Nearly there!") Case Is > 7 And Number < 11 ' Score is 8 ,9, or 10 lblMsgbox ("Getting close! ") Case Else ' Score is one of the other values. lblMsgbox (“Number is to large! ") End Select 2/21/2019 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) 2/21/2019 Prepared By: Deborah Becker

Prepared By: Deborah Becker 2/21/2019 Prepared By: Deborah Becker

Prepared By: Deborah Becker For Next Block For index = lower limit to upper limit Coding statements This could be code to print the records in a file. Print one record from a file May contain condition blocks Next index 2/21/2019 Prepared By: Deborah Becker

Action of For...Next structure Counter<=ending value Counter>ending value For counter = start To end statement1 statement2 . . . For counter = start To end statement1 statement2 . . . Increment counter Next counter Next counter statement The Index can also be called the counter. 2/21/2019 Prepared By: Deborah Becker

Iterations of a For...Next loop Private Sub cmdForNextTest_Click() Dim X As Integer ‘ define the index For X = 1 To 3 ‘ 1 = lower limit picDisplay.Print X, ‘ 3 = upper limit Next X End Sub What will be the value of X when the loop terminates? What will be inside the picture box? 2/21/2019 Prepared By: Deborah Becker

Any Questions about the homework problem? Problem Check Any Questions about the homework problem? 2/21/2019 Prepared By: Deborah Becker