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

Similar documents
School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

Chapter 4: Control Structures I

Motivating Examples (1.1) Selections. Motivating Examples (1.2) Learning Outcomes. EECS1022: Programming for Mobile Computing Winter 2018

Selections. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

H212 Introduction to Software Systems Honors

Oct Decision Structures cont d

2.2 - Making Decisions

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

Controls Structure for Repetition

Programming: Java. Chapter Objectives. Control Structures. Chapter 4: Control Structures I. Program Design Including Data Structures

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

Midterm Examination (MTA)

AP Computer Science Unit 1. Programs

Programming with Java

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website:

Conditional Programming

Module 3 SELECTION STRUCTURES 2/15/19 CSE 1321 MODULE 3 1

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Flow Control. Key Notion. Statement Categories. 28-Oct-10

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

Mr. Monroe s Guide to Mastering Java Syntax

Flow Control. Boaz Kantor Introduction to Computer Science, Fall semester IDC Herzliya

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

Fundamentals of Programming Data Types & Methods

Using Classes and Objects Chapters 3 Creating Objects Section 3.1 The String Class Section 3.2 The Scanner Class Section 2.6

Introduction to Computer Science Unit 2. Notes

A token is a sequence of characters not including any whitespace.

Java I/O and Control Structures

Please answer the following questions. Do not re-code the enclosed codes if you have already completed them.

Section 2.2 Your First Program in Java: Printing a Line of Text

Full file at

CSC 1051 Data Structures and Algorithms I

CSG 100 Data Structures

Boolean Expressions. So, for example, here are the results of several simple Boolean expressions:

CSE 114 Computer Science I

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Conditional Execution

AP Computer Science Unit 1. Writing Programs Using BlueJ

Lesson 7 Part 2 Flags

Section 2.2 Your First Program in Java: Printing a Line of Text

COE 212 Engineering Programming. Welcome to Exam I Tuesday November 11, 2014

CS 106 Introduction to Computer Science I

Java I/O and Control Structures Algorithms in everyday life

Expression statements are formed by adding a semicolon to the end of certain types of expressions.

CS 112 Introduction to Programming

CS 112 Introduction to Programming

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Full file at

Java Coding 3. Over & over again!

1. [3 pts] What is your section number, the period your discussion meets, and the name of your discussion leader?

Introduction to Computer Science, Shimon Schocken, IDC Herzliya. Lectures Control Structures

Midterms Save the Dates!

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

Control Structures: if and while A C S L E C T U R E 4

Practice Midterm 1 Answer Key

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 24, Name: KEY 1

Topic 11 Scanner object, conditional execution

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

Object Oriented Programming. Java-Lecture 6 - Arrays

AP COMPUTER SCIENCE A

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

CSE 1223: Introduction to Computer Programming in Java Chapter 1 Computer Basics

Lab Exercise 1. Objectives: Part 1. Introduction

Chapter 2 ELEMENTARY PROGRAMMING

Lectures 3-1, 3-2. Control Structures. Control Structures, Shimon Schocken IDC Herzliya, slide 1

Loops. GEEN163 Introduction to Computer Programming

Control flow, conditionals, boolean expressions, block statements, nested statements. Course website:

Lecture Set 4: More About Methods and More About Operators

What two elements are usually present for calculating a total of a series of numbers?

PROGRAMMING FUNDAMENTALS

4. Java Project Design, Input Methods

University of Palestine. Mid Exam Total Grade: 100

Claremont McKenna College Computer Science

Instance Method Development Demo

CSC 1051 Data Structures and Algorithms I

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

CS141 Programming Assignment #5

For that purpose, java provides control structures that serve to specify what has to be done by our program, when and under which circumstances.

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Elementary Programming

Program Control Flow

Program Control Flow

Warm up Exercise. What are the types and values of the following expressions: * (3 + 1) 3 / / 2.0 (int)1.0 / 2

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 25, Name: KEY A

Tutorial 03. Exercise 1: CSC111 Computer Programming I

Lecture Set 2: Starting Java

CS111: PROGRAMMING LANGUAGE II

Chapter 3. Selections

ICSE Class 10 Computer Applications ( Java ) 2014 Solved Question Paper

Topic 11 Scanner object, conditional execution

St. Edmund Preparatory High School Brooklyn, NY

Introduction to Java & Fundamental Data Types

Lecture Set 2: Starting Java

Section 004 Spring CS 170 Exam 1. Name (print): Instructions:

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

Data Structure and Programming Languages

Practice Midterm 1. Problem Points Score TOTAL 50

Transcription:

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

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

Chapter Contents The if-else Statement Logical Operators

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

Flow of Control Figure 7-1 Invoking a method

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

The if Statement Specify a condition If condition is true one or more statements execute If condition is false these statements skipped Syntax

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

Basic Comparisons Figure 7-3 Relational operators that compare primitive values

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..

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!. No need to write any more methods. Write a tester to test the constructor.

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"); }

Comparing Objects Methods equals for String Object variable does not contain object Object variable contains reference to it Thus, == operator Compares references Not values

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.");

Comparing Objects Distinct references to strings whose values are the same

Comparing Objects Two variables containing equal references to the same string

If-else & Logical Operators

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

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(); } //Note:next( ) inputs a String up to the next whitespace.

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

The if-else Statement The logic of an if-else statement

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

Logical Operators Logical operators Used to combine boolean expressions Extra parenthesis are optional. 20 <= age < 30 doesn't work

Logical Operators The operator &&

Logical Operators The operator

Logical Operators The operator!! true -> false!("joe".equals("joe"))! false -> true!("sam".equals("joe"))

Precedence for Ops Order of operations in a logical expression

Wrap Up Question 1)How do you best compare two Strings to see if they are equal?

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.

import java.util.scanner; public class Equilateral { public static void main(string[] args) { Scanner scan = new Scanner(System.in); //Input the side measures System.out.print("Enter Side A:"); double sidea = scan.nextdouble(); System.out.print("Enter Side B:"); double sideb = scan.nextdouble(); System.out.print("Enter Side C:"); double sidec = scan.nextdouble(); //Find out if it is equilateral } }

Branching Chapter 5