Boolean Types & Compound Conditionals CSC 1401: Introduction to Programming with Java Lecture 4 – Part 3 Wanda M. Kunkle.

Slides:



Advertisements
Similar presentations
Logic & program control part 2: Simple selection structures.
Advertisements

An Introduction to Programming with C++ Fifth Edition Chapter 5 The Selection Structure.
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
The Type boolean. Boolean Expressions and Boolean Variables  The type boolean is a primitive type  Variables of type boolean and Boolean expressions.
Boolean Expressions Conditional Statements & Expressions CSC 1401: Introduction to Programming with Java Lecture 4 – Part 2 Wanda M. Kunkle.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
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.
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.
Primitive Types CSE 115 Spring 2006 April 3 &
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.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
Adapted from Discrete Math
Chapter 4: Basic C Operators
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Computer Science 101 The Boolean System. George Boole British mathematician ( ) Boolean algebra –Logic –Set theory –Circuits –Conditions in if.
Computer Science Selection Structures.
3/6: Logical Operators questions about previous program modifications logical operators program of the day.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
1 Conditional statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
© 2006 Pearson Education 1 Obj: to use compound Boolean statements HW: p.184 True/False #1 – 6 (skip 3)  Do Now: 1.Test your “Charge Account Statement”
CSCI 1100/1202 January 28, The switch Statement The switch statement provides another means to decide which statement to execute next The switch.
Computer Science 210 Computer Organization Introduction to Boolean Algebra.
ITM 352 Flow-Control: if and switch. ITM © Port, KazmanFlow-Control - 2 What is "Flow of Control"? Flow of Control is the execution order of instructions.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
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.
Lecture #6 OPERATORS AND ITS TYPES By Shahid Naseem (Lecturer)
Thinking Mathematically
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.
COMP Primitive and Class Types Yi Hong May 14, 2015.
Principles of Programming Chapter 4: Basic C Operators  In this chapter, you will learn about:  Arithmetic operators  Unary operators  Binary operators.
Boolean values Gateway to decision making. Background Our problem-solving solutions so far have the straight-line property –They execute the same statements.
Control statements Mostafa Abdallah
Operators & Expressions
Computer Programming with Java Chapter 2 Primitive Types, Assignment, and Expressions.
Operators.
1 CS161 Introduction to Computer Science Topic #6.
Department of Electronic & Electrical Engineering Expressions operators operands precedence associativity types.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I.
What is the Result and Type of the Following Expressions? int x=2, y=15;double u=2.0,v=15.0; -xx+yx-y x*vy / xx/yy%xx%y u*vu/vv/uu%v x * u(x+y)*uu /(x-x)
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 15, 2004 Lecture Number: 11.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
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.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators.
Lecture 3 Selection Statements
Relational Operator and Operations
Chapter 3 Selection Statements
Computer Science 210 Computer Organization
Sequence, Selection, Iteration The IF Statement
Lecture 2: Data Types, Variables, Operators, and Expressions
A mechanism for deciding whether an action should be taken
Relational Operations
An Introduction to Programming with C++ Fifth Edition
OPERATORS (2) CSC 111.
Logical Operators & Truth Tables.
Relational Operators Operator Meaning < Less than > Greater than
CSC530 Data Structure - Decision
Computer Science 210 Computer Organization
Chapter 2 Programming Basics.
Lecture 5 Binary Operation Boolean Logic. Binary Operations Addition Subtraction Multiplication Division.
Life is Full of Alternatives
The boolean type and boolean operators
boolean Expressions Relational, Equality, and Logical Operators
Presentation transcript:

Boolean Types & Compound Conditionals CSC 1401: Introduction to Programming with Java Lecture 4 – Part 3 Wanda M. Kunkle

2 The Type Boolean The boolean type is a primitive data type whose value is either true or false. Associated with it are two symbolic constants, true and false. An example of a symbolic constant with which you are already familiar is . An example of a symbolic constant with which you are already familiar is .

3 The Type Boolean You declare variables of type boolean the same way you declare variables of type int, float, double, or char. Examples: boolean isPositive; boolean isEven; Examples: boolean isPositive; boolean isEven; You assign values to variables of type boolean the same way you assign values to variables of type int, float, double, or char. Examples: boolean isPositive = true; boolean isEven = false; Examples: boolean isPositive = true; boolean isEven = false;

4 The Type Boolean Let’s look at a Java program that uses boolean variables: EvenOrOdd.java EvenOrOdd.java

5 Boolean (or Logical) Operators The Java logical operators can be used to form complex conditions by combining two or more simple conditions. The most frequently used logical operators are: && (and) && (and) || (or) || (or) ! (not) ! (not)

6 Boolean (or Logical) Operators && (and) Usage: Usage: When used to combine two simple conditions, the resulting condition is true if both of the simple conditions are true; false, otherwise Format: (Condition1 && Condition2) // Condition1 and Condition2 // are simple conditions Format: (Condition1 && Condition2) // Condition1 and Condition2 // are simple conditions Example: char letter = 'd'; if (letter >= 'A' && letter = 'a' && letter = 'A' && letter = 'a' && letter <= 'z') out.writeln("Lowercase");

7 The Type Boolean Let’s look at a Java program that uses the and operator: UpperOrLower.java UpperOrLower.java

8 Boolean (or Logical) Operators || (or) Usage: Usage: When used to combine two simple conditions, the resulting condition is true if at least one of the simple conditions is true; false, if both conditions are false Format: (Condition1 || Condition2) // Condition1 and Condition2 // are simple conditions Format: (Condition1 || Condition2) // Condition1 and Condition2 // are simple conditions Example: float examGrade = 80; if (examGrade 100) out.writeln("Invalid grade!"); else out.writeln("Valid grade!"); Example: float examGrade = 80; if (examGrade 100) out.writeln("Invalid grade!"); else out.writeln("Valid grade!");

9 The Type Boolean Let’s look at a Java program that uses the or operator: Grade3.java Grade3.java

10 Boolean (or Logical) Operators ! (not) Usage: Usage: Used to negate the value of a condition; i.e., returns the opposite value of a condition Format: (!Condition1)// Condition1 is a simple condition Format: (!Condition1)// Condition1 is a simple condition Example: boolean isPositive = false; boolean isNegative = !isPositive; if (isNegative) out.writeln("The number is negative."); else out.writeln("The number is positive."); Example: boolean isPositive = false; boolean isNegative = !isPositive; if (isNegative) out.writeln("The number is negative."); else out.writeln("The number is positive.");

11 Truth Table for the Boolean && (and) Operator Value of X Value of Y Resulting Value of X && Y truetruetrue truefalsefalse falsetruefalse falsefalsefalse

12 Truth Table for the Boolean || (or) Operator Value of X Value of Y Resulting Value of X || Y truetruetrue truefalsetrue falsetruetrue falsefalsefalse

13 Truth Table for the Boolean ! (not) Operator Value of X Resulting Value of !X truefalse falsetrue

14 Operator Precedence Revisited First: Unary operators +, -, ! Second: Binary arithmetic operators *, /, % Third: Binary arithmetic operators +, - Fourth: Boolean operators, = Fifth: Boolean operators ==, != Sixth: Boolean operator & (similar to &&) Seventh: Boolean operator | (similar to ||) Eighth: Boolean operator && Ninth: Boolean operator || Highest Precedence Lowest Precedence

15 Conditional Operator (? :) The conditional operator (?:) can be used in place of an if-else statement. Example: out.writeln( grade >= 60 ? "You passed! :-)" : "You didn’t pass. :-(" ); is equivalent to: if (grade >= 60) out.writeln("You passed! :-)"); else out.writeln("You didn't pass. :-(");

16 Conditional Operator (? :) Example: out.writeln( grade >= 60 ? "You passed! :-)" : "You didn’t pass. :-(" ); To clarify: The boolean expression to the left of the ? Is the condition. The boolean expression to the left of the ? Is the condition. The expression between the ? And the : is the value of the boolean expression if the condition evaluates to true. The expression between the ? And the : is the value of the boolean expression if the condition evaluates to true. The expression to the right of the : is the value of the boolean expression if the condition evaluates to false. The expression to the right of the : is the value of the boolean expression if the condition evaluates to false.

17 Conditional Operator (? :) Note: I would “spare you” this operator were it not for the fact that it appears in Lab 3. I would “spare you” this operator were it not for the fact that it appears in Lab 3.