Conditions, logical expressions, and selection. Introduction to control structures

Similar documents
Conditions, logical expressions, and selection. Introduction to control structures

Flow Charts. Visual Depiction of Logic Flow

Flow of Control. bool Data Type. Flow Of Control. Expressions. C++ Control Structures < <= > >= ==!= ! &&

Chapter 5. Conditions, Logical Expressions, and Selection Control Structures

Chapter 5 Topics. Chapter 5 Topics. Flow of Control. bool Data Type. Flow of Control. Chapter 5

Lesson #4. Logical Operators and Selection Statements. 4. Logical Operators and Selection Statements - Copyright Denis Hamelin - Ryerson University

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4.

Conditions and Logical Expressions. C Programming

C++ Programming: From Problem Analysis to Program Design, Third Edition

C++ Programming: From Problem Analysis to Program Design, Third Edition

Flow of programming execution control is sequential unless a control structure is used to change that there are 2 general types of

UEE1302(1102) F10: Introduction to Computers and Programming

Concepts Review. 2. A program is the implementation of an algorithm in a particular computer language, like C and C++.

Fundamentals of Programming Session 9

Chapter 4 - Notes Control Structures I (Selection)

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

C/C++ Programming for Engineers: Matlab Branches and Loops

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Control Structures: The IF statement!

Lecture Transcript While and Do While Statements in C++

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 4: Control Structures I (Selection)

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

Maltepe University Computer Engineering Department. Algorithms and Programming. Chapter 4: Conditionals - If statement - Switch statement

Programming for Engineers Iteration

Computers and FORTRAN Language Fortran 95/2003. Dr. Isaac Gang Tuesday March 1, 2011 Lecture 3 notes. Topics:

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

SNS COLLEGE OF ENGINEERING,

CS 106 Introduction to Computer Science I

Introduction to C/C++ Lecture 3 - Program Flow Control

Motivating Examples (1.1) Selections. Motivating Examples (1.2) Learning Outcomes. EECS1022: Programming for Mobile Computing Winter 2018

Computer Science & Engineering 150A Problem Solving Using Computers

CCHAPTER SELECTION STATEMENTS HAPTER 3. Objectives

Chapter 4. Flow of Control

Control Structures. Control Structures Conditional Statements COMPUTER PROGRAMMING. Electrical-Electronics Engineering Dept.

Lesson 04. Control Structures I : Decision Making. MIT 31043, VISUAL PROGRAMMING By: S. Sabraz Nawaz

Problem Solving and 'C' Programming

LECTURE 5 Control Structures Part 2

Algorithm. Building blocks of algorithm

In this chapter, you will:

Practice Exam #3, Math 100, Professor Wilson. MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Computational Physics - Fortran February 1997

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

Chapter 4: Programming with MATLAB

Problem Solving with Decisions. T.Fatin Alhila

Syntax of Eiffel: a Brief Overview

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

Commands, and Queries, and Features. Syntax of Eiffel: a Brief Overview. Escape Sequences. Naming Conventions

Introduction to Decision Structures. Boolean & If Statements. Different Types of Decisions. Boolean Logic. Relational Operators

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Previous Lecture (and Discussion): Branching (if, elseif, else, end) Relational operators (<, >=, ==, ~=,, etc.) Logical operators (&&,, ~)

Constants and Variables

Microsoft Visual Basic 2005: Reloaded

Test #2 October 8, 2015

Control Structures in Java if-else and switch

CHAPTER 9 FLOW OF CONTROL

Selections. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG

Theory of control structures

Week 7: Assignment Solutions

A B C ((NOT A) AND B) OR C

K44 Code lock. Technical manual


5. Selection: If and Switch Controls

Making Decisions In Python

Cardinality of Sets MAT231. Fall Transition to Higher Mathematics. MAT231 (Transition to Higher Math) Cardinality of Sets Fall / 15

CP FAQS Q-1) Define flowchart and explain Various symbols of flowchart Q-2) Explain basic structure of c language Documentation section :

5. Control Statements

Appendix C: HVAC PRO Modules

Week 2 Introduction to Computer and Algorithm (Part 2)

CS1150 Principles of Computer Science Boolean, Selection Statements

PLD Semester Exam Study Guide Dec. 2018

Problem with Scanning an Infix Expression

Math Week in Review #5. A proposition, or statement, is a declarative sentence that can be classified as either true or false, but not both.

Chapter Five: Functions

Dataflow Processing. A.R. Hurson Computer Science Department Missouri Science & Technology

Chapter 5 Conditional and Iterative Statements. Statement are the instructions given to the computer to perform any kind of action.

Chapter 4: Control Structures I

Java Coding 2. Decisions, decisions!

LESSON 3 CONTROL STRUCTURES

1 Introduction Java, the beginning Java Virtual Machine A First Program BlueJ Raspberry Pi...

Programming Logic and Design Sixth Edition

Python Programming: An Introduction to Computer Science

Islamic University of Gaza Computer Engineering Dept. C++ Programming. For Industrial And Electrical Engineering By Instructor: Ruba A.

Introduction to Computer Programming for Non-Majors

Control Structures in Java if-else and switch

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures

Loops. Repeat after me

LECTURE 2 An Introduction to Boolean Algebra

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

2 nd Week Lecture Notes

Programming: Java. Chapter Objectives. Control Structures. Chapter 4: Control Structures I. Program Design Including Data Structures

Cambridge International Examinations Cambridge International Advanced Subsidiary and Advanced Level. Published

Lab 09: Advanced SQL

Loop Unrolling. Source: for i := 1 to 100 by 1 A[i] := A[i] + B[i]; endfor

CS 106 Introduction to Computer Science I

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

Transcription:

Conditions, logical expressions, and selection Introduction to control structures 1

Flow of control In a program, statements execute in a particular order By default, statements are executed sequentially: One after another, from top to bottom One at a time Exactly once This sequential flow of control can be altered through the use of control structures 2

Control structures There are two general types of control structures: Selection (aka branching) structures: cause flow of control to take, or not take, a particular direction Repetition (aka iteration, looping) structures: cause a set of statements to execute several times Both of these type of structures depend on the evaluation of logical expressions 3

Flow charts for control structures Selection Repetition 4

Logical expressions Logical expressions evaluate to true or false Most logical expressions use relational and logical operators In App Inventor, as in most programming languages, the form of a simple logical expression is: Operand1 operator Operand2 The operands can be simple or compound expressions of any type The operator is a relational operator 5

Relational operators The relational operators in AI (found in the Math drawer) include: < : is less than <= : is less than or equal to = : equals > : is greater than >= : is greater than or equal to not= : does not equal 6

Logical expression examples Suppose you had the following declarations: x = 3, y = 7; Then the following expressions would have the values indicated: x > y // (false) y >= x // (true) x not= y // (true) (x > y) = true // (false) ((y >= x) = (x not= y)) // true 7

Logical operators Three operators in AI (found in the Logic drawer) can be used to form compound logical expressions They are: and - logical and or - logical or not logical not 8

Logical operators Logical and (&&) combines two expressions; if both sub-expressions are true, then the compound expression is true otherwise, the compound expression is false Logical or also combines two expressions; the compound expression is true if one or both subexpressions is true, false otherwise Logical not reverses the truth value of an expression; if the original expression was true, not makes it false, and vice versa 9

Truth table Graphical display of relationships between truth values of propositions Shows all possible values of propositions, or combinations of propositions Suppose p represents an expression; then the truth table for!p is as show below: p not p T F F T 10

What is the value? Given: age = 25; height = 70; EXPRESSION VALUE not (age < 10)? not (height > 60)? 11

Truth table for p and q Suppose p and q represent two logical sub-expressions; then the compound expression p and q has the following truth table: p q p and q T T T T F F F T F F F F 12

Truth table for p or q Suppose p and q represent two logical sub-expressions; then the compound expression p or q has the following truth table: p q p or q T T T T F T F T T F F F 13

age = 20; temperature = 102.0; issenior = (age >= 55) ; hasfever = (temperature > 98.6) ; EXPRESSION issenior and hasfever issenior or hasfever not issenior not hasfever VALUE false true true false 14

Logical equivalence Two different logical expressions that yield the same result are said to be logically equivalent Logical equivalence is not always obvious; two examples follow that illustrate this 15

Example 1 We are given the following two expressions: not((a=b) or (A=C)) (A not= B) and (A not= C) Are they equivalent? To find out, create truth tables 16

Truth Tables not((a=b) or (A=C)) (A not= B) and (A not= C) A B C A=B A = C or not 0 0 0 1 1 1 0 0 0 1 1 0 1 0 0 1 0 0 1 1 0 0 1 1 0 0 0 1 1 0 0 0 0 0 1 1 0 1 0 1 1 0 1 1 0 1 0 1 0 1 1 1 1 1 1 0 A B C A not= B A not= C and 0 0 0 0 0 0 0 0 1 0 1 0 0 1 0 1 0 0 0 1 1 1 1 1 1 0 0 1 1 1 1 0 1 1 0 0 1 1 0 0 1 0 1 1 1 0 0 0 17

Example 2 not (A and B) A B A and B not 0 0 0 1 0 1 0 1 1 0 0 1 1 1 1 0 A or B A B A or B 0 0 0 0 1 1 1 0 1 1 1 1 18

Short-Circuit Evaluation Many programming languages use short circuit evaluation of logical expressions This means logical expressions are evaluated left to right and evaluation stops as soon as the final truth value can be determined 19

Short-Circuit Example Given: age = 25; height = 70; EXPRESSION (age > 50) and (height > 60) false Evaluation can stop now because result of and is only true when both sides are true. It is already determined that the entire expression will be false. 20

More Short-Circuiting Given: age = 25; height = 70; EXPRESSION (height > 60) or (age > 40) true Evaluation can stop now because result of or is true if one side is true. It is already determined that the entire expression will be true. 21

What happens? Given: age = 25; weight = 145; EXPRESSION (weight < 180) and (age >= 20) true Must still be evaluated because truth value of entire expression is not yet known. Why? Result of and is only true if both sides are true. 22

What happens? Given: age = 25; height = 70; EXPRESSION! (height > 60) or (age > 50) true false Does this part need to be evaluated? 23

Write an expression for each taxrate is over 25% and income is less than $20000 temperature is less than or equal to 75 or humidity is less than 70% age is over 21 and age is less than 60 age is 21 or 22 24

Logical expressions & program control Relational expressions can be used to control the flow of logic in a program Depending on the truth value of an expression, a program can be made to perform one task or another (but not both) A control structure that fits this description is called a selection structure 25

Logical expressions & program control Relational expressions can be used to control the flow of logic in a program Depending on the truth value of an expression, a program can be made to perform one task or another (but not both) A control structure that fits this description is called a selection structure 26

Selection Blocks in AI Simple if block 27

Compound Selection in AI Nested structure 28

Compound Selection Example Suppose you need to design the software for a controller that monitors the temperature of a critical piece of equipment If the temperature falls within the normal range (55-80 F), no action is necessary But if the temperature falls below 55 or rises above 80, an alarm should be triggered: 10 degrees in either direction should sound the immediate action alert More than 10 degrees in either direction should trigger an automatic shutoff 29

Example continued Variable to test: temp Expressions: Normal range: temp >= 50 and temp <= 80 Action alert: (temp<50 and temp>40) or (temp>80 and temp<90) Automatic shutoff: temp <= 40 or temp >= 90 30

Flowchart (not a very good one!) 31