The Type boolean. Boolean Expressions and Boolean Variables  The type boolean is a primitive type  Variables of type boolean and Boolean expressions.

Slides:



Advertisements
Similar presentations
Introduction to Programming Java Lab 5: Boolean Operations 8 February JavaLab5 lecture slides.ppt Ping Brennan
Advertisements

Intro to CS – Honors I Control Flow: Branches GEORGIOS PORTOKALIDIS
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
Type Title Here for Tic-Tac-Toe Type names of students in group here.
If Statements & Relational Operators Programming.
Branching Loops exit(n) method Boolean data type and expressions
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Boolean Types & Compound Conditionals CSC 1401: Introduction to Programming with Java Lecture 4 – Part 3 Wanda M. Kunkle.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
1 Selection in C. 2 If / else if statement:  The else part of an if statement can be another if statement. if (condition) … else if (condition) … else.
ITM352 Loops Lecture #6. 1/28/03ITM352 Fall 2003 Class 5 – Control Flow 2 Announcements r Lab class dates have changed!!! m Check the “Weekly Schedule”
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
BOOLEAN LOGIC CSC 171 FALL 2004 LECTURE 7. ASSIGNMENT Review Quiz # 2 Start reading Chapter 5.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 3Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 3 l Branching l Loops l exit(n) method l Boolean data type.
Chapter 4: Basic C Operators
Computer Science 101 The Boolean System. George Boole British mathematician ( ) Boolean algebra –Logic –Set theory –Circuits –Conditions in if.
XOR and XNOR Logic Gates. XOR Function Output Y is TRUE if input A OR input B are TRUE Exclusively, else it is FALSE. Logic Symbol  Description  Truth.
Chapter 3 Making Decisions
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
COMP 110: Introduction to Programming Tyler Johnson Feb 2, 2009 MWF 11:00AM-12:15PM Sitterson 014.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Chapter 3 Edited by JJ Shepherd
1 Conditional statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
Flow of Control Module 3. Objectives Use Java branching statements Compare values of primitive types Compare objects such as strings Use the primitive.
UniMAP Sem1-07/08EKT120: Computer Programming1 Week2.
Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical.
Introduction To Scientific Programming Chapter 3.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
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 Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
Pemrograman Dasar Operators, Expressions & Statements PTIIK - UB 1.
Control statements Mostafa Abdallah
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC INTRO TO COMPUTING - PROGRAMMING If Statement.
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.
Computer Programming with Java Chapter 2 Primitive Types, Assignment, and Expressions.
Operators.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Lecturer: Dr. AJ Bieszczad Chapter 3 COMP 150: Introduction to Object-Oriented Programming 3-1 l Branching l Loops l exit(n) method l Boolean data type.
1 CS161 Introduction to Computer Science Topic #6.
Department of Electronic & Electrical Engineering Expressions operators operands precedence associativity types.
5.02B Decision Making Structure (part 2). Compound Boolean Expressions.
Chapter 31 Announcements Project 2 has been posted –Due Feb 1st at 10:00pm –Work ALONE! Help hours –Monday – Thursday, 7-10pm –LWSN B146 Quiz solutions.
Flow of Control Chapter 3. JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Chapter 3Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 3 l Branching l Loops l exit(n) method l Boolean data type.
Rational Expressions relational operators logical operators order of precedence.
 Type Called bool  Bool has only two possible values: True and False.
Chapter 3Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 3 l Branching l Loops l exit(n) method l Boolean data type.
Lecture 3 Selection Statements
COS 260 DAY 5 Tony Gauvin.
Relational Operator and Operations
Chapter 3 Selection Statements
Computer Science 210 Computer Organization
Relational Operators Operator Meaning < Less than > Greater than
Student #7 starts with Locker 7 and changes every seventh door
Computer Science 210 Computer Organization
Expressions.
Lecture 5 Binary Operation Boolean Logic. Binary Operations Addition Subtraction Multiplication Division.
Ms. Lindsey’s Kindergarten Class 11/1/16
Flow of Control Chapter 3.
Primitive Types and Expressions
Announcements/Reminders
Announcements Program 1 due noon Lab 1 due noon
Presentation transcript:

The Type boolean

Boolean Expressions and Boolean Variables  The type boolean is a primitive type  Variables of type boolean and Boolean expressions can have only values of either true or false.  Example: if (number > 0) System.out.println(“The number is positive.”); else System.out.println(“The number is negative or zero”);

Boolean Expressions and Boolean Variables (cont’d)  A Boolean variable can be given the value of a Boolean expression by using an assignment statement.  Example: int number = -5; boolean isPositive; isPositive = (number > 0);

Boolean Expressions and Boolean Variables (cont’d)  A value can also be assigned in the declaration statement. boolean isPositive = (number > 0);  The previous example can be rewritten as follows: if (isPositive) System.out.println(“The number is positive.”); else System.out.println(“The number is negative or zero”);

Naming Boolean Variables  When naming a Boolean variable choose a statement that will be true when the value of the Boolean expression is true.  Examples: boolean isPositive = (number > 0); boolean systemsAreOK = (temperature = 12000) && (cabinPressure > 30); boolean lightsOn = true;

Precedence Rules  First: the unary operators +, -, ++, --, and !  Second: the binary arithmetic operators *, /, %  Third: the binary arithmetic operators +, -  Fourth: the boolean operators, =  Fifth: the boolean operators ==, !=  Sixth: the boolean operator &  Seventh: the boolean operator ^  Eighth: the boolean operator |  Ninth: the boolean operator &&  Tenth: the boolean operator ||

Truth Table for the && (and) Boolean Operator Value of AValue of BResulting Value of A && B true false truefalse

Truth Table for the || (or) Boolean Operator Value of AValue of BResulting Value of A || B true falsetrue falsetrue false

Truth Table for the ^ ( exclusive or) Boolean Operator Value of AValue of BResulting Value of A ^ B true false truefalsetrue falsetrue false

Truth Table for the ! (not) Boolean Operator Value of AResulting Value of !A truefalse true

Input and Output of Boolean Values  The values true and false of the type boolean can be input and output the same way that values of the other primitive types  Example: boolean booleanVar = false; System.out.println(booleanVar); System.out.println(“Enter a boolean value:”); Scanner keyboard = new.Scanner(System.in); booleanVar = keyboard.nextBoolean(); System.out.println(“You entered “ + booleanVar);