booleans hold a true/false value We take advantage of this by using them to decide which route our program will take. Examples: stinky holds the boolean.

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

Logic Gates.
© 2007 Lawrenceville Press Slide 1 Chapter 5 The if Statement  Conditional control structure, also called a decision structure  Executes a set of statements.
If Statements & Relational Operators Programming.
Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York University.
The Type boolean. Boolean Expressions and Boolean Variables  The type boolean is a primitive type  Variables of type boolean and Boolean expressions.
Flow control 1: if-statements (Liang 72-80) if(radius < 0) { System.out.println(“cannot get area: radius below zero”); } else { double area = radius *
1 Selection Structures. 2 Making Decisions Sample assignment statements to figure worker pay with possible overtime PayAmount = Hours * Rate PayAmount.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
In a not gate, if the input is on(1) the output is off (0) and vice versa.
- Meeting 5 – Making Decision By: Felix Valentin, MBA.
PO D basicadvanced 4b + 6 b= 5, c= 3, d=7 (10c ÷b) 2 + d 4(5) (10(3) ÷5) (30 ÷5) (6)
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
CPSC 171 Introduction to Computer Science Boolean Logic, Gates, & Circuits.
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
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
1 Conditional statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
1. We’ve learned that our programs are read by the compiler in order, from top to bottom, just as they are written The order of statement execution is.
Practice 1.2 Answers
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Chapter 5 - VB 2005 by Schneider1 Chapter 5 – Decisions 5.1 Relational and Logical Operators 5.2 If Blocks.
Lesson - 5. Introduction While programming, we usually need to decide the path of the program flow according to the parameters and conditions. Actually.
03 August 2004 NLP-AI Java Lecture No. 4 Operators & Decision Constructs Satish Dethe.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 If’s – The Basic Idea “Program” for hubby: take out garbage.
FG ABCDE Vocabulary Exponents & Powers1.5 Translating Verbal Phrases 1.3 Order of Operations1.6.
Lecture 4 Boolean Algebra. Logical Statements °A proposition that may or may not be true: Today is Monday Today is Sunday It is raining °Compound Statements.
More Python!. Lists, Variables with more than one value Variables can point to more than one value at a time. The simplest way to do this is with a List.
Laws of Boolean Algebra Commutative Law Associative Law Distributive Law Identity Law De Morgan's Theorem.
Designing Combinational Logic Circuits
Control statements Mostafa Abdallah
Operators.
Decision Statements, Short- Circuit Evaluation, Errors.
TestScore < 80 testScore * 2 >= < w / (h * h) x + y != 2 * (a + b) 2 * Math.PI * radius
CS 139 – Programming Fundamentals Lecture 08A. Relational/comparison Operators Relational Operator Meaning > is greater than < is less than >= is greater.
IST 210: PHP LOGIC IST 210: Organization of Data IST210 1.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
Logic Gates and Boolean Algebra Introduction to Logic II.
Circuits, Truth Tables & Boolean Algebra. Expressions Can describe circuits in terms of Boolean expression.
Digital Logic Design. Truth Table  Logic Circuit 1. Start with truth table 2. When your output is a 1, figure out the combination of inputs, ANDs, and.
IF STATEMENTS AND BOOLEAN EXPRESSIONS. BOOLEAN EXPRESSIONS Evaluate to a value of true or false Use relational or equivalence operators Boolean operators.
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
 Type Called bool  Bool has only two possible values: True and False.
Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators.
Computer Science 210 Computer Organization
Truth Tables and Equivalent Statements
OBJECT ORIENTED PROGRAMMING I LECTURE 8 GEORGE KOUTSOGIANNAKIS
Sequence, Selection, Iteration The IF Statement
Section 7.1 Logical Operators
Logic Gates.
Topics The if Statement The if-else Statement Comparing Strings
Simple Control Structures
Chapter 4: Decision Structures and Boolean Logic
Topics The if Statement The if-else Statement Comparing Strings
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Computer Science 210 Computer Organization
Chapter 4: Decision Structures and Boolean Logic
Logic Gates.
Selection Statements.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Lecture 5 Binary Operation Boolean Logic. Binary Operations Addition Subtraction Multiplication Division.
Relational Operators.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Truth tables Mrs. Palmer.
Evaluating Boolean expressions
Compound Conditionals
Chapter 4: Decision Structures and Boolean Logic
The boolean type and boolean operators
DIGITAL ELECTRONICS AND LOGIC GATES. ANALOG SIGNAL:- Analog signal is continuous time varying current or voltage signal.
Presentation transcript:

booleans hold a true/false value We take advantage of this by using them to decide which route our program will take. Examples: stinky holds the boolean value for whether or not Mr. Mayewsky is stinky. If stinky… then Mr. Mayewsky takes a shower. redLight holds the boolean value for whether or not the light is red If redLight… then stop car… otherwise (else) step on it to make the light.

if (boolean){ …code…//executes if boolean is true }else if (boolean2){ …code…//executes if boolean if false //and boolean2 is true }else{ …code…//executes if all booleans are false }

== equal to != not equal to > greater than >= greater than or equal to < less than <= less than or equal to **Only use these with primitive variables!!!!!!!!!!!!!!!!

if (time < 7){ mayewsky.sleep(); }else if (time < 16){ mayewsky.work(); }else{ mayewsky.playComputerGames(); }

! – the not operator which will negate the boolean value(true becomes false and vice versa) && – the and operator which will and two boolean values (both need to be true for the result to be true) || – the or operator which will or two boolean values (if either is true then the result is true) Order of Operation: ! then && then || Expressions in parenthesis are evaluated first

isSmart || !isTall && isFast The expression below will be evaluated in the following order: 1. isTall notted by the ! 2. !isTall anded with isFast by the && 3. isSmart is ored with !isTall && isFast by the || The expression could also be rewritten with parenthesis the following way without changing how it is evaluated: isSmart || ((!isTall) && isFast)

AB!AA && BA || B You can show the results of a boolean expression using a truth table such as the one below that lists all the possible inputs and outputs. 1’s represent true and 0’s represent false.

When given a complicated boolean expression such as the one below, you can break it down into components to more easily evaluate the results. (!A||B) && !(A&&C) || B ABC!A!A||BA&&C!(A&&C)(!A||B)&&!(A&&C)(!A||B)&&!(A&&C)||B

Many times you can simplify a boolean expression. There are several tactics that you can take such as truth tables. For example, the expression that is evaluated on the previous slide can be simplified down to !A||B

if (time < 7 || isWeekEnd && time < 10){ mayewsky.sleep(); }else if (time < 16 && !isWeekEnd){ mayewsky.work(); }else if(!isWeekEnd){ mayewsky.playComputerGames(); }else{ mayewsky.watchFootball();

You can put an if statement inside another if statement This can be used to better organize your code. It makes it both easier to write, read, and edit!

if (isWeekEnd)){ if (time < 10){ mayewsky.sleep(); }else{ mayewsky.watchFootball(); } }else{ if (time < 7){ mayewsky.sleep(); }else if (time < 5){ mayewsky.work(); }else{ mayewsky.playComputerGames(); }