Docsity

Java Control Statements: Conditional, Switch, DO, Infinite Loop, Compare Floats, Strings, , Study notes of Data Structures and Algorithms

Toggle sidebar
bg1
Less-Known Java Control Statements
The Conditional operator ? :
The SWITCH statement
The DO loop
The infinite loop
The iterated FOR loop
4.2 – Java’s Conditional Operator
Its syntax is
condition ? expression1 : expression2
If the condition is true, expression1 is evaluated;
if it is false, expression2 is evaluated
The value of the entire conditional operator
is the value of the sele cted expression
1-2
System.out.println ("Your change is " +
count + ((count == 1) ? "Dime" : "Dimes"));
4.3 – Comparing Data
When comparing data using boolean expressions,
it's important to unders tand the nuances of certain data types
Comparing integers is straight forward but how about…
comparing floating point values for equality
comparing characters
comparing strings (alpha betical order)
comparing object vs. compar ing object references
4.3 – Comparing Floats and Doubles
if (Math.abs(f1 - f2) < TOLERANCE)
System.out.println ("Essentially equal");
• If the difference betwe en the two floating point values
is less than the TOLERANCE,
they are considered to be equal
•TOLERANCE could be set to a n appropriate level, i.e. 0.000001
You should never use the equalit y operator (==)
when comparing two floa ting point values (float or double)
Two floating point values are equal only if
their underlying binary representations match exactly
To determine the equality of two floats,
you may want to use the following technique
pf3
pf4

Partial preview of the text

Download Java Control Statements: Conditional, Switch, DO, Infinite Loop, Compare Floats, Strings, and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Less-Known Java Control Statements

The Conditional operator? :

The SWITCH statement

The DO loop

The infinite loop

The iterated FOR loop

4.2 – Java’s Conditional Operator

Its syntax is

condition? expression1 : expression

If the condition is true, expression1 is evaluated;

if it is false, expression2 is evaluated

The value of the entire conditional operator

is the value of the selected expression

1- 2

System.out.println ("Your change is " +

count + ((count == 1)? "Dime" : "Dimes"));

4.3 – Comparing Data

When comparing data using boolean expressions,

it's important to understand the nuances of certain data types

Comparing integers is straight forward but how about…

 comparing floating point values for equality  comparing characters  comparing strings (alphabetical order)  comparing object vs. comparing object references

4.3 – Comparing Floats and Doubles

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

  • TOLERANCE could be set to an appropriate level, i.e. 0. You should never 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 To determine the equality of two floats, you may want to use the following technique

4.3 – Comparing chars

Java characters are based on the Unicode character set

Unicode establishes a particular numeric value

for each character, and therefore an ordering

For example, the character '+' is less than 'J'

because it comes before it in the Unicode character set

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^ Unicode Values

0 – 9 48 through 57

A – Z 65 through 90

a – z 97 through 122

4.3 – 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

if (name1.equals(name2))

System.out.println ("Same name");

4.3 – Lexicographic Order

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"); Lexicographic ordering is not strictly alphabetical when uppercase and lowercase characters are mixed For example, "Great" comes before the string "fantastic” Also, short strings come before longer strings with the same prefix Therefore "book" comes before "bookcase"

4.3 – 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,

but unless we redefine it when we write a class,

it has the same semantics as the == operator

== has been redefined in the String class

to compare the characters in the two strings

When you write a class,

you should redefine the equals method

to return true under whatever conditions are appropriate

Another Infinite Loop 4.6 – Iterators

An iterator is an object that allows you

to process a collection of items one at a time.

An iterator object has a hasNext method

that returns true if there is at least one more item to process

The next method returns the next item

The Scanner class is an iterator

 the hasNext method returns true if there is more data to be scanned  the next method returns the next scanned token as a string

The Scanner class also has variations on the hasNext method

for specific data types (such as hasNextInt)

4.6 URLDissector.java

fileScan = new Scanner (new File("websites.inp")); // Read and process each line of the file while (fileScan.hasNext()) { url = fileScan.nextLine(); System.out.println ("URL: " + url); urlScan = new Scanner (url); urlScan.useDelimiter("/"); // Print each part of the url while (urlScan.hasNext()) System.out.println (" " + urlScan.next()); System.out.println(); } } }

4.8 – Iterators and for Loops

A variant of the for loop simplifies the repetitive processing the

items

For example, if BookList is an iterator that manages Book objects,

the following loop will print each book:

This style of for loop can be read "for each Book in BookList, …"

Therefore the iterator version of the for loop

is sometimes referred to as the foreach loop

It eliminates the need to call the hasNext and next methods

explicitly

for (Book myBook : BookList)

System.out.println (myBook);