Transcript Document
CIS 260: App Dev I
Three Basic Structures
Sequence
do A, do B, do C, …
A, B, and C are basic methods or operations
Selection (a.k.a., ________)
if A, do B; else, do C
A is a ___________ that is either true or false
A could be compound, such as (D and E) or (D or E)
Repetition (a.k.a., _______)
while A, do B
A is also a condition
Selection and loop are called _________
structures since they can alter sequential flow
2
Relational Operators
An expression is simply a statement of action,
such as (x + 7) or (i > j)
A condition is an expression that is either true
or false (a.k.a., a logical or _________
expression)
In Java, a relational operator is one that allows
comparison of __________ data types
Relational operators: ==, !=, <, <=, >, >=
Conditions: 5 == 4, x != 7, i < j, ‘6’ <= ‘>’
Because of rounding, comparing floating points
in Java can be tricky (see Example 4-1)
Comparing chars is based on _______ values3
Comparing Strings
In Java, strings are compared character by
character until a mismatch is found.
Assume the following Java statements:
String
String
String
String
string1
string2
string3
string4
=
=
=
=
“Hello”;
“hello”;
“Bill”;
“Air”;
Use the compareTo method of the ________
class
string1.compareTo( string2 ) evaluates to -32
string1.compareTo( “Hello” ) evaluates to ____
string3.compareTo( string4 ) evaluates to 1
string3.compareTo( “Billy” ) evaluates to ____
4
String Equality
You can use the equals method of the String
class
String string1 = “Hello”;
String string2 = “Hello”;
string1.equals( string2 ); // evaluates to true
string1 == string2; // evaluates to _______
String string2 = “Hi”;
string1.equals( string2 ); // evaluates to
//______
5
Logical Operators and Expressions
Logical (or Boolean) operators in Java
! expression
• ! means ______
• true becomes false, false becomes true
expression1 && expression2
• && means ______
• both expressions must be true for the whole to be true
expression1 || expression2
• || means ______
• both expressions must be false for the whole to be false
Examples
(5 < 7) && !(‘A’ > ‘B’) is _______
(5 < 7) || (‘A’ > ‘B’) is _______
!(5 < 7) || (‘A’ > ‘B’) is _______
6
Precedence of Operators in Java
!, +, - (unary operators)
*, /, %
+, - (binary operators)
<, <=, >, >=
==, !=
&&
||
= (assignment)
Short-circuit evaluation of compound expression
Evaluation is left to right—stops when result is known
(5 < 7) || (‘A’ > ‘B’) stops after first comparison
Using & , | instead of && , || disables short-circuit 7
if and if…else
In Java, there are two selection structures:
if statements
_________ statements
The syntax of a one-way selection is
if( logical expression )
action statement
Example
if( score >= 90 )
T
expression
statement
F
grade = ‘A’;
The expression is called the ________ maker.
If the expression is ______, the statement is
executed; otherwise the statement is skipped. 8
Two-Way Selection
Two-way selection allows an alternative action
statement if the expression is ______.
The syntax of a two-way selection is
if( logical expression )
action statement1
F
else
action statement2
Statement 2
T
expression
Statement 1
Example
if( hours > 40.0 )
wages = 40.0 * rate + 1.5 * rate*(hours–40.0);
else
wages = hours * rate;
9
Compound Statements (Block)
A compound statement in Java is a series of
statements enclosed in ____.
Example:
if( hours > 40.0 )
{
wages = 40.0 * rate + 1.5 * rate*(hours–40.0);
tax = wages * .15;
}
else
{
wages = hours * rate;
tax = wages * .10;
}
10
Multiple Selections: Nested if
Example:
if( balance > 50000.00 )
interestRate = 0.07;
else
if( balance > 25000.00 )
interestRate = 0.05;
else
if( balance > 1000.00 )
interestRate = 0.03;
else
interestRate = 0.00;
Java associates an else with the most recent
incomplete ______.
11
switch Structures
switch allows a choice from many alternatives.
Syntax of switch:
switch( expression )
{
case value1: statement1
break;
case value2: statement2
break;
…
case valuen: statementn
break;
default: statements
}
T
case 1
F
Statement 1
break
Statement 2
break
Statement 3
break
T
case 2
F
T
case 3
F
default
The expression is called the __________ and
values are called ________.
expression and valuen must evaluate to an
12
_________.