IF STATEMENTS AND BOOLEAN EXPRESSIONS. BOOLEAN EXPRESSIONS Evaluate to a value of true or false Use relational or equivalence operators Boolean operators.

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 13 Control Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Control Structures Conditional.
Short circuit code for boolean expressions: Boolean expressions are typically used in the flow of control statements, such as if, while and for statements,
Making Decisions in Python Sec 9-10 Web Design. Objectives The student will: Understand how to make a decision in Python Understand the structure of an.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
Python Programming Language
CS0007: Introduction to Computer Programming
CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Chapter 6 Horstmann Programs that make decisions: the IF command.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
true (any other value but zero) false (zero) expression Statement 2
CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes.
Flow control 1: if-statements (Liang 72-80) if(radius < 0) { System.out.println(“cannot get area: radius below zero”); } else { double area = radius *
Running JavaScript Chapter 18.
Decisions – Chapter 3 IPC144 Week 2. IPC144 Introduction to Programming Using C Week 2 – Lesson 2 (Pages 12 to 18 in IPC144 Textbook)
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Lecture 2: Static Methods, if statements, homework uploader.
Decision Structures and Boolean Logic
Branching and Conditions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
CPS120: Introduction to Computer Science Decision Making in Programs.
Fluency with Information Technology INFO100 and CSE100 Katherine Deibel Katherine Deibel, Fluency in Information Technology1.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Conditionals & boolean operators
Agenda Exam #1 Review Modulus Conditionals Boolean Algebra Reading: Chapter Homework #5.
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Flow of Control Part 1: Selection
© 2000 Scott S Albert Selection Structures Structured Programming 256 Chapter 4.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
1 2. Program Construction in Java. 2.4 Selection (decisions)
Selection Relational Expressions A condition or logical expression is an expression that can only take the values true or false. A.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
Conditional Statements Consider: if the mouse location is contained in the rectangle, display message “success” Some programming constructs can choose.
Java Decision Making and booleans (Java: An Eventful Approach, Ch 4), Slides Credit: Bruce, Danyluk and Murtagh CS 120 Lecture October 2012.
Chapter 4 Conditional Statements. A Programmer's Lament I really hate this damned machine; I wish that they would sell it It never does quite what I want.
Chapter 5.  Conditionals  Booleans  Relational and Logical Operators  Switch statements.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Chapter 51 Decisions Relational and Logical Operators If Blocks Select Case Blocks.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 4 – Logical Operators & Branching.
In the last lesson we discussed about: Casting Precedence The “=“ used as an assignment operator Made a calculate average program.
Fluency with Information Technology INFO100 and CSE100 Katherine Deibel Katherine Deibel, Fluency in Information Technology1.
CPS120: Introduction to Computer Science Decision Making in Programs.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Lesson thirteen Conditional Statement "if- else" ©
CPS120: Introduction to Computer Science Decision Making in Programs.
Knowledge Base. Defining a Variable Dim statement Dim intXX As Integer Public in a Module Public dblNN As Double.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
USING CONDITIONAL CODE AMIR KHANZADA. Conditional Statement  Conditional statements are the set of commands used to perform different actions based on.
Computer Science Up Down Controls, Decisions and Random Numbers.
 Type Called bool  Bool has only two possible values: True and False.
Chapter 3 Control Statements
OBJECT ORIENTED PROGRAMMING I LECTURE 8 GEORGE KOUTSOGIANNAKIS
Sequence, Selection, Iteration The IF Statement
Chapter 4: Making Decisions.
Section 7.1 Logical Operators
Selections Java.
Simple Control Structures
Expressions and Control Flow in JavaScript
Computers & Programming Languages
Flow Control Statements
Selection Statements.
Relational Operators.
Conditionals.
Presentation transcript:

IF STATEMENTS AND BOOLEAN EXPRESSIONS

BOOLEAN EXPRESSIONS Evaluate to a value of true or false Use relational or equivalence operators Boolean operators like AND and OR

RELATIONAL OPERATORS <Less than >Greater than <=Less than or equal to >=Greater than or equal to ==Equals Not to be confused with the assignment operator = !=Not equal to

BOOLEAN OPERATORS ||Or as in if a equals b or b equals c if (a == b || b == c) If either comparison is true than the equation is true. If both comparisons are false than the whole equation is false &&And as in if a equal b and b equals c If (a == b && b == c) If either comparison is false than the equation is false. Both comparisons must be true for the equation to be true

SIMPLE IF STATEMENT if (coin == 0) MessageBox.Show("Heads"); If the value of coin is 0 then a message box is displayed. If coin has a value of anything else nothing is displayed.

ADDING THE ELSE OPTION if (coin == 0) MessageBox.Show("Heads"); else MessageBox.Show("Tails"); If coin holds a 0 then Heads is displayed otherwise Tails is displayed

CODE BLOCKS Enclosed in curly brackets { } Allow multi statements to be treated as a single block of code Recommended even for single statement “blocks” May be used with simple if statements or more complex if/else and if/else if/else statements

ADDING CODE BLOCKS If (coin == 0) { heads++; MessageBox.Show("Heads"); } else { tails++; MessageBox.Show("Tails"); }

ELSE IF if (coin == 0) { heads++; MessageBox.Show("Heads"); } else if (coin == 1) { tails++; MessageBox.Show("Tails"); } else MessageBox.Show("Whoops");

BOOLEAN EXPRESSION – OR OPERATOR if (coin == 1 || coin == 0) MessageBox.Show("Good roll"); If either comparison is true the message is displayed. Only if neither comparison is true is nothing displayed

BOOLEAN EXPRESSIONS – AND OPERATOR if (coin == 1 && last == coin) MessageBox.Show("Two tails in a row"); The message is displayed only if both comparisons are true If the first comparison is false the second comparison will not be tried This is called short circuiting

LET’S START WITH THE CODE HUNT PUZZLES Click on the Capture Code button to test the code