









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Professor: Adjeroh; Class: Introduction-Computer Science; Subject: Computer Science; University: West Virginia University; Term: Unknown 1989;
Typology: Study notes
1 / 16
This page cannot be seen from the preview
Don't miss anything!










Java Programming: From Problem Analysis to Program Design, Second Edition 1
Java Programming: From Problem Analysis to Program Design, Second Edition 2
Java Programming: From Problem Analysis to Program Design, Second Edition 3
Java Programming: From Problem Analysis to Program Design, Second Edition 4
Java Programming: From Problem Analysis to Program Design, Second Edition 7
Java Programming: From Problem Analysis to Program Design, Second Edition 8
< <
0 str1 str
0 str1 str
0 str1 str str1.compareTo(str2) aninteger if string
ifstring isequaltostring
aninteger ifstring
Java Programming: From Problem Analysis to Program Design, Second Edition 9
Comparing Strings String str1 = "Hello"; String str2 = "Hi"; String str3 = "Air"; String str4 = "Bill"; String str5 = "Bigger";
Java Programming: From Problem Analysis to Program Design, Second Edition 10
Comparing Strings
Java Programming: From Problem Analysis to Program Design, Second Edition 13
Java Programming: From Problem Analysis to Program Design, Second Edition 14
Java Programming: From Problem Analysis to Program Design, Second Edition 15
Java Programming: From Problem Analysis to Program Design, Second Edition 16
Syntax: if (expression) statement Expression referred to as decision maker. Statement referred to as action statement.
Java Programming: From Problem Analysis to Program Design, Second Edition 19
Java Programming: From Problem Analysis to Program Design, Second Edition 20
Java Programming: From Problem Analysis to Program Design, Second Edition 21
Two-Way Selection
Example 4-
if (hours > 40.0) wages = 40.0 * rate + 1.5 * rate * (hours - 40.0); else wages = hours * rate;
Java Programming: From Problem Analysis to Program Design, Second Edition 22
Example 4-
if (hours > 40.0); //Line 1 wages = 40.0 * rate + 1.5 * rate * (hours - 40.0); //Line 2 else //Line 3 wages = hours * rate; //Line 4
Because a semicolon follows the closing parenthesis of the if statement (Line 1), the else statement stands alone. The semicolon at the end of the if statement (see Line 1) ends the if statement, so the statement at Line 2 separates the else clause from the if statement. That is, else is by itself. Because there is no separate else statement in Java, this code generates a syntax error.
Two-Way Selection
Java Programming: From Problem Analysis to Program Design, Second Edition 25
expression1? expression2 : expression
Java Programming: From Problem Analysis to Program Design, Second Edition 26
Syntax:
if (expression1) statement else if (expression2) statement else statement
Else is associated with the most recent incomplete if. Multiple if statements can be used in place of if…else statements. May take longer to evaluate.
Java Programming: From Problem Analysis to Program Design, Second Edition 27
switch (expression) { case value1: statements break; case value2: statements break; ... case valuen: statementsn break; default: statements }
Java Programming: From Problem Analysis to Program Design, Second Edition 28
Java Programming: From Problem Analysis to Program Design, Second Edition 31
Java Programming: From Problem Analysis to Program Design, Second Edition 32