Chapter 4 – Decisions 4.1 Relational and Logical Operators

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

Chapter 4 - Control Statements
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.
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.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
Chapter 5 - VB.Net by Schneider
CSC110 Fall Chapter 5: Decision Visual Basic.NET.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
C++ for Engineers and Scientists Third Edition
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Dani Vainstein1 VBScript Session 9. Dani Vainstein2 What we learn last session? VBScript coding conventions. Code convention usage for constants, variables,
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
2440: 211 Interactive Web Programming Expressions & Operators.
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.
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 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks.
CONTROL STRUCTURE The if, elseif, and else & switch Statements 1.
Chapter 5 - VB 2005 by Schneider1 Chapter 5 – Decisions 5.1 Relational and Logical Operators 5.2 If Blocks.
Numbers continued The Integer Data Type Multiple Declarations Parentheses Three Types of Errors.
Computer Science 1620 boolean. Types so far: Integer char, short, int, long Floating Point float, double, long double String sequence of chars.
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.
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.
Relational and Boolean Operators CSIS 1595: Fundamentals of Programming and Problem Solving 1.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
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.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
CHAPTER FOUR Performing Calculations and Manipulating Data: Expressions.
CSC 162 Visual Basic I Programming. String Functions LTrim( string ) –Removes leading spaces from the left side of string RTrim( string ) –Removes trailing.
Chapter 3 - VB 2008 by Schneider1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
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.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Chapter 4: Decisions and Conditions
Chapter 4: Decisions and Conditions
Chapter 4 - VB 2008 by Schneider
The switch Statement, and Introduction to Looping
Chapter 4: Making Decisions.
2.5 Another Java Application: Adding Integers
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4: Making Decisions.
Starter Question In your jotter write the pseudocode to take in two numbers, add them together then display the answer. Declare variables RECEIVE firstNumber.
Chapter 4 – Decisions 4.1 Relational and Logical Operators
Topics The if Statement The if-else Statement Comparing Strings
Conditions and Ifs BIS1523 – Lecture 8.
Chapter 8 JavaScript: Control Statements, Part 2
String Variable, Methods and Properties
VB Decisions, Conditions & If
String Variable, Methods and Properties
Visual Basic – Decision Statements
Selection Statements.
Chapter 4: Control Structures I (Selection)
Conditional Logic Presentation Name Course Name
JavaScript.
String Variable, Methods and Properties
Relational Operators.
Chapter 5 Decisions.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
String Variable, Methods and Properties
The Selection Structure
Lecture 9: JavaScript Syntax
Presentation transcript:

Chapter 4 – Decisions 4.1 Relational and Logical Operators 4.2 If Blocks (see other set of slides) 4.3 Select Case Blocks (see other set of slides) 4.4 Input via User Selection (see other set of slides)

4.1 Relational and Logical Operators ANSI Values Relational Operators Logical Operators Boolean Data Type Two Boolean-Valued Methods A Boolean-Valued Function

Condition A condition is an expression involving relational and/or logical operators The value of the condition is Boolean – that is, True or False

ANSI Character Set A numeric representation for every key on the keyboard and for other assorted characters.

ANSI Character Set (continued) A numeric representation for every key on the keyboard and for other assorted characters.

Chr Function For n between 0 and 255, Chr(n) is the string consisting of the character with ANSI value n. Examples: Chr(65) is A Chr(162) is ¢

Asc Function For a string str, Asc(str) is ANSI value of the first character of str. Examples: Asc("A") is 65 Asc("¢25") is 162

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.

Condition A condition is an expression involving relational and/or logical operators. Result of the condition is True or False.

Example When a = 3, b = 4 (a + b) < 2 * a 2 * 3 = 6 3 + 4 = 7 7 is NOT less than 6 and so the value of the expression is False

Another Example a = 4 b = 3 c = "hello" d = "bye" ( c.Length – b ) = ( a / 2 ) 5 – 3 = 2 4 / 2 = 2 True because 2 equals 2

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

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

Example 4.3 n = 4, answ = “Y” Are the following expressions true or false? Not (n < 6) (answ = "Y") Or (answ = "y") (answ = "Y") And (answ = "y") Not(answ = "y") Not (n < 6) = False (answ = "Y") Or (answ = "y") = True (answ = "Y") And (answ = "y") = False Not(answ = "y") = True

Boolean Expression An expression that evaluates to either True or False is said to have Boolean data type. Example: The statement txtBox.Text = CStr((2 + 3) < 6) displays True in the text box.

Boolean Variable A variable declared with a statement of the form Dim var As Boolean Has Boolean data type. It can assume just the two values True and False. Example: Dim boolVar As Boolean boolVar = 2 < 6 txtBox.Text = CStr(boolVar) displays True in the text box.

Syntax Error The following is NOT a valid way to test whether n falls between 2 and 5: 2 < n < 5

Correction to Syntax Error To test if n falls between 2 and 5 use: (2 < n ) And ( n < 5 ) A complete relational expression must be on either side of the logical operators And and Or.

Common Error in Boolean Expressions A common error is to replace the condition Not ( 2 < 3 ) with the condition ( 2 > 3 ). The correct replacement is ( 2 >= 3 ) because >= is the opposite of <, just as <= is the opposite of >.

Two Boolean-Valued Methods The expression strVar1.EndsWith(strVar2) is true if the value of the first variable ends with the value of the second variable The expression strVar1.StartsWith(strVar2) is true if the value of the first variable begins with the value of the second variable Note: String literals can be used instead of string variables

Examples After the following code is executed each text box will contain the word True. Dim firstName As String = "William" txtBox1.Text = firstName.EndsWith("am") txtBox2.Text = firstName.StartsWith("Will")

A Boolean-Valued Function The expression IsNumeric(strVar) is true if the value of strVar can be converted to a number with CInt or CDbl. Note: The string variable can be replaced with a string literal. Examples: IsNumeric("123") is true IsNumeric("$123") is true IsNumeric("3 - 2") is false