Boolean expressions Conditional statements and expressions

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

CSCI 51 Introduction to Programming Dr. Joshua Stough February 10, 2009.
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
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 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
Chapter 4 Making Decisions
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
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.
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
Boolean expressions Conditional statements and expressions.
Chapter 3: Program Statements
Chapter 5: Conditionals and loops. 2 Conditionals and Loops Now we will examine programming statements that allow us to: make decisions repeat processing.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Chapter 5 Loops.
Chapter 3 Flow of Control Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008 Pearson.
Flow of Control Part 1: Selection
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.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
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.
Decisions. Three Forms of Decision Making in Java if statements (test a boolean expression) switch statements (test an integer expression) conditional.
More loops while and do-while. Recall the for loop in general for (initialization; boolean_expression; update) { }
Decisions Bush decision making.
Java Review if Online Time For loop Quiz on Thursday.
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
Computer Programming Lab 9. Exercise 1 Source Code package excercise1; import java.util.Scanner; public class Excercise1 { public static void main(String[]
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.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Structured Programming Structured Programming is writing a program in terms of only 3 basic control structures: sequence selection repetition We have already.
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.
Lecture 4 CS140 Dick Steflik. Reading Keyboard Input Import java.util.Scanner – A simple text scanner which can parse primitive types and strings using.
Repetition Statements
CS0007: Introduction to Computer Programming
Switch statement.
CompSci 230 S Programming Techniques
Strings, conditional statements, Math
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
Lecture 4: Program Control Flow
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,
Repetition-Sentinel,Flag Loop/Do_While
SELECTION STATEMENTS (1)
Control Statement Examples
If statement.
Relational Operators Operator Meaning < Less than > Greater than
Outline Boolean Expressions The if Statement Comparing Data
Fundamentals 2.
Chapter 4: Decision Structures and Boolean Logic
AP Java Review If else.
Chapter 7 Conditional Statements
Control Structure Chapter 3.
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
CS2011 Introduction to Programming I Selections (I)
AP Java Review If else.
Chapter 5 Decisions.
Life is Full of Alternatives
CSC 1051 – Data Structures and Algorithms I
Outline Software Development Activities
Question 1a) What is printed by the following Java program? int s;
Conditionals and Loops
CSS161: Fundamentals of Computing
Chapter 4: Decision Structures and Boolean Logic
Control Structure.
Presentation transcript:

Boolean expressions Conditional statements and expressions

Penny guessing game I weighed a pound of pennies and found there was exactly 160. 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. final int exact = 160; 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==exact) System.out.println( “You are correct!” ); else System.out.println( “Nope.” ); in.close(); }

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

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

Penny guessing game 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.

Penny guessing game 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. 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. … //check the answer if (guess==exact) System.out.println( “You are correct!” ); else System.out.println( “Nope.” );

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

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

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

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

Truth table for AND

Truth table for OR

Truth table for NOT

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

Comparing strings Use .equals() instead: if (city1.equals(city2)) … .equals() is case sensitive (i.e., NYC is not the same as NyC) .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. if (x==y) singleStatement; if (x==y) { statement1; statement2; … statementn; } { s1; s2; … sn; } This is called a block.

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

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;

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

Boolean type and 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(); You must type in either true or false and hit return (enter). 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 ?: 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 );

Conditional operator ?: (boolean-expr) ? exprif-true : exprif-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 ?: 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 ?: 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 ?:?

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. How can we do this with if-else? if (A>B) C = A; else C = B; 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. How can we do this with if-else? if (A>B) C = A; else C = B; How can we do this with ?:? C = (A>B) ? A : B;