Flow of Control Example Default Flow Conditional Statements

CS200 Computer Science I
Kevin Sahr, PhD
Lecture 9
Conditional Statements
1
Flow of Control
✴the order of statement execution
✴begins with first statement in main
method
✴default flow-of-control is top-to-bottom
2
2
Example
public class Sum
{
public static void main (String [] args)
{
double x, y;
double sum;
x = 32.5;
y = 9.4;
sum = x + y;
Output.showValue("The first number is ", x);
Output.showValue("The second number is ", y);
Output.showValue("The sum is ", sum);
} // method main
} // class Sum
3
3
Default Flow
4
4
Conditional Statements
✴we can change the flow of control using
conditional statements
✴conditional statements let us choose which
statement to execute next
✴the Java conditional statements are:
• if-statements
• if-else statements
• switch statements (covered in CS257)
5
5
Boolean Expressions
✴the choice of which statement to execute
next is determined by whether a particular
condition is true or false
✴something that can either be true or false
is called a boolean expression
✴for now we will use simple boolean
expressions involving numbers and
relational operators
6
6
Relational Operators
operator
meaning
<
less-than
>
greater-than
==
equal-to
!=
not-equal-to
<=
less-than-or-equal-to
>=
greater-than-or-equal-to
7
7
== vs. =
✴Note: == is not the same as =
• = is the assignment operator
is a relational operator meaning equal• ==
to
8
8
Boolean Expressions
✴syntax:
•
value1 relationalOp value2
✴relationalOp can be any one of the relational
operators
✴value1 and value2 can each be anything that
evaluates to a number
• a literal number (EX: 67.5 or -3)
• a variable of type double or int (EX: x or
numToes)
• an arithmetic expression (EX: x + 5.0)
9
9
Examples
✴the boolean expression x > 25 is either
true or false, depending on whether or not
the current value of x is greater than 25
✴the boolean expression a == b is either
true or false, depending on whether or not
the current value of a is equal to the
current value of b
10
10
The If-Statement
✴syntax:
•
boolean
expression
if (condition)
statement;
•
✴if the condition is true, execute the
statement. Otherwise skip it.
11
11
If-Statement
12
12
Example 9.1
✴
✴
✴
double width;
width = Input.readDouble(“Enter width: “);
if (width < 0.0)
Output.showMessage(“NEGATIVE WIDTH”);
Output.showMessage(“I am here.”);
✴
✴
• if user enters a negative width, outputs
NEGATIVE NUMBER then I am here.
• otherwise outputs just I am here
13
13
Example 9.2
✴
✴
int answer;
answer = Input.readInt(
“Enter the answer: “);
if (answer == 42)
Output.showMessage(“YES!”);
Output.showMessage(“Here I am.”);
✴
✴
✴
✴
• if user enters 42, outputs YES! then Here I
am.
• otherwise outputs just Here I am.
14
14
Example 9.3
✴
✴
double max = 100.0, length;
length = Input.readDouble(
“Enter the length: “);
if (length > max)
length = max;
Output.showValue(“length is ”, length);
✴
✴
✴
✴
• if user enters a number greater than max,
length is assigned the value max
• otherwise assigns length is unchanged
15
15
If Example Revisited
double max = 100.0, length;
length = Input.readDouble(
“Enter the length: “);
if (length > max)
length = max;
Output.showValue(“length is ”, length);
✴
✴
✴
✴
✴
✴
✴if user enters a number greater than max,
length is assigned the value max
• but shouldn’t we tell the user we changed it?
• requires more than one statement in the if
16
16
Block Statement
✴a block statement is several statements
grouped together with curly braces { }
✴can be used anywhere a single statement
can be used
✴for example, in an if-statement
✴remember to put a comment on your }
17
17
Example 9.4
✴
✴
double max = 100.0, length;
length = Input.readDouble(
“Enter the length: “);
if (length > max)
{
Block Statement
Output.showMessage(
“Warning: max length exceeded.”);
Output.showValue(“Length set to “, max);
length = max;
} // if
✴
✴
✴
✴
✴
✴
✴
✴
✴
Output.showValue(“length is ”, length);
18
18
Block Statement
Example
✴now if user enters a number greater than
max, two things happen:
• user is warned that a change was made
• length is assigned the value max
19
19
The If-Else Statement
✴syntax:
•
boolean
expression
if (condition)
statementA;
else
statementB;
•
•
•
✴if the condition is true, execute statementA.
Otherwise execute statementB.
✴one or the other is executed, never both
20
20
If-Else Statement
21
21
Example 9.2
✴outputs the smaller of two input numbers
✴in the case where the two numbers are
equal it doesn’t matter which one we choose
22
22
Example 9.2
✴
✴
✴
✴
double x, y, min;
x = Input.readDouble("Enter first num: ");
y = Input.readDouble("Enter second num: ");
if (x < y)
min = x;
else
min = y;
✴
✴
✴
✴
Output.showValue("Smaller number is: ", min);
23
23
If-Else and Block
Statements
✴the if-clause and else-clause statements in an
if-else statement may each be either single
statements or block statements
24
24
Example 9.3
✴
hours = Input.readDouble(
"Enter hours worked:");
rate = Input.readDouble("Enter pay rate:");
if (hours <= 40.0)
pay = hours * rate;
else
{
over = hours - 40.0;
pay = (40.0 * rate) + (over * 1.5 * rate);
Output.showMessage("OVERTIME WORKED");
} // else
Output.showValue(“Pay is: “, pay);
✴
✴
✴
✴
✴
✴
✴
✴
✴
✴
✴
25
25
Lecture 9 Vocabulary
flow of control
conditional statement
boolean expression
relational operator
block statement
if-else statement
26
26