Decisions. Three Forms of Decision Making in Java if statements (test a boolean expression) switch statements (test an integer expression) conditional.

Slides:



Advertisements
Similar presentations
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Advertisements

CSE 1301 Lecture 5B Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
(c) 2003 E.S.Boese1 Conditionals Chapter 10 - Student.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Primitive Data Types There are exactly eight primitive data types in Java four of them represent integers: byte (class Byte), short (class Short), int.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Chapter 4 Making Decisions
Switch Statements Comparing Exact Values. 2 The Switch Statement The switch statement provides another way to decide which statement to execute next The.
Java Programming Constructs 1 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 7 Decision Making : selection statements.
1 CSC103: Introduction to Computer and Programming Lecture No 11.
CSCI 1100/1202 January 28, The switch Statement The switch statement provides another means to decide which statement to execute next The switch.
© 2005 Lawrenceville Press Slide 1 Chapter 5 Relational Operators Relational OperatorMeaning =greater than.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
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.
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
Decision Making Selection structures (if....else and switch/case)
Switch Statements Comparing Exact Values. The Switch Statement: Syntax The switch statement provides another way to decide which statement to execute.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
1 2. Program Construction in Java. 2.4 Selection (decisions)
Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Chapter Making Decisions 4. Relational Operators 4.1.
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.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Control statements Mostafa Abdallah
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Decision Statements, Short- Circuit Evaluation, Errors.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Quiz 1 Exam 1 Next Monday. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) System.out.println(“You have an A!” ); else System.out.println(“You.
Control Structures- Decisions. Smart Computers Computer programs can be written to make computers seem smart Making computers smart is based on decision.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Chapter 4: Making Decisions.
Selections Java.
The switch Statement, and Introduction to Looping
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Chapter 4: Making Decisions.
Introduction to programming in java
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
Java LESSON 2 Operators & Conditionals
Control Statement Examples
محاضرة 1: مقدمة للمسـاق و مراجعـة للأساسيـات
Selection CSCE 121 J. Michael Moore.
Chapter 7 Conditional Statements
Flow Control Statements
Lecture Notes – Week 2 Lecture-2
Java Programming Review 1
The Java switch Statement
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Control Structure.
Presentation transcript:

Decisions

Three Forms of Decision Making in Java if statements (test a boolean expression) switch statements (test an integer expression) conditional operator (tests a boolean expression)

if Statement if(boolean expr.) statement [block] else if(boolean expr.) statement [block] else statement [block] if (a < 0) { // do something } else if (a == 0) { // do something else } else { // do something // in any case }

if Statement (cont.) One option: to do or not to do if (a < 0) a = 0; Two alternatives: one or the other if (a < 0) b = 0; else b = 1; Several alternatives: do one or do nothing if (a < 0) b = 0; else if (a < 1) b = 1; else if (a < 10) b = 2; // what if a = 12? Several alternatives with default (continues above structure) else b = 3;

Relational Operators Evaluate to a boolean (true/false) value <less than <=less than/equal >greater than >=greater than/equal ==equal !=not equal if (a < 10) if (a <= 10) if (a > b) if (a >= b) if (a == b * 13) if (a+b != c+d)

Boolean Operators combine boolean values b1 && b2 true if both b1 and b2 are true b1 || b2 true if either b1 or b2 is true b1 ^ b2 true if only b1 or b2 is true !b1 True if b1 is not true if (a > b && c != 7) if (a 10) if (a == 5 ^ b == 5) if (!person.isFemale())

Operator Precedence Arithmetic operators * / % + - Relational operators == != >= Boolean operators ! ^ && || Example: double crLimit, crUsed; boolean sale, hasAcct; sale = cash > price || hasAcct && crLimit – crUsed > price; sale = (cash > price) || (hasAcct && ((crLimit – crUsed) > price));

Traps else pairs with nearest unmatched if if (a > 10) if (b > 12) statement1; else statement2; statement3; What will happen if a = 5? (statement2 will be executed only if a>10 and b <=12) if (a > 10) { if (b > 12) { statement1; } else { statement2; } statement3;

More Traps a = 0 is an assignment statement a == 0 is a relational expression if (a = 0) will generate a compiler error

More Traps floats and doubles cannot be compared exactly double x = 6.4, y = 8.6; if (x + y == 15.) will probably fail if (Math.abs (x + y – 15.) <.0001) will achieve the desired effect

Comparing Objects the problem String s1 = “Monday”; String s2 = KeyboardReader.readString(); if (s2 == s1) compares references, and will fail even if “Monday” is typed at the keyboard the solution if (s2.equals (s1)) compares the contents of two strings (or other object types) if (s2.equalsIgnoreCase (s1)) compares the contents of two strings without considering case

Comparing Objects (cont.) all objects define or inherit an equals() method (however, the equals() method inherited from Object merely compares the references with ==)

switch Statement switch (integer expression) { case value1: // do something break; case value2: // do something break; case value3: // do something break; default: // do something } int paycode = …; boolean exempt; switch (paycode) { case 2: exempt = false; break; case 4: case 6: case 7: exempt = true; break; default: System.out.println (“Bad paycode”); }

Data Types in Switches byte char short int not long case keyword must be followed by a constant: –a literal, e.g, case 5: –a final variable (see next slide)

Use of Final Variables in Switch Statements int gender = …; switch (gender) { case 1: … break; case 2: … break; } final int MALE = 1; final int FEMALE = 2; int gender = …; switch (gender) { case MALE: … break; case FEMALE: … break }

Traps Forgetting the break statement switch (paycode) { case 2: exempt = false; case 4: case 6: case 7: exempt = true; default: System.out.println (“Bad paycode”); }

Conditional Operator (boolean expr.) ? value if true : value if false The boolean expression is evaluated; –if true, the result of the operation is the expression following the ? – if false, the result of the operation is the expression following the : Example int nHats = …; String word=(nHats != 1)? ” hats” :” hat”; System.out.println (“I have “ + nHats + word);

Conditional Operator (cont.) This is the same as: int nHats = …; String word; if (nHats != 1) word = ” hats”; else word = ” hat”; System.out.println (“I have “ + nHats + word);