Boolean expressions Conditional statements and expressions.

Slides:



Advertisements
Similar presentations
Flow of Control Usually the order of statement execution through a method is linear: one after another flow of control: the order statements are executed.
Advertisements

Logic & program control part 2: Simple selection structures.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 10, 2009.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Introduction to Computers and Programming Lecture 5 New York University.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
Chapter 4 Making Decisions
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007.
If statements Chapter 3. Selection Want to be able to do a statement sometimes, but not others if it is raining, wear a raincoat. Start first with how.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/33 Conditionals and Loops Now we will examine programming statements.
Review Review Review. Concepts Comments: definition, example, different types of comments Class: definition, example Object: definition, example Data.
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 3: Program Statements
Instructor: Craig Duckett Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Tuesday, October 13 th I will double dog try to.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
© 2005 Lawrenceville Press Slide 1 Chapter 5 Relational Operators Relational OperatorMeaning =greater than.
Flow of Control Part 1: Selection
CMSC 150 CONDITIONAL EXECUTION CS 150: Mon 16 Jan 2012.
The if-else statement. The if-else statement in Java The if-else statement is the second conditional statement in Java The if-else statement selects one.
1 Chapter 3 Selections. 2 Outline 1. Flow of Control 2. Conditional Statements 3. The if Statement 4. The if-else Statement 5. The Conditional operator.
Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.
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.
In the last lesson we discussed about: Casting Precedence The “=“ used as an assignment operator Made a calculate average program.
Decisions Bush decision making.
Print Me Who’s Data? First Executed Name Me My Mistake Q $100 Q $200 Q $300 Q $400 Q $500 Q $100 Q $200 Q $300 Q $400 Q $500 Final Jeopardy.
Java Review if Online Time For loop Quiz on Thursday.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
 Type Called bool  Bool has only two possible values: True and False.
(Java Looping and Conditional Statements). Flow of control Sequential Executes instructions in order Method Calls Transfer control to the methods, then.
Switch statement.
Computer Science 210 Computer Organization
COMP 14 Introduction to Programming
Boolean expressions and if-else statements
Selection (also known as Branching) Jumail Bin Taliba by
Multiple variables can be created in one declaration
Boolean Expressions and If
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
Boolean expressions Conditional statements and expressions
SELECTION STATEMENTS (1)
Control Statement Examples
Expression Review what is the result and type of these expressions?
Conditional Statements
Relational Operators Operator Meaning < Less than > Greater than
Outline Boolean Expressions The if Statement Comparing Data
Computer Science 210 Computer Organization
Fundamentals 2.
Building Java Programs
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Life is Full of Alternatives
CSC 1051 – Data Structures and Algorithms I
Outline Software Development Activities
Conditionals and Loops
CSS161: Fundamentals of Computing
Conditionals.
Presentation transcript:

Boolean expressions Conditional statements and expressions

Penny guessing game I weighed a pound of pennies and found there was exactly 160. I weighed a pound of pennies and found there was exactly is an integer. 160 is an integer. int exact = 160; But nothing stops me from changing it. It is also a constant that I never want to change. It is also a constant that I never want to change. final int exact = 160; So let’s write a guessing game. So let’s write a guessing game.

Penny guessing game import java.util.Scanner; public class GuessingGame { public static void main ( String s[] ) { final int exact = 160; Scanner in = new Scanner( System.in ); //prompt for a guess System.out.print( “Guess the # of pennies in a pound. “ ); int guess = in.nextInt(); //check the answer // If guess and exact are equal, then let them know. // Otherwise, let them know that their guess was incorrect. (We currently don’t have the language tools to do this!) in.close();}}

Penny guessing game import java.util.Scanner; public class GuessingGame { public static void main ( String s[] ) { final int exact = 160; Scanner in = new Scanner( System.in ); //prompt for a guess System.out.print( “Guess the # of pennies in a pound. “ ); int guess = in.nextInt(); //check the answer if (guess==exact) System.out.println( “You are correct!” ); System.out.println( “You are correct!” );else System.out.println( “Nope.” ); System.out.println( “Nope.” );in.close();}}

Penny guessing game … //check the answer if (guess==exact) System.out.println( “You are correct!” ); System.out.println( “You are correct!” );else System.out.println( “Nope.” ); System.out.println( “Nope.” );… == is a boolean (true or false) operator == is a boolean (true or false) operator It tests for equality. It tests for equality. A==B is a boolean expression A==B is a boolean expression The result of this expression is either true or false. The result of this expression is either true or false.

George Boole 2 November 1815 – 8 December November 1815 – 8 December 1864 an English mathematician and philosopher an English mathematician and philosopher As the inventor of Boolean logic—the basis of modern digital computer logic—Boole is regarded in hindsight as a founder of the field of computer science. As the inventor of Boolean logic—the basis of modern digital computer logic—Boole is regarded in hindsight as a founder of the field of computer science.

Penny guessing game Let’s accept a guess that’s 160 ± 10%. Let’s accept a guess that’s 160 ± 10%.

Penny guessing game Let’s accept a guess that’s 160 ± 10%. Let’s accept a guess that’s 160 ± 10%. So let’s define two more constants – the minimum and the maximum that we’ll accept. So let’s define two more constants – the minimum and the maximum that we’ll accept.

Penny guessing game Let’s accept a guess that’s 160 ± 10%. Let’s accept a guess that’s 160 ± 10%. So let’s define two more constants – the minimum and the maximum that we’ll accept. So let’s define two more constants – the minimum and the maximum that we’ll accept. final int exact = 160; final int max = exact + exact/10; final int min = exact - exact/10;

Penny guessing game Now we must change the check to include the acceptable range. Now we must change the check to include the acceptable range.… //check the answer if (guess==exact) System.out.println( “You are correct!” ); System.out.println( “You are correct!” );else System.out.println( “Nope.” ); System.out.println( “Nope.” );…

Penny guessing game Now we must change the check to include the acceptable range. Now we must change the check to include the acceptable range.… //check the answer if (guess==exact) System.out.println( “You are correct!” ); System.out.println( “You are correct!” ); else if (guess<min) System.out.println( “Nope.” ); System.out.println( “Nope.” ); else if (guess>max) System.out.println( “Nope.” ); System.out.println( “Nope.” );else System.out.println( “Close enough.” ); System.out.println( “Close enough.” );…

Penny guessing game … //check the answer if (guess==exact) System.out.println( “You are correct!” ); System.out.println( “You are correct!” ); else if (guess<min) System.out.println( “Nope.” ); System.out.println( “Nope.” ); else if (guess>max) System.out.println( “Nope.” ); System.out.println( “Nope.” );else System.out.println( “Close enough.” ); System.out.println( “Close enough.” );… Note that we write “Nope.” if the first boolean expression or the second boolean expression is true. Wouldn’t it be nice if we had a boolean operator for or?

Penny guessing game … //check the answer if (guess==exact) System.out.println( “You are correct!” ); System.out.println( “You are correct!” ); else if (guess max) System.out.println( “Nope.” ); System.out.println( “Nope.” );else System.out.println( “Close enough.” ); System.out.println( “Close enough.” );… boolean operator for or

Penny guessing game … //check the answer if (guess==exact) System.out.println( “You are correct!” ); System.out.println( “You are correct!” ); else if (guess>=min & guess =min & guess<=max) System.out.println( “Close enough.” ); System.out.println( “Close enough.” );else System.out.println( “Nope.” ); System.out.println( “Nope.” );… boolean operator for and

Operators used in boolean expressions Unary (i.e., op A): Unary (i.e., op A): !not !not Binary (i.e., A op B): Binary (i.e., A op B):, =lt, gt, le, ge, =lt, gt, le, ge ==, !=equals, not equal ==, !=equals, not equal &and &and |or |or &&and &&and ||or ||or This is also the precedence from highest to lowest. This is also the precedence from highest to lowest.

Truth table for AND

Truth table for OR

Truth table for NOT

COMPARING STRINGS

Comparing strings == doesn’t work (as you expect) so please don’t use it for comparing strings. (But use == for comparing ints or doubles.) == doesn’t work (as you expect) so please don’t use it for comparing strings. (But use == for comparing ints or doubles.) Example: Example: String city1 = “Philadelphia”; String city2 = “Phoenix”; I want to say: I want to say: if (city1==city2)but I can’t!

Comparing strings Use.equals() instead: Use.equals() instead: if (city1.equals(city2)) ….equals() is case sensitive (i.e., NYC is not the same as NyC).equals() is case sensitive (i.e., NYC is not the same as NyC).equalsIgnoreCase() is not case sensitive.equalsIgnoreCase() is not case sensitive if (city1.equalsIgnoreCase(city2)) …

if’s and blocks Sometimes we wish to execute more than one statement for an if statement. Sometimes we wish to execute more than one statement for an if statement. if (x==y) singleStatement; if (x==y) { statement1;statement2;…statementn;} { s1; s2; … sn; } This is called a block.

if’s and blocks Common mistake: Are the following all the same (or are they different)? Common mistake: Are the following all the same (or are they different)? if (x==y) System.out.println( "hello" ); System.out.println( "there" ); if (x==y) System.out.println( "hello" ); System.out.println( "there" );

if’s and blocks Common mistake: Are the following all the same (or are they different)? Common mistake: Are the following all the same (or are they different)? if (x==y) System.out.println( "hello" ); System.out.println( "there" ); if (x==y) { System.out.println( "hello" ); System.out.println( "there" ); }

if’s and blocks Common mistake: Are the following all the same (or are they different)? Common mistake: Are the following all the same (or are they different)? if (x==y) { System.out.println( "hello" ); System.out.println( "there" ); } if (x==y) { System.out.println( "hello" ); System.out.println( "there" ); }

if’s and blocks Common mistake: Are the following all the same (or are they different)? Common mistake: Are the following all the same (or are they different)? if (x==y) { System.out.println( "hello" ); } System.out.println( "there" ); if (x==y) { System.out.println( "hello" ); } System.out.println( "there" );

if‘s and blocks Rule(s): Rule(s): Don’t be confused/misled by indentation! Don’t be confused/misled by indentation! The purpose of indentation is to make code more humanly readable. The purpose of indentation is to make code more humanly readable. Indentation does not matter to the compiler at all. Indentation does not matter to the compiler at all. Blocks (not indentation) group lines of code together. Blocks (not indentation) group lines of code together.

if examples if (x == 2 & y < 10) x = 10; if (x == 2) { x = 10; } if (x == y) { x = 0; y = 1; }

if examples if (x==y) { x = 0; y = 1; } else { x = 1; y = 0; }

if examples if (x==1) { x = 2; y = 3; } else if (x==2) { x = 3; y = 4; } else { x = 1; y = 0; }

if examples if (x==1) { x = 2; y = 3; } else if (x==2) { x = 3; y = 4; } else { //we don’t need to restate else if (x!=1 && x!=2) x = 1; y = 0; }

if-else form in general if (conds1) one-statement; or { block-of-statements } else if (conds2) one-statement; or { block-of-statements } … else if (condsn) one-statement; or { block-of-statements } else

if-else form Same or different? Same or different? int x = 5; if (x >= 4) System.out.println( "A" ); if (x >=5) System.out.println( "B" ); int x = 5; if (x >= 4) System.out.println( "A" ); else if (x >=5) System.out.println( "B" );

if-else form Same or different? Same or different? int x = 5; if (x >= 4) { System.out.println( "A" ); } else if (x >=5) { System.out.println( "B" ); } int x = 5; if (x >= 4) System.out.println( "A" ); else if (x >=5) System.out.println( "B" );

if-else form Same or different? Same or different? int x = 5; if (x >= 4) { System.out.println( "A" ); } else if (x >=5) { System.out.println( "B" ); } int x = 5; if (x >= 4) { System.out.println( "A" ); } else if (x >=5) { System.out.println( "B" ); }

Boolean type, and boolean variables boolean b; b = true; b = false; int x = 10; int y = 9; b = (x==y); b = (x>y);

Boolean I/O boolean answer = in.nextBoolean(); boolean answer = in.nextBoolean(); You must type in either true or false and hit return (enter). You must type in either true or false and hit return (enter). System.out.println( answer ); //works as too System.out.println( answer ); //works as too

Conditional operator ?: Many programs contain code like the following: if (n > 0) average = sum / n; else average = 0;

Conditional operator ?: example Many programs contain code like the following: int apples = 6; if (apples != 1) System.out.printf( "I have %d apples. \n", apples ); else System.out.printf( "I have %d apple. \n", apples ); int apples = 6; if (apples != 1) System.out.println( "I have " + apples + " apples." ); else System.out.println( "I have " + apples + " apple." ); (examples use printf and println)

Conditional operator ?: (boolean-expr) ? expr if-true : expr if-false evaluated if-and-only-if (iff) boolean-expr is true evaluated iff boolean-expr is false

Conditional operator ?: if (n > 0) average = sum / n; else average = 0; average = (n > 0) ? sum / n : 0;

Conditional operator ?: (example using printf) int apples = 6; if (apples != 1) System.out.printf( "I have %d apples \n", apples ); else System.out.printf( "I have %d apple \n", apples ); System.out.printf( "I have %d apple%s \n", apples, (apples != 1) ? "s" : "" );

Conditional operator ?: (example using println) int apples = 6; if (apples != 1) System.out.println( "I have " + apples + " apples." ); else System.out.println( "I have " + apples + " apple." ); System.out.printf( "I have " + apples + " apple" + (apples != 1) ? "s" : "" + "." );

Conditional operator ?: Problem: We have 2 int’s, A and B. We want another int, C, to be set to the maximum of A and B. Problem: We have 2 int’s, A and B. We want another int, C, to be set to the maximum of A and B. How can we do this with if-else? How can we do this with if-else? How can we do this with ?:? How can we do this with ?:?

Conditional operator ?: Problem: We have 2 int’s, A and B. We want another int, C, to be set to the maximum of A and B. Problem: We have 2 int’s, A and B. We want another int, C, to be set to the maximum of A and B. How can we do this with if-else? How can we do this with if-else? if (A>B) C = A; else C = B; How can we do this with ?:? How can we do this with ?:?

Conditional operator ?: Problem: We have 2 int’s, A and B. We want another int, C, to be set to the maximum of A and B. Problem: We have 2 int’s, A and B. We want another int, C, to be set to the maximum of A and B. How can we do this with if-else? How can we do this with if-else? if (A>B) C = A; else C = B; How can we do this with ?:? How can we do this with ?:? C = (A>B) ? A : B;