Bools and simple if statements

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 2 Flow of Control. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 2-2 Learning Objectives Boolean Expressions Building, Evaluating.
3. S/E with Control Structures 3.1 Relational Operators and Expressions 3.2 If and if-else Statements 3.3 The Type Double 3.4 Program Design with the While.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
Visual C++ Programming: Concepts and Projects
Geography 465 Assignments, Conditionals, and Loops.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
Data Types. Every program must deal with data The data is usually described as a certain type This type determines what you can do with the data and how.
Identifiers and Assignment Statements. Data structures In any programming language you need to refer to data The simplest way is with the actual data.
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
Computer Science 101 The Boolean System. George Boole British mathematician ( ) Boolean algebra –Logic –Set theory –Circuits –Conditions in if.
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.
Computer Science 210 Computer Organization Introduction to Boolean Algebra.
Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical.
Decisions in Python Bools and simple if statements.
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Relational and Boolean Operators CSIS 1595: Fundamentals of Programming and Problem Solving 1.
CONDITIONALS. Boolean values Boolean value is either true or false It is name after the British mathemetician, George Boole who first formulated Boolean.
Control statements Mostafa Abdallah
OPERATORS Chapter2:part2. Operators Operators are special symbols used for: mathematical functions assignment statements logical comparisons Examples.
Conditional statements and boolean expressions Arithmetic, relational and logical operators.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Types, Truth, and Expressions Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Types, Truth, and Expressions Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Random Functions Selection Structure Comparison Operators Logical Operator
if ( condition ) statement; if is a Java reserved word The condition must be a boolean expression. It must evaluate to either true or false.
Control Structures I Chapter 3
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Based on slides by Shawn.
Data Types and Expressions
Computer Science 210 Computer Organization
Data Types and Expressions
Basics programming concepts-2
Selection (also known as Branching) Jumail Bin Taliba by
Sequence, Selection, Iteration The IF Statement
Chapter 4: Making Decisions.
Introduction to Python and programming
A mechanism for deciding whether an action should be taken
Data Types, Identifiers, and Expressions
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Multiple variables can be created in one declaration
Topics The if Statement The if-else Statement Comparing Strings
Introduction To Robot Decision Making
JavaScript conditional
Topics The if Statement The if-else Statement Comparing Strings
Expression Review what is the result and type of these expressions?
Introduction to Python and programming
And now for something completely different . . .
Introduction to Python and programming
Computer Science 210 Computer Organization
C++ Data Types Data Type
Introduction to Python and programming
Conditional Logic Presentation Name Course Name
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Expressions.
Chapter 2: Java Fundamentals
SSEA Computer Science: Track A
JavaScript conditional
ECS15 boolean.
Introduction to Python and programming
Conditionals.
Types, Truth, and Expressions
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Bools and simple if statements Decisions in Python Bools and simple if statements

Computer logic is Boolean George Boole, 19th cent. English mathematician, logician Had the idea to do logic with two values True and False Computer logic is based on exactly that Making decisions in a computer is based on two-valued logic, results are either True or False Python has two predefined constants (literals): True and False (note, no quotes) Several operators will give Boolean results

Relational Operators Familiar from algebra: >, <, >=, <=, ==, != Relational operators give the “relationship” between two things They can compare many different types, but be careful to compare things of the same type, ints and floats are ok, ints and strings are not They compare the two operands and give True or False as a result Comparing numbers is fairly routine Be careful about comparing floats to floats for exact equality The way floats are stored may cause some numbers that you think should be equal NOT to be equal Precedence of all of these operators is lower than the arithmetic operators and all six of them have the same precedence

Where to use them? You can use them to generate Bools and store the results X_larger = x > y # stores True or False in X_larger the_same = x == y # stores True or False in the_same Most often use the results in an if statement

if statement syntax statement starts with the word if immediately followed by some expression which has a Bool value then a colon next will be at least one statement indented more than the if statement, usually several statements

if statement semantics When an if statement is encountered in execution First the expression that gives the Boolean value will be evaluated If the result is True, the statements which are indented after the if will all be executed If the result of the expression is False, the statements which are indented will be skipped In either case execution continues at the next statement after the if statement