BOOLEAN OPERATIONS AND CONDITIONALS CHAPTER 20 1.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Logic & program control part 2: Simple selection structures.
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
(c) 2003 E.S.Boese1 Conditionals Chapter 10 - Student.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
If Statements Sections 1.25, Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.
CS 117 Spring 2002 Decision Making Hanly Chapter 3 Friedman-Koffman Chapter 4.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Chapter 4 Making Decisions
C++ for Engineers and Scientists Third Edition
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
CIS162AD - C# Decision Statements 04_decisions.ppt.
Decision Structures and Boolean Logic
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
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.
CIS162AD - C# If Statements 04_decisions.ppt. CIS162AD2 Overview of Topic  Pseudocode  Flow Control Structures  Flowcharting  If, If-Else  Nested.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Flow of Control Part 1: Selection
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
1 2. Program Construction in Java. 2.4 Selection (decisions)
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
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.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
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.
Chapter Making Decisions 4. Relational Operators 4.1.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 4 – Logical Operators & Branching.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
IT CS 200: C ONDITION Lect. Napat Amphaiphan. T HE ABILITY TO CONTROL THE FLOW OF YOUR PROGRAM, LETTING IT MAKE DECISIONS ON WHAT CODE TO EXECUTE 2.
Control statements Mostafa Abdallah
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
Control Statements: Part1  if, if…else, switch 1.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Rational Expressions relational operators logical operators order of precedence.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Selections Java.
Chapter 4: Making Decisions.
EGR 2261 Unit 4 Control Structures I: Selection
Chapter 4: Making Decisions.
Expressions and Control Flow in JavaScript
Control Structures – Selection
Conditionals & Boolean Expressions
Chapter 4 Selection.
Summary Two basic concepts: variables and assignments Basic types:
Chapter 4: Control Structures I (Selection)
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Chapter 2 Programming Basics.
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Presentation transcript:

BOOLEAN OPERATIONS AND CONDITIONALS CHAPTER 20 1

Topics  Boolean Operations –Shorting vs. Non-Shorting –Combination of Boolean Operations Order of Operations  Comparison Operators  Conditional Statements –if –if…else if…else –switch 2

Boolean Operations  Operations that combine and compare bools –!The NOT Operator –&&The AND Operator –||The OR Operator 3

Boolean Operations  !The NOT Operator –Pronounced either "not" or "bang" –Reverses value of the bool print( !true ); // Outputs: false print( !false ); // Outputs: true print( !(!true) ); // Outputs: true (the double negative of true) –Also called the "logical negation operator" This differentiates it from ~, the bitwise not operator 4

Boolean Operations  &&The AND Operator –Returns true only if both operands are true print( false && false ); // false print( false && true ); // false print( true && false ); // false print( true && true ); // true 5

Boolean Operations  ||The OR Operator –Returns true if either operand is true print( false && false ); // false print( false && true ); // true print( true && false ); // true print( true && true ); // true –| (the pipe) is Shift-Backslash Just above the return or enter key on a US keyboard 6

Boolean Operations  Shorting vs. Non-Shorting Boolean Operators –&& and || are shorting operators If the first operand of && is false, the second is not evaluated If the first operand of || is true, the second is not evaluated –& and | are non-shorting operators Both operands are evaluated regardless of value –& and | are also bitwise operators & and | compare each bit of the values passed into them Bitwise operators will be used much later when dealing with Unity layers and collisions 7

Boolean Operations  Combining Boolean Operations –Can combine several on a single line bool tf = true || false && true; –Must follow order of operations !NOT &Non-Shorting AND / Bitwise AND |Non-Shorting OR / Bitwise OR &&AND ||OR –The line above would be interpreted as: bool tf = true || (false && true);// true –It's best to always use parentheses to enforce the order in which you want the evaluation to take place! 8

Comparison Operators  Allow the comparison of two values  Return a bool (either true or false) ==Is Equal To !=Not Equal To >Greater Than <Less Than >=Greater Than or Equal To <=Less Than or Equal To 9

COMPARISON BY VALUE OR REFERENCE  Simple variables are compared by value –bool, int, float, char, string, Vector3, Color, Quaternion  More complex variables are compared by reference –When variables are compared by reference, the comparison is not of their internal values but of whether they point to the same location in memory –GameObject, Material, Renderer, HelloWorld (and other C# classes you write) 1 GameObject go0 = Instantiate( boxPrefab ) as GameObject; 2 GameObject go1 = Instantiate( boxPrefab ) as GameObject; 3 GameObject go2 = go0; 4 print( go0 == go1 ); // Output: false 5 print( go0 == go2 ); // Output: true 10

Comparison Operators  ==Is Equal To –Returns true if the values or references compared are equivalent print( 10 == 10 ); // Outputs: True print( 20 == 10 ); // Outputs: False print( 1.23f == 3.14f ); // Outputs: False print( 1.23f == 1.23f ); // Outputs: True print( 3.14f == Mathf.PI ); // Outputs: False // Mathf.PI has more decimal places than 3.14f –Do NOT confuse == and = ==The comparison operator =The assignment operator 11

Comparison Operators  !=Not Equal To –Returns true if the values or references compared are NOT equivalent print( 10 != 10 ); // Outputs: False print( 20 != 10 ); // Outputs: True print( 1.23f != 3.14f ); // Outputs: True print( 1.23f != 1.23f ); // Outputs: False print( 3.14f != Mathf.PI ); // Outputs: True 12

Comparison Operators  >Greater Than –Returns true if the first operand is greater than the second print( 10 > 10 ); // Outputs: False print( 20 > 10 ); // Outputs: True print( 1.23f > 3.14f ); // Outputs: False print( 1.23f > 1.23f ); // Outputs: False print( 3.14f > 1.23f ); // Outputs: True  <Less Than –Returns true if the first operand is less than the second print( 10 < 10 ); // Outputs: True print( 20 < 10 ); // Outputs: False print( 1.23f < 3.14f ); // Outputs: True print( 1.23f < 1.23f ); // Outputs: True print( 3.14f < 1.23f ); // Outputs: False 13

Comparison Operators  >=Greater Than or Equal To –True if the 1 st operand is greater than or equal to the 2 nd print( 10 >= 10 ); // Outputs: True print( 20 >= 10 ); // Outputs: True print( 1.23f >= 3.14f ); // Outputs: False print( 1.23f >= 1.23f ); // Outputs: True print( 3.14f >= 1.23f ); // Outputs: True  <=Less Than or Equal To –True if the 1 st operand is less than or equal to the 2 nd print( 10 <= 10 ); // Outputs: True print( 20 <= 10 ); // Outputs: False print( 1.23f <= 3.14f ); // Outputs: True print( 1.23f <= 1.23f ); // Outputs: True print( 3.14f <= 1.23f ); // Outputs: False 14

Conditional Statements  Control Flow Within Your Programs if if / else switch  Can be combined with Boolean operations  Make use of braces { } 15

Conditional Statements  ifPerforms code within braces if the argument within parentheses is true if (true) { print( "This line will print." ); } if (false) { print( "This line will NOT print." ); } // The output of this code will be: // This line will print.  All the code within the braces of the if statement executes 16

Conditional Statements  Combining if statements with boolean operations bool night = true; bool fullMoon = false; if (night) { print( "It's night." ); } if (!fullMoon) { print( "The moon is not full." ); } if (night && fullMoon) { print( "Beware werewolves!!!" ); } if (night && !fullMoon) { print( "No werewolves tonight. (Whew!)" ); } // The output of this code will be: // It's night. // The moon is not full. // No werewolves tonight. (Whew!) 17

Conditional Statements  Combining if statements with comparison operators if (10 == 10 ) { print( "10 is equal to 10." ); } if ( 10 > 20 ) { print( "10 is greater than 20." ); } if ( 1.23f = 1.23f ) { print( "1.23 is greater than or equal to 1.23." ); } if ( 3.14f != Mathf.PI ) { print( "3.14 is not equal to "+Mathf.PI+"." ); // + can be used to concatenate strings with other data types. // When this happens, the other data is converted to a string. }  Don't accidentally use = in an if statement!!! 18

Conditional Statements  if / else –Performs one action if true, and another if false bool night = false; if (night) { print( "It's night." ); } else { print( "What are you worried about?" ); } // The output of this code will be: // What are you worried about? 19

Conditional Statements  if / else if / else –Possible to chain several else if clauses bool night = true; bool fullMoon = true; if (!night) { // Condition 1 (false) print("It’s daytime. What are you worried about?"); } else if (fullMoon) { // Condition 2 (true) print( "Beware werewolves!!!" ); } else { // Condition 3 (not checked) print( "It's night, but the moon is not full." ); } // The output of this code will be: // Beware werewolves!!! 20

Conditional Statements  Nested if statements bool night = true; bool fullMoon = false; if (!night) { print( "It’s daytime. Why are you worried about?" ); } else { if (fullMoon) { print( "Beware werewolves!!!" ); } else { print( "It's night, but the moon isn't full." ); } } // The output of this code will be: // It's night, but the moon isn't full. 21

Conditional Statements  switchAlternative to several if statements –Can only compare for equality –Can only compare against a single variable against literals int num = 3; switch (num) { // The variable in parentheses is being compared case (0): // Each case is a literal that is compared against num print( "The number is zero." ); break; // Each case must end with a break statement. case (1): print( "The number is one." ); break; case (2): print( "The number is two." ); break; default: // If none of the other cases are true, default will happen print( "The number is more than a couple." ); break; } // The switch statement ends with a closing brace. // The output of this code is: The number is more than a couple. 22

Conditional Statements  Switch can "fall through" to other cases int num = 3; switch (num) { case (0): print( "The number is zero." ); break; case (1): print( "The number is one." ); break; case (2): print( "The number is a couple." ); break; case (3): // case (3) falls through to case (4) case (4): // case (4) falls through to case (5) case (5): print( "The number is a few." ); break; default: print( "The number is more than a few." ); break; } // The output of this code is: The number is a few. 23

Chapter 20 – Summary  Boolean Operations: ! && || & |  Learned about "shorting operations"  Boolean operations can be combined  Comparison Operators: == != > = <=  Conditional Statements: if if…else switch –if and switch statements can be combined in complex ways  Next Chapter: Loops in C# code! 24