Decision Making in Code Logical Tests & Truth Logical Expressions

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 Structures I (Selection)
A8 – Control Structures if, if-else, switch Control of flow in Java Any sort of complex program must have some ability to control flow.
Control Structures Control structures are used to manage the order in which statements in computer programs will be executed Three different approaches.
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.
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures.
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.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
Chapter 6 Control Statements Continued
JavaScript, Third Edition
1 Selection Structures. 2 Making Decisions Sample assignment statements to figure worker pay with possible overtime PayAmount = Hours * Rate PayAmount.
Visual C++ Programming: Concepts and Projects
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
CIS162AD - C# Decision Statements 04_decisions.ppt.
Microsoft Visual Basic 2008: Reloaded Fourth Edition
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
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 5 – Decisions 5.1 Relational and Logical Operators 5.2 If Blocks 5.3 Select Case Blocks.
Selection Structure If... Then.. Else Case. Selection Structure Use to make a decision or comparison and then, based on the result of that decision or.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing.
Selection Boolean What is Boolean ? Boolean is a set with only two values : –true –false true and false are standard identifiers in Pascal, called Boolean.
VBScript Language. What is VBScript Based on the Visual Basic family of languages Supports object oriented features Interpreted Loosely Typed Implicitly.
© ABB University - 1 Revision C E x t e n d e d A u t o m a t i o n S y s t e m x A Chapter 11 Structured Text Course T314.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Introduction to branching.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Chapter 6 Control Statements Continued
An Object-Oriented Approach to Programming Logic and Design Chapter 5 Making Decisions.
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.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Random Functions Selection Structure Comparison Operators Logical Operator
1 Sections 3.1 – 3.2a Basic Syntax and Semantics Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.-Edited By Maysoon Al-Duwais1.
Java Programming Fifth Edition
Sequence, Selection, Iteration The IF Statement
Section 7.1 Logical Operators
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Chapter 4: Making Decisions.
Data Types, Identifiers, and Expressions
Control Structures: Part 2
The Selection Structure
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4: Making Decisions.
Expressions and Control Flow in JavaScript
Topics The if Statement The if-else Statement Comparing Strings
Data Types, Identifiers, and Expressions
The C++ IF Statement Part 2 Copyright © Curt Hill
Chapter 8 JavaScript: Control Statements, Part 2
Relational Operators Operator Meaning < Less than > Greater than
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Chapter 3: Introduction to Problem Solving and Control Statements
Objectives After studying this chapter, you should be able to:
Chapter 4: Decision Structures and Boolean Logic
Chapter 6 Control Statements: Part 2
Selection Control Structure
3. Decision Structures Rocky K. C. Chang 19 September 2018
VB Decisions & Conditions
Conditional Logic Presentation Name Course Name
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 5 Decisions.
Chapter 4: Decision Structures and Boolean Logic
Lecture 7 – Unit 1 – Chatbots Python – For loops + Robustness
Boolean Expressions September 1, 2019 ICS102: The course.
Decision Making Using the IF and EVALUATE Statements
Presentation transcript:

Decision Making in Code Logical Tests & Truth Logical Expressions ISM 3005 Course Introduction Topics Decision Making in Code Logical Tests & Truth Logical Expressions Logical Operators & Precedence Range Testing The Not Operator Boolean Properties “Home computers are being called upon to perform many new functions, including the consumption of homework formerly eaten by the dog” Doug Larson 2

Decision Making Overview Decision making in code is one of the three fundamental programming constructs Sequence Selection or Choice Iteration Allows program to execute designated code when certain conditions are met If dblTotal <= 5000 Then sglTax = dblTotal * .06 Else sglTax = (5000 * .06) + (sglTotal - 5000) * .05 End If What policy is implemented in this code?

Beauty is Truth, Truth Beauty (John Keats) Programming often depends on testing a condition to control program behavior Whether specific code will execute How many times a loop executes Condition testing is accomplished by evaluation whether an expression is True or False True and False are literal Boolean values Dim blnProgrammingIsEasy as Boolean blnProgrammingIsEasy = True Logical tests are performed using rules of logic and precedence

Logical Tests Logical tests always evaluate to a result of either True or False In the test If dblTotal <= 5000 Then the current value of the variable dblTotal is tested to see if it is less than or equal to the literal value 5000 The entire expression dblTotal <= 5000 will be replaced with True or False depending on the value of dblTotal If the expression is True the following code will execute

Logical Expressions & Evaluation Logical expressions are tests Made with logical operators Evaluate to True or False Logical operators < Less than <= Less than or equal to = Equal to > Greater than >= Greater than or equal to <> Not equal to Require two characters to write

Logical Expressions & Evaluation (cont.) Evaluating numeric expressions If intAge <= 100 Then If intAge <= Val(txtAge.Text) Then Either term in the test may be provided by a Literal • Variable Function result • Constant Boolean property • Calculation result Dissimilar numeric data types can be compared but the results may be unexpected Best to convert before comparing

Logical Expressions & Evaluation (cont.) Evaluating string data types String evaluations are made according to the ASCII codes of each value in the strings “abc” is less than “abc “ “ABC” is less than “abc” “abcd” is greater than “abc” UCase(“abc”) is equal to “ABC” “123” is less than “234” “45” is greater than “123” Space ???

Logical Expressions & Evaluation (cont.) Logical operators And works on multiple conditions and requires both tests to be true for the overall test to be true If intX >= 3 And intX <= 8 Then Or will have the overall test true if either test is true If intX < 3 Or intX > 8 Then Not reverses the test result to which it is applied If Not intX = 2 Then The line continuation character can be useful when constructing complex logical tests

Logical Expressions & Evaluation (cont.) Compound tests with the “And” operator If x >= 3 And x <= 8 Then First Second x Test Test Overall 2 False True False 3 True True True 10 True False False

Logical Expressions & Evaluation (cont.) Compound tests with the “Or” operator If x >= 3 Or x <= 8 Then First Second x Test Test Overall 2 False True True 3 True True True 10 True False True If x <= 3 Or x >= 8 Then 5 False False False

Logical Expressions & Evaluation (cont.) Precedence of Logical Operators Not And Or Left to right in case of ties x >= 0 And x < 3 Or x > 8 And x <= 10 __________ Or __________ __________

Logical Expressions & Evaluation (cont.) Parentheses can suppress natural evaluation order Natural evaluation sequence (x >= 0 And x < 3) Or x = 10 Changing the precedence of evaluation x >= 0 And (x < 3 Or x = 10) Use parentheses to visually group tests (x >= 0) Or (x <> y And x < 3) Or (x = 10) Use parentheses to group tests for the Not operator Not (x = y Or y < 5)

It is common to test a value against a range Value in a range Range Testing It is common to test a value against a range Value in a range intAge >= 18 And intAge <= 30 Value not in a range intAge < 18 And intAge > 30 Not (intAge >= 18 And intAge <= 30) Value in a complex range intAge >= 18 And intAge <= 30 Or intAge > 60 Pay careful attention to the inequality operators and end point values when testing ranges Avoid overlap of range segments

Range tests can be combined with other tests Range Testing (cont.) You cannot do this: 18 <= intAge <= 30 Test must be performed as two independent tests of intAge with an appropriate operator (And or Or) Range tests can be combined with other tests If intAge < 18 _ And sglTotHoursWorked > 20 _ And (Now.Month < 6 _ Or Now.Month > 8)

The Not Operator Not reverses the Boolean status of whatever immediately follows it Not True = False – Not False = True Not 4 <= 5 = Not True = False 4 <= 5 And Not 5 > 4 = True And Not True = True And False = False Parentheses can extend the scope of Not Not (4 < 5 Or 5 < 4) = Not (True Or False) = Not (True) = False Not (4 < 5 Or Not 5 < 4) = Not (True Or Not False) = Not (True Or True) = Not (True) = False

Logical tests must evaluate to True or False Boolean Properties Logical tests must evaluate to True or False Many properties are Boolean in that they have values of True or False The following tests are equivalent If chkIsValid.Checked = True Then If chkIsValid.Checked Then The same applies to Boolean variables and constants If blnIsValid = True Then If blnIsValid Then

Exercises Write out using the logical operators but English expressions for values the logic to determine if: You passed all of your classes last semester You passed any of your classes last semester You are ready to graduate this semester Evaluate the following: True And False OR False Or Not False (True And False) OR False Or Not False