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.

Slides:



Advertisements
Similar presentations
Logic & program control part 3: Compound selection structures.
Advertisements

CS0007: Introduction to Computer Programming
8-May-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
CPSC150 JavaDr. L. Lambert CPSC150 Fall 2005 Dr. L Lambert.
CPSC150 Spring 2006 Dr. L Lambert. Week 1/2 intro (and Chapter 1)
16-Jun-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else 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.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
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,
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Conditionals II Lecture 11, Thu Feb
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
Conditional If Week 3. Lecture outcomes Boolean operators – == (equal ) – OR (||) – AND (&&) If statements User input vs command line arguments.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
© 2005 Lawrenceville Press Slide 1 Chapter 5 Relational Operators Relational OperatorMeaning =greater than.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
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.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Chapter 4: Control Structures SELECTION STATEMENTS.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
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.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
OOP (pre) Basic Programming. Writing to Screen print will display the string to the screen, the following print statement will be appended to the previous.
COMP Flow of Control: Branching 1 Yi Hong May 19, 2015.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
COMP 110: Spring Announcements Lab 1 was due at noon Lab 2 on Friday (Bring Laptops) Assignment 1 is online TA Office hours online 30-min quiz at.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
ICT Introduction to Programming Chapter 4 – Control Structures I.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Control statements Mostafa Abdallah
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC INTRO TO COMPUTING - PROGRAMMING If Statement.
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
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.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CS0007: Introduction to Computer Programming
CompSci 230 S Programming Techniques
Lecture 3 Selection Statements
Chapter 4: Control Structures I
CS0007: Introduction to Computer Programming
Chapter 4: Control Structures I
Chapter 4: Making Decisions.
Introduction to programming in java
SELECTION STATEMENTS (1)
Chapter 4: Control Structures I
Self study.
Building Java Programs
Control Structure Chapter 3.
Chapter 2 Programming Basics.
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
PROGRAM FLOWCHART Selection Statements.
CSC 1051 – Data Structures and Algorithms I
Outline Software Development Activities
Comparing Data & the ‘switch’ Statement
CSS161: Fundamentals of Computing
Control Structure.
Presentation transcript:

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 to make a condition

if statements if (booleanexpression) java statement; any Java statement you know about we don’t know about this

Boolean Expressions Evaluate to be true or false boolean variables –boolean isVisible = false; relational expressions (compares values) logical expressions (compares expressions with and, or, not)

Comparing Values, >=, <= != == 3 == 4 false 4 == 4 true 3 < 4 true 3 <= 4 true 3 >= 4 false 3 > 4 false These do not work for object types

Evaluating Boolean Expressions A true, B false, C error int x=3; int y = 4; int z=5; x < y x < y < z x = y y == 4 z >= x x != 3 (x + 4) < (y - 1) true error: < cannot be applied to boolean,int error: incompatible types - found int; expected boolean true true false 7 < 3; false

Logical Operators and (&&, single & very different) –both values must be true for the expression to be true –if it is cold and rainy, wear your winter raincoat (both must be true) or (|| - on keyboard, called pipe symbol) –either value can be true –if it is cold or rainy, wear a coat (if either or both is true, do) not (!) –changes the truth value of the expression –if it is not cold, do not wear a winter coat

Logical Operators A true, B false, C error int x=3; int y=10; (x < y) && (y < 20) (x == 3) || (y == 3) x < y; 3 < 10; true y < 20; 10 < 20; true true && true is true x == 3 true. short circuit evaluation (y==3 false true || false is true)

More logical operators A true, B false, C error int x=3; int y=10; !(y=10) (x != 3) || (y != 3) trick question error !(y==10) y == 10 true !true false x != 3 false Keep going. y != 3 true false || true is true

Yet more logical operators A true, B false, C error int x=3; int y=10; !((x+1 < 4) || (y <= 10)) !((x+1 < 4) && (y <= 10)) x+1 = 4 4 < 4 false.keep going y <= 10 true false || true true ! true is false 4 < 4 false. DONE with &&. Do not look at y <=10. !false true

if statements if statement form: –if (boolean expression) java statement; if (x < y) System.out.println(“x < y”); you know both parts now

if statements cautions MUST have ( )s around boolean expression no syntax error for non-boolean like expressions only ONE statement in an if statement no ';' after if condition Make sure you account for values that are equal use relational operators only with primitives use equals, compareTo with String

Write on board Write an if statement that prints "Before Middle" if int x is less than int middle Write an if statement that assigns 0 to average (double) if numberCounted (an int) is 0

if-else If you want to do one thing if a condition is true and something else if not, use if-else. –form: if (condition) Java statement else Java statement if (x < y) System.out.println(x + " is less than the other number"); else System.out.println(y + " is less than the other number"); What's wrong with this logic?

> one statement in an if If you want to have more than one statement inside an if or an else, use {}s: if (x < y) { System.out.println(x + " is less than the other number”); x = 0; } else { System.out.println(y + " is less than the other number”); y = 0; }

If-else cautions either if clause or else clause or both may have {}s. After statements inside if and else clause are executed, control passes back to next sequential statement no ';' after else Make sure you account for values that are equal

Computer Work public class MyTwoInts { public static void main(String[] args) { java.util.Scanner kbd = new java.util.Scanner (System.in); System.out.println(“Enter two numbers: “); int x = kbd.nextInt( ); int y = kbd.nextInt( ); // Write CODE IN HERE TO PRINT THE 2 // numbers IN ORDER // THE SMALLEST FIRST }

Watch Out if (3 < 4) x = 3; else System.out.println(“3 < 4 is false”); x = 7; System.out.println( "the value of x is " + x); Prints what? A. the value of x is 3 B. the value of x is 0 C. 3 < 4 is false D. the value of x is 7 E. none of the above

Embedded ifs If statements and if-else statements may be embedded (if within if). simply evaluate them as the Java code is executed: if-else example is most common. sets up a table of conditions

Embedded if-else for table if (ave >= 90) grade = 'A'; else if ((ave = 80)) // note: need ()s around entire condition grade = 'B'; else if ((ave =70)) grade = 'C'; else if ((ave =60)) grade = 'D'; else if ((ave < 70) && (ave < 60)) grade = 'F';

Tracing through the embedded if

Fixing the embedded if if (ave >= 90) grade = 'A'; else if (ave >= 80) // We know (ave < 90) or we wouldn't be here grade = 'B'; else if (ave >=70) // we know ave < 80 grade = 'C'; else if (ave >=60) grade = 'D'; else // if ((ave < 70) && (ave < 60)) grade = 'F';

Cautions for embedded ifs Don't use redundant comparisons Make sure you check for values that are equal Account for out of range values Embedded ifs can be implemented using switch statements. We will not go over these or test you on them. But switch statements are in the book.

1.Write a program to read a day (1 for Monday, 2 for Tuesday, etc. to 5 for Friday), then print “yipee! Today is CPSC150.” for days 1, 3 and 5; or “Only one more day until CPSC150!” for days 2 and 4. 2.Write a program to read an integer, and print “yes” if it is even and “no” otherwise. 3.Write a program that prints a message about academic status according to this table. GPA and attempted hours will be read from the keyboard. Credit Hours Attempted * Minimum Good Standing Academic Probation Academic Suspension Or Less Or Less Or Less Or Less 91 Or More Or Less