Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case 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

Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:
Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks.
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.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Chapter 5 - VB.Net by Schneider
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
Chapter 5 - Visual Basic Schneider1 Chapter 5 Decisions.
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
Intro to Discrete Structures
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
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.
BY: MISS FARAH ADIBAH ADNAN IMK. CHAPTER OUTLINE: PART III 1.3 ELEMENTARY LOGIC INTRODUCTION PROPOSITION COMPOUND STATEMENTS LOGICAL.
Computer Science Selection Structures.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
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 - VB 2005 by Schneider1 Chapter 5 – Decisions 5.1 Relational and Logical Operators 5.2 If Blocks.
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
VBScript Language. What is VBScript Based on the Visual Basic family of languages Supports object oriented features Interpreted Loosely Typed Implicitly.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Conditional Expression One of the most useful tools for processing information in an event procedure is a conditional expression. A conditional expression.
How do I show that two compound propositions are logically equivalent?
Chapter 51 Decisions Relational and Logical Operators If Blocks Select Case Blocks.
1 SELECTION using IF and IF-ELSE Chapter 4. 2 Agenda Background  One Way selection (if) Two Way selection (if-else) Compound Statements Nested if-else.
Chapter Making Decisions 4. Relational Operators 4.1.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals.
Simplifying Boolean Expressions. Boolean Operators (T/F) xyx AND y FFF FTF TFF TTT xyx OR y FFF FTT TFT TTT xyx XOR y FFF FTT TFT TTF xNOT x FT TF.
Review for Final (Part 2) School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 15, Friday 5/2/2003)
1 CS 177 Week 5 Recitation Booleans and Control Flow.
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
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.
 Type Called bool  Bool has only two possible values: True and False.
Chapter 4: Decisions and Conditions
Chapter 3 Selection Statements
Chapter 4: Decisions and Conditions
Chapter 4 - VB 2008 by Schneider
EGR 2261 Unit 4 Control Structures I: Selection
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4 – Decisions 4.1 Relational and Logical Operators
Topics The if Statement The if-else Statement Comparing Strings
VB Decisions, Conditions & If
Chapter 5 - Visual Basic Schneider
Visual Basic – Decision Statements
Chapter 4: Control Structures I (Selection)
VB Decisions & Conditions
Control Structure Chapter 3.
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Chapter 5 Decisions.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Chapter 3: Selection Structures: Making Decisions
The Selection Structure
Control Structure.
Presentation transcript:

Chapter 5 Decisions

Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks

Condition A condition is a Boolean expression that can be either true or false Conditions can be formed by using the six Relational operators and the three Logical operators

Relational Operators

Examples 1<=1True 1 < 1False 2 < 5True 3 <> 3False 0 < 3.5 True 5 <= 3False

Examples Determine whether each of the following conditions is true or false assume that: a = 4, b = 3, c = “hello”, d = “bye”  (a+b) < 2 * a True  (Len(c) – b) = (a / 2)True

Computers use a special coding system to compare character strings called ANSI With ANSI each character has a number associated with it, this number is called ANSI value of the character. The ANSI values of characters are given in following table. (Appendix A) You do not need to memorize the table Just know the numbers, small and capital letters A= 65 a=97 sp=32 0=48 Using Relational Operators on Strings

8 Punctuation < Capitals letters < Small letters

Using Relational Operators on Strings The string str1 is said to be less than the string str2 if str1 precedes str2 alphabetically when using the ANSI (or ASCII) table Two strings are compared working from left to right, character by character, to determine which one should precede the other.

Example of Comparing Character Strings “Hope” < “Hopeful” “Chase” < “Chaz”True “ Cat” < “Cat”True “Pay” < “Pay “True “Jones” <> “James”True

Example… Determine whether the following condition is true or false (assume that c = “hello” and d = “bye”)  c < (“good” & d) False

Logical Operators The result of a logical operator is also True or False The three Logical Operators are: Not And Or

Not Not: Negates a single expression Example: Suppose answ = “Y” Not (answ = “y”) is True XNot X FT TF

And Takes two expressions, returns True only if both expressions are evaluated as being true Example: Suppose answ = “Y” (answ = “Y”) And (answ = “y”) is False XYX And Y FFF FTF TFF TTT

Or Takes two expressions, returns true if either one of the expressions is evaluated as being true. Example: Suppose answ = “Y” (answ = “Y”) Or (answ = “y”) is True XYX Or Y FFF FTT TFT TTT

Chapter 5 - Visual Basic Schneider16 Note: n Not ( X And Y ) = Not X OR Not Y n Not ( X Or Y ) = Not X And Not Y

Precedence First, all Arithmetic operations are carried out Then all expressions involving >, <, and = are evaluated to true or false The logical operations are next applied in the order Not, then And, and Finally Or.

Examples …

Evaluate the following to True or False Assume a=4, b=3 Print a*3-2=5-b False Print a=b+3<=a+b True Print not (a<2) or b<4 = a+b True Print Not(a =b+2) False Be careful about the priorities

Notes A Condition such as 2<n<5 (not a syntax error) but should never be used, because visual basic will not evaluate it as intended. The correct condition is (2<n) And (n<5) The following conditions are equivalent:  a<>b Not (a=b)  a>b Not (a<=b)  Not (n =m)

Types of Decision Structures If Block Statement Single alternative: If...Then Compound alternative: If Then...Else Select Case Statement

Single Alternative Decision An action is taken if the condition is true, otherwise the control goes to the next statement. Syntax: If condition Then action End If If condition is true, action is executed. Otherwise, action is skipped

Example

Compound Alternative Decision Syntax If condition Then action1 Else action 2 End If

Example

If cond1 Then If cond2 Then action(s) End If If cond1 And cond2 Then action(s) End If This is easier to understand A confusing If Block

Compound Alternative Decision Syntax If condition1 Then action1 ElseIf condition2 Then action 2 ElseIf condittion3 Then action3 Else action4 End If This block statement searches for the first True condition, carries out its action, and then skips to the statement following end if. If none of the conditions is true, then else’s action is carried out. In general, an IF block can contain any number of ElseIf clauses.

Example

Single Line If Statement Syntax If condition Then action There is no End If If the condition is true, the action will be executed If the condition is false, the (else) action will be taken

Example

The Select Case Block Similar to If statement Used instead of compound If statements Action is selected from a list of alternatives Avoids confusion of deeply nested If blocks

Select Case Block (Syntax) Select Case selector Case value-list-1 action1 Case value-list-2 action2 ….. Case Else action of last resort End Select

Example

what is the output of the following code a = 3 b = 4 Select Case a = b Case 3 Print "A" Case 4 Print "B" Case 1 Print "C" Case 0 Print "D" End Select Answer: D

what is the output of the following code a = "hello" Select Case a Case "apple" To "orange" Print "A" Case "Hello" Print "B" Case Is <= "z" Print "C" Case "hello" Print "D" End Select Answer: A

References Chapter 5 - Visual Basic Schneider