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.

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

5.04 Apply Decision Making Structures
1.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
Programming with Microsoft Visual Basic th Edition
An Introduction to Programming with C++ Fifth Edition Chapter 5 The Selection Structure.
Decisions (Conditional Programming) Chapter 5 (Sec. 5.1 & 5.2)
VB.Net Introduction - 2. Counter Example: Keep track the number of times a user clicks a button Need to declare a variable: Dim Counter As Integer Need.
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.
I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for.
© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
Chapter Four The Selection Structure
CIS162AD - C# Decision Statements 04_decisions.ppt.
Microsoft Visual Basic 2008: Reloaded Fourth Edition
Chapter 4: The Selection Structure
Chapter 4 The If…Then Statement
Chapter 4: The Selection Process in Visual Basic.
Chapter 4: The Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
Chapter 4: The Selection Structure
Chapter 4 Controlling the Flow Adapted From: Starting Out with Visual Basic 2012 (Pearson)
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Selection Control Structures. Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses.
Flow of Control Part 1: Selection
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
CSE1222: Lecture 6The Ohio State University1. Common Mistakes with Conditions (1)  Consider the following code: int age(26); if (age = 18) { cout
22/11/ Selection If selection construct.
Chapter 51 Decisions Relational and Logical Operators If Blocks Select Case Blocks.
Chapter Four The Selection Structure Programming with Microsoft Visual Basic th Edition.
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.
Computer Programming TCP1224 Chapter 5 The Selection Structure.
COMPUTER PROGRAMMING I 5.04 Apply Decision Making Structures.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 1 Unit 4 Decisions Chapter 4 Making Decisions and Working.
31/01/ Selection If selection construct.
110 E-1 Variables, Constants and Calculations(2) Chapter 3: Operations on variables, scope of a variable, formatting data Doing Arithmetic.
An Object-Oriented Approach to Programming Logic and Design Chapter 5 Making Decisions.
Decisions with Select Case and Strings Chapter 4 Part 2.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 11 So Many Paths … So Little Time.
Chapter 10 So Many Paths … So Little Time (Multiple-Alternative Selection Structures) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Knowledge Base. Defining a Variable Dim statement Dim intXX As Integer Public in a Module Public dblNN As Double.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
An Introduction to Programming with C++1 The Selection Structure Tutorial 6.
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.
Random Functions Selection Structure Comparison Operators Logical Operator
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Chapter 4: Decisions and Conditions
Chapter 4: Decisions and Conditions
Chapter 4 The If…Then Statement
Chapter 4: Making Decisions.
The Selection Structure
Chapter 4: The Selection Structure
Chapter 4: Making Decisions.
An Introduction to Programming with C++ Fifth Edition
Making Decisions in a Program
Objectives After studying this chapter, you should be able to:
If selection construct
VB Decisions, Conditions & If
If selection construct
Microsoft Visual Basic 2005: Reloaded Second Edition
VB Decisions & Conditions
Conditional Logic Presentation Name Course Name
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Chapter 3: Selection Structures: Making Decisions
The Selection Structure
Chapter 5: The Selection Structure
Presentation transcript:

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 within our programs. (e.g. if the number of items purchased is greater than 10, apply a discount rate if not apply the usual rate) We will learn: The If…Then, Else, End If statement How to write a logical expression using logical operators (And, Or) and more... Check the user’s input

110 F-2 If statement (1) A conditional statement: In plain English (also called pseudocode): If the age is less than 18, display the message “can’t vote” Else display the message “can vote” As a flowchart display can vote display Can’t vote ? age < 18 False True

110 F-3 As a VB statement If intAge < 18 Then lblMessage.Text = "Can’t Vote" Else lblMessage.Text = "Can Vote" End If If statement (2)

110 F-4 VB If statement General Form: If ( condition ) Then statement(s) Else statement(s) End If optional To write the condition, we can use the following relational operators: > for greater than < for less than >= for greater than or equal to <= for less than or equal to <> for not equal to = for equal to compare numbers… and strings parentheses are optional but clearer

110 F-5 Conditions examples If (CInt(txtNumber.txt)>intMAX) Then If ( dblB^2 - 4*dblA*dblC > 0 ) Then If ( intAge <> 25) Then With numbers: With strings: Use of lexicographic order (  dictionary) All characters are ordered according to their Unicode encoding (see table p 150) the computer can compare strings "&123#" > "abc" "monkey"<"month" False because & < a True because k < t … < & < … < 5 < … < A < B < … < a < … < z < … < } < …

110 F-6 Logical operators How to write in VB a condition like: If 0 < intNumber < 20 ? use the logical operator And If ( intNumber>0 And intNumber<20) Then The other logical operators are Not and Or Recall the logical table: F F T T T F F F T T T F P, Q are conditions T and F stand for True and False Not PP And QP Or Q

110 F-7 Using And, Or, Not If (dblFinal > 90) And (dblMidterm > 90) Then lblGrade.Text = “A” Endif ( ) are optional, but clearer Be careful with the precedence order: Not Or And lower precedence higher precedence intA = 2 intB = 3 intC = 4 If Not intA>3 And intB<>3 Or Not intC = 6 Then lblDisplay.Text="the condition is true" Else lblDisplay.Text = "the condition is false" Endif What is displayed? the condition is true Do not hesitate to use parentheses!

110 F-8 Condition Truth Value(1) _When we write If (condition) Then VB evaluates the condition as a Boolean ( = True or False) e.g. If (dblTemperature>212) Then lblDisplay.Text = "The water is boiling" End If True or False _You may use shortcuts if you are testing a variable which is a Boolean: e.g. the Checked property of a radio button is either True or False If radRed.Checked = True Then same as If radRed.Checked Then

110 F-9 Condition Truth Value (2) _ If your condition is just a number Not a good programming practice! Forbidden with Option Strict On! If ( x ) Then False if x=0, True if x  0, Always use If (x=0) Then If (x<>0) Then

110 F-10 The numeric value of a condition What happens when VB converts a boolean to a number ? Warning: this example is illegal with Option Strict On! CInt(True) is -1 CInt(False)is 0 We can understand how VB reads the condition: 0 <= x <= 10 Evaluated first True or False To evaluate True (or False) <= 10, VB converts True to -1 and False to 0 Since -1 (or 0) is less than 10, the condition 0 <= x <= 10 is always True! True (always) Similarly, -10 <= x <= - 5 is always False

110 F-11 IsNumeric: a useful function _ to check the user’s input of a number: How do we check that an entry written in a text box is a number? e.g. 1.3 is OK but “one” is not! Instead use the VB function IsNumeric IsNumeric("1.3") is True IsNumeric("one") is False If IsNumeric(txtInput.Text) Then

110 F-12 An Example Goal Input the user’s age and check that it is a valid entry (e.g. 0 < age < 130) How to validate? if the age is not between 0 and 130 or is not a numeric entry, display a warning message and clear the text box. use a message box Exit Validate Enter your age text box

110 F-13 Nested Ifs Useful for cases with more than 2 choices: How to code this in VB? Example: print the percentage tax based on pay if  pay < % if  pay < % if  pay 31% if  pay < % if pay < %

110 F-14 If (dblPay>=0 And dblPay<15000) Then dblRate = 0 Endif If (dblPay >=15000 And dblPay <30000) Then dblRate = 0.18 Endif But we can do better! If (dblPay >=30000 And dblPay <50000) Then dblRate = 0.22 Endif If (dblPay >=50000 And dblPay <100000) Then dblRate = 0.28 Endif If (dblPay >=100000) Then dblRate = 0.31 Endif Simple Solution

110 F-15 If (dblPay < 15000) Then dblRate = 0.00 Else End If Better Cascaded ifs If (dblPay < 30000) Then dblRate = 0.18 Else End If If (dblPay < 50000) Then dblRate = 0.22 Else End If If (dblPay < ) Then dblRate = 0.28 Else dblRate = 0.31 End If

110 F-16 using ElseIf (clearer) If (dblPay < 15000) Then dblRate = 0.00 ElseIf (dblPay < 30000) Then dblRate = 0.18 ElseIf (dblPay < 50000) Then dblRate = 0.22 ElseIf (dblPay < ) Then dblRate = 0.28 Else dblRate = 0.31 End If order of the conditions is important. Conditions are successively inclusive Best Only one End If

110 F-17 using Select Case Select Case intAge Case Is < 13 lblAgeGroup.Text = “Child” Case 13 To 19 lblAgeGroup.Text = “Teenager” Case 20 To 30 lblAgeGroup.Text = “Young adult” Case 31 To 60 lblAgeGroup.Text = “Middle aged” Case Else lblAgeGroup.Text = “Senior” End Select Another approach Other possibilities: Testing for a set of values Case 1, 2, 5, 10 Works with strings as well Case “UW”, “Seattle Central”

110 F-18 More on Strings Function for strings (can be useful in conditionals) ToUpper converts all of the letters to uppercase ToLower converts all of the letters to lowercase e.g. txtString1.Text.ToUpper() Dim strName As String strName=strName.ToLower() A string is immutable (= can't change) strName.ToLower() doesn't change strName You must write: strName = strName.ToLower() & to concatenate strings: "Hello, " & "world!" Special characters (in the ControlChars class) e.g. ControlChars.NewLine