Lesson 6: Boolean and If Statements Computer Science 1 Mr. Bernstein.

Slides:



Advertisements
Similar presentations
What type of data can a variable hold?
Advertisements

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.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 2 Flow of Control. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 2-2 Learning Objectives Boolean Expressions Building, Evaluating.
Chapter 4 - Control Statements
Chapter 4 Decision Making Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E. Reingold.
INTRODUCTION LOGICAL OPERATIONS TRUTH TABLE AND RULES.
Programming for GCSE Topic 3.1: If Statements in Python T eaching L ondon C omputing William Marsh School of Electronic Engineering and Computer Science.
Computer Science 101 While Statement. Iteration: The While-Statement The syntax for the While- Statement is while : The syntax for the While- Statement.
Types of selection structures
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
Top-Down Design CSC 161: The Art of Programming Prof. Henry Kautz 9/16/2009.
Conditional Statements Introduction to Computing Science and Programming I.
Computer Science A 3: 10/2. Logical Expressions Expressions that has a value which is either true or false. Logical expressions are essential in a program.
The If/Else Statement, Boolean Flags, and Menus Page 180
Administrative MUST GO TO CORRECT LAB SECTION! Homework due 11:59pm on Tuesday. 25 points off if late (up to 24 hours) Cannot submit after 11:59pm on Wednesday.
Geography 465 Assignments, Conditionals, and Loops.
Chapter 9 IF Statement Bernard Chen. If Statement The main statement used for selecting from alternative actions based on test results It’s the primary.
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Python Control Flow statements There are three control flow statements in Python - if, for and while.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Xin Liu Feb 4, * Use midterm questions for previous 231/217 midterms for practices * ams
Chapter 3: Branching and Program Flow CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
Python Conditionals chapter 5
Decision Structures, String Comparison, Nested Structures
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Logical Operators, Boolean Variables, Random Numbers This template was just too good to let go in one day!
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 3 Decision Trees Conditionals.
The If Statement There are no switch statements in Python. You need to use just if statements. There are no switch statements in Python. You need to use.
If statement.  It is made up of three main components:  The keyword itself,  an expression that is tested for its truth value,  and a code suite to.
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
Decisions: Conditional Statements (informally called If Statements) IST 256 Application Programming for Information Systems.
PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances.
Input, Output and Variables GCSE Computer Science – Python.
 Type Called bool  Bool has only two possible values: True and False.
Computer Science 1000 LOGO II. Boolean Expressions like Excel and Scratch, LOGO supports three Boolean operators less than (
Programming Simple Programming Variables.
Control Structures I Chapter 3
Introduction to Python
Making Choices with if Statements
IF statements.
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
Logical Operators and While Loops
JavaScript conditional
Introduction to Programming
And now for something completely different . . .
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Multiple Selections (ELIF Statements)
Chapter (3) - Looping Questions.
CSE 1020:Control Structure
Introduction to Programming
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
Conditional Logic Presentation Name Course Name
Types, Truth, and Expressions (Part 2)
Logical Operators and While Loops
Programming Concepts and Database
Understanding Conditions
Python Basics with Jupyter Notebook
Nate Brunelle Today: Conditional Decision Statements
Truth tables Mrs. Palmer.
Types, Truth, and Expressions
Introduction to Programming
Conditionals.
Types, Truth, and Expressions (Part 2)
Presentation transcript:

Lesson 6: Boolean and If Statements Computer Science 1 Mr. Bernstein

BOOlean Boolean are statements that yield a truth or false value. Works with Strings and Integers

BOOlean Expressions Examples: a == b (== not initiating variables) 2 == 3 a > b a < b 10 >= 10 5 != 2

True or False True or True = True or False = False or True = False or False =

True or False True and True = True and False = False and True = False and False =

NOT! not False = not (True or False) = not (False and False) = not (True or True) =

DeMorgans Law not (P and Q) = not P or not Q For example: not (True and False) = not True or not False

If Conditional Statements if RELATIONALEXPRESSION: statement Lowercase if Colon after the RE Statements that follow must be indented

If Example: if rg3 == injured: print(RG3 is out for the season!) if rg3 != quarterback: print(RG3 is not the quarterback!)

IF Examples: rgcondition = good condition qb = rg3 if rgcondition == injured: print(RG3 is not playing next Sunday.) if rgcondition != injured and qb == rg3: print(RG3 is playing this Sunday!)

All together! Write a program that prompts the user how many hours they work. (input) If they work 40 hours or less, they make 8 dollars an hour. (if statement) If they work more than 40 hours, each hour after they make 10 dollars an hour. (if statement) Determine how much a person makes if they work: 21 hours, 40 hours, 48 hours

Else and elif Statement No need for multiple if statements! if ( apple > 10): print(I has more apples!) elif (apple <= 10): print(I has less apples :-( ) else: print(I dont know how many I have!)

Example in Class Write me a program that tells me what is for lunch when the user inputs a day of the week. (prompt the user) Monday – Sloppy Joes Wednesday – Chicken Patties Friday - Salads Tues/Thurs – Pizza (challenge) What if someone inputs Sat/Sun? What if they dont input a day of the week?

Summary Boolean – True or False If statement –RelationalExpression: (colon) statement If, Elif, and Else – Conditions that arent easily put into an if statement