ECE 122. Engineering Problem Solving with Java

Similar documents
ECE 122. Engineering Problem Solving with Java

CS1004: Intro to CS in Java, Spring 2005

Comparing Data. Comparing Floating Point Values. Comparing Float Values. CS257 Computer Science I Kevin Sahr, PhD

Conditionals and Loops

Java Flow of Control

Program Planning, Data Comparisons, Strings

Chapter 4: Conditionals and Loops

Chapter 4: Conditionals and Loops

Lab 8: IF statement. Conditionals and Loops. Copyright 2012 Pearson Education, Inc.

Chapter 4: Conditionals and Loops

Program Development. Chapter 3: Program Statements. Program Statements. Requirements. Java Software Solutions for AP* Computer Science A 2nd Edition

Chapter 3: Program Statements

Program Development. Java Program Statements. Design. Requirements. Testing. Implementation

Conditionals and Loops Chapter 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

BRANCHING if-else statements

Topics. Chapter 5. Equality Operators

Full file at

COMP Primitive and Class Types. Yi Hong May 14, 2015

Decisions in Java IF Statements

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

Conditionals. For exercises 1 to 27, indicate the output that will be produced. Assume the following declarations:

Conditional Programming

More Programming Constructs -- Introduction

Flow of Control. Chapter 3

CMPT 125: Lecture 4 Conditionals and Loops

Getting started with Java

Logical Operators and switch

Flow of Control. Chapter 3

CS 115 Lecture 8. Selection: the if statement. Neil Moore

Fall 2017 CISC124 9/16/2017

CSC Java Programming, Fall Java Data Types and Control Constructs

Strings. Strings and their methods. Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

ECE 122. Engineering Problem Solving with Java

Java Fall 2018 Margaret Reid-Miller

ECE 122. Engineering Problem Solving with Java

Basic data types. Building blocks of computation

CMPT 125: Lecture 3 Data and Expressions

Visual C# Instructor s Manual Table of Contents

Selection Statements and operators

CS-201 Introduction to Programming with Java

PROGRAMMING FUNDAMENTALS

printf( Please enter another number: ); scanf( %d, &num2);

Datatypes, Variables, and Operations

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

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

Flow of Control. Chapter 3. Chapter 3 1

Programming with Java

Strings. Strings and their methods. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

COMP 111. Introduction to Computer Science and Object-Oriented Programming

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

Control Structures in Java if-else and switch

Number Systems Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur Number Representation

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

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

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

What we have learned so far

COMP2121: Microprocessors and Interfacing. Number Systems

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

Exam 1. CSC 121 Spring Lecturer: Howard Rosenthal. March 1, 2017

CompSci 125 Lecture 11

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Zheng-Liang Lu Java Programming 45 / 79

Object-Oriented Programming

ECE 122. Engineering Problem Solving Using Java

COMP-202: Foundations of Programming. Lecture 9: Arrays and Practice Jackie Cheung, Winter 2016

COMP-202: Foundations of Programming

Chapter 3. Selections

Control Structures in Java if-else and switch

StudyHub+ 1. StudyHub: AP Java. Semester One Final Review

Darrell Bethea May 25, 2011

CS 251 Intermediate Programming Java Basics

Computer Components. Software{ User Programs. Operating System. Hardware

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

CIS 110: Introduction to Computer Programming

Programming Projects: 2.1, 2.3, 2.4, 2.7, 2.8, 2.13

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

CS313D: ADVANCED PROGRAMMING LANGUAGE

Princeton University. Computer Science 217: Introduction to Programming Systems. Data Types in C

CS 177 Week 15 Recitation Slides. Review

Chapter 3: Operators, Expressions and Type Conversion

Selection as a Control Structure

1007 Imperative Programming Part II

Variables and Primitive Types

Language Basics. /* The NUMBER GAME - User tries to guess a number between 1 and 10 */ /* Generate a random number between 1 and 10 */

switch case Logic Syntax Basics Functionality Rules Nested switch switch case Comp Sci 1570 Introduction to C++

CHAPTER 2.1 CONTROL STRUCTURES (SELECTION) Dr. Shady Yehia Elmashad

Chapter 2 Part 2 Edited by JJ Shepherd, James O Reilly

Computational Expression

1.00 Lecture 4. Promotion

Programming. Syntax and Semantics

Java Identifiers, Data Types & Variables

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

Presented By : Gaurav Juneja

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

Topic 2: Making Decisions

ESc101 : Fundamental of Computing

Transcription:

ECE 122 Engineering Problem Solving with Java Lecture 8 More Conditional Statements Outline Problem: How do I make choices in my Java program? Understanding conditional statements Remember: Boolean logic The switch statement Allows for choices between many alternatives Important to understand how combinations of selection statements work together

The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression, then attempts to match the result to one of several possible cases Each case contains a value and a list of statements The flow of control transfers to statement associated with the first case value that matches The switch Statement The general syntax of a switch statement is: switch and case are reserved words switch ( expression ) { case value1 : statement-list1 case value2 : statement-list2 case value3 : statement-list3 case... } If expression matches value2, control jumps to here

The switch Statement Often a break statement is used as the last statement in each case's statement list A break statement causes control to transfer to the end of the switch statement If a break statement is not used, the flow of control will continue into the next case Sometimes this may be appropriate, but often we want to execute only the statements associated with one case The switch Statement An example of a switch statement: switch (option) { case 'A': acount++; break; case 'B': bcount++; break; case 'C': ccount++; break; } Assumes we have a character variable named option that has a character value as in char option = C ;

The switch Statement A switch statement can have an optional default case The default case has no associated value and simply uses the reserved word default If the default case is present, control will transfer to it if no other case value matches if there is no default case, and no other value matches, control falls through to the statement after the switch The switch Statement The expression of a switch statement must result in an integral type, meaning an int or a char It cannot be a boolean value, a floating point value (float or double), or another integer type The implicit boolean condition in a switch statement is equality You cannot perform relational checks with a switch statement

Comparing Data Comparing data using boolean expressions Important to understand the nuances of certain data types Let's examine some key situations: Comparing floating point values for equality Comparing characters Comparing strings (alphabetical order) Comparing object vs. comparing object references Comparing Float Values Don t use the equality operator (==) when comparing two floating point values (float or double) Two floating point values are equal only if their underlying binary representations match exactly Computations often result in slight differences that may be irrelevant Consider two floating point numbers to be "close enough" even if they aren't exactly equal Determine absolute value and perform comparison

Comparing Float Values To determine the equality of two floats, you may want to use the following technique: if (Math.abs(f1 - f2) < TOLERANCE) System.out.println ("Essentially equal"); If the difference between the two floating point values is less than the tolerance, they are considered to be equal The tolerance could be set to any appropriate level, such as 0.000001 Comparing Characters As we've discussed, Java character data is based on the Unicode character set Unicode establishes a particular numeric value for each character, and therefore an ordering We can use relational operators on character data based on this ordering For example, the character '+' is less than the character 'J' because it comes before it in the Unicode character set Appendix C provides an overview of Unicode Look at this Appendix!!

Comparing Characters In Unicode, the digit characters (0-9) are contiguous and in order Likewise, the uppercase letters (A-Z) and lowercase letters (a-z) are contiguous and in order Characters 0 9 A Z a z Unicode Values 48 through 57 65 through 90 97 through 122 Comparing Strings Remember that in Java a character string is an object The equals method can be called with strings to determine if two strings contain exactly the same characters in the same order The equals method returns a boolean result Assuming name1 and name2 are objects of type String and already have values, we can say: if (name1.equals(name2)) System.out.println ("Same name"); Note the syntax!!! This is how you do it.

Comparing Strings We cannot use the relational operators to compare strings The String class contains a method called compareto to determine if one string comes before another (like if we are sorting or alphabetizing ) A call to name1.compareto(name2) returns zero if name1 and name2 are equal (contain the same characters) returns a negative value if name1 is less than name2 returns a positive value if name1 is greater than name2 Comparing Strings VIP Example!! if (name1.compareto(name2) < 0) System.out.println (name1 + "comes first"); else if (name1.compareto(name2) == 0) System.out.println ("Same name"); else System.out.println (name2 + "comes first"); Because comparing characters and strings is based on a character set, it is called a lexicographic ordering

Lexicographic Ordering Lexicographic ordering is not strictly alphabetical when uppercase and lowercase characters are mixed For example, the string "Great" comes before the string "fantastic" All of the uppercase letters come before all of the lowercase letters in Unicode Also, short strings come before longer strings with the same prefix (lexicographically) "book" comes before "bookcase Characters are compared one-by-one until a difference is discovered!! Comparing Objects The == operator can be applied to objects It returns true if the two references are aliases of each other The equals method is defined for all objects It performs the same operation as the == operator It has been redefined in the String class to compare the characters in the two strings When you write a class, you can redefine the equals method Specify conditions which result in true

Summary Conditionals form an important part of programming Essential for making choices Conditional statements rely on the evaluation of Boolean expressions Switch statements allow for evaluation of many cases Important to remember break and default Comparing some primitives (float, char, and double) and objects can be tricky Practice comparing these objects!