Branching Chapter 5 11/14/16 & 11/15/16 1 1.

Slides:



Advertisements
Similar presentations
3-1 Chapter 3 Flow of Control (part a - branching)
Advertisements

INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter4: Control Statements Part I.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
Chapter 5 Selection Statements. Topics Controlling program flow selection –if –switch Boolean expressions –boolean primitive type –comparison operators.
Introduction to Computers and Programming Lecture 5 New York University.
Introduction to Computer Programming Decisions If/Else Booleans.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
1 Basic control structures Overview l Relational and Logical Operations l Selection structures »if statement »switch statement l Preview:
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
Computer Science Selection Structures.
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 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
A First Look at Java Chapter 2 1/29 & 2/2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
More about Java Chapter 2 9/8 & 9/9 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Introduction Chapter 1 8/31 & 9/1 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Chapter 4 10/26 & 10/27. More Linux Commands mkdir rmdir echo > redirect output mv file, directory mv oldFileName newFileName more file rm file.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
CSC 1051 M.A. Papalaskari, Villanova University Algorithms 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.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
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.
COMP 110 Branching Statements and Boolean Expressions Luv Kohli September 8, 2008 MWF 2-2:50 pm Sitterson
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 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:
Just Enough Java 29-Sep-17.
CS0007: Introduction to Computer Programming
CompSci 230 S Programming Techniques
Wrapper Classes Debugging Interlude 1
Chapter 4: Control Structures I
CS0007: Introduction to Computer Programming
Chapter 4: Control Structures I
Chapter 3 Selections ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH Introduction to Java Programming, Liang (Pearson 2014)
Multiple if-else boolean Data
Operator Precedence Operators Precedence Parentheses () unary
Dialogues and Wrapper Classes
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,
SELECTION STATEMENTS (1)
Boolean Expressions And if…else Statements.
Repetition Chapter 6 12/06/16 & 12/07/16 1 1
Chapter 4 Control Statements: Part I
Chapter 4: Control Structures I
Debugging 9/22/15 & 9/23/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Multiple if-else boolean Data
CS139 October 11, 2004.
Selection Statements.
Control Structure Chapter 3.
Chapter 3: Selection Structures: Making Decisions
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Chapter 3: Selection Structures: Making Decisions
Outline Software Development Activities
Just Enough Java 17-May-19.
Conditionals and Loops
Control Structure.
Repetition CSC 1051 – Data Structures and Algorithms I Course website:
Presentation transcript:

Branching Chapter 5 11/14/16 & 11/15/16 1 1

Chapter Contents Flow of Control The if Statement Basic Comparisons Compound Statements Basic Comparisons Comparing Primitives Comparing Objects 2 2

Chapter Contents The if-else Statement Logical Operators 3 3 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 3 3

Flow of Control Statements normally executed in sequence From first to last This sequence can be altered A statement may call another method Decision statements cause different sequences to be followed Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 4 4

Flow of Control Figure 7-1 Invoking a method 5 5 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 5 5

If Statement int broncos = 24; int lobos = 31; if(broncos < lobos) System.out.println("Broncos lose!"); System.out.println("Good Night!"); 6 6

The if Statement Specify a condition Syntax If condition is true one or more statements execute If condition is false these statements skipped Syntax Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 7 7

The if Statement Figure 7-2 The logic of an if statement 8 8

Basic Comparisons Figure 7-3 Relational operators that compare primitive values Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 9 9

Write a Program Write a program to ask the user for her age. Output “Old Enough to Join the Army” if she is 18 or older. Next output a message that says “Army Strong.”. 10 10

Participation Write a class called Thermometer. Give the class a parameterized constructor that initializes the temperature. If the value passed to the constructor is greater that 135, output “Record High!” and set the temperature to the passed value, otherwise just the temperature to the value with no output. No need to write any more methods. Write a tester to test the constructor. 11 11

Compound Statements When if statement requires multiple statements to execute Enclose those multiple statements within braces { } after if (condition) if(resp == YES){ System.out.println("Evaluate Launch Conditions."); System.out.println("Respond as prompted"); System.out.println("call 888-555-5555"); } Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 12 12

Comparing Objects Methods equals for String Thus, == operator Object variable does not contain object Object variable contains reference to it Thus, == operator Compares references Not values Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 13 13

Comparing Objects String friend = "Sam"; //String with same data String name = new String("Sam"); if(friend != name) //Compares addresses System.out.println("Not the same object!"); if(friend.equals(name)) System.out.println ("Words in objects are equal."); 14 14

Comparing Objects Distinct references to strings whose values are the same 15 15

Comparing Objects Two variables containing equal references to the same string 16 16

If-else & Logical Operators 11/30/15 & 12/01/15

A Program with else A program that asks the user whether the user wants a quote by Turing or another quote. For Turing output:"Machines take me by surprise with great frequency.- Turing" For other output:"Intelligence is the ability to adapt to change.-Hawking" BrainyQuotes.java 18 18

import java.util.Scanner; public class BrainyQuotes { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Would you like a Turing quote?"); String ans = scan.next(); if(ans.equals("yes")) System.out.println("Machines take me" + " by surprise with great frequency.--Turing"); else System.out.println("Intelligence is" + " the ability to adapt to change.--Hawking"); scan.close(); }

The if-else Statement When you wish to do one thing if the condition is true But you want to do something else when the condition is false Syntax Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 20 20

The if-else Statement The logic of an if-else statement 21 21 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 21 21

Write an if-else Statement Write an if-else statement that assigns 0.1 to commission unless sales is greater that or equal to 50000.0, in which case it assigns 0.2 to commission.

Logical Operators 23 23

Logical Operators Logical operators Used to combine boolean expressions Extra parenthesis are optional. 20 <= age < 30 doesn't work Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 24 24

Logical Operators The operator && 25 25 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 25 25

Logical Operators The operator || 26 26 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 26 26

Logical Operators The operator ! ! true -> false !("Joe".equals("Joe")) ! false -> true !("Sam".equals("Joe")) Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 27 27

Leap Year A year is a leap year if it is Divisible by 4 but not divisible by 100 Or divisible by 400 Write a program to input the year. It should output a message about whether it is a leap year.

Precedence for Ops Order of operations in a logical expression 29 29 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 29 29

Wrap Up Question How do you best compare two Strings to see if they are equal? Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 30 30

Participation Write a program to input the measures of three sides of a triangle. Output a statement the says it is either “equilateral” or “not equilateral” after comparing the sides. Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 31 31

Branching Chapter 5 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 32 32