Computer Programming 1: Making Decisions
Computer Programming 1: Making Decisions
Programming 1
Making Decisions
Learning Objectives
Learning Objectives
The simplest statement you can use to make a decision is the if statement.
An if statement is sometimes called a single-alternative selection because
there is only one alternative—the true alternative.
if(quizScore == 10)
System.out.println("The score is perfect");
The if Statement
The simplest statement you can use to make a decision is the if statement.
An if statement is sometimes called a single-alternative selection because
there is only one alternative—the true alternative.
if(quizScore == 10)
System.out.println("The score is perfect");
The if Statement
In the example, if quizScore holds the value 10, the Boolean value of the
expression quizScore == 10 is true, and the subsequent output statement
executes. If the value of the expression quizScore == 10 is false (meaning
that the 3/10 score is any value other than 10), the output statement does
not execute. As the flowchart segment shows, whether the tested
expression is true or false, the program continues and executes any
statements that follow the complete if statement.
The if Statement
if(quizScore == 10)
System.out.println("The score is perfect");
else
System.out.println("It's not perfect!");
The if…else Statement
When you execute an if…else statement, only one of the resulting actions
takes place depending on the evaluation of the Boolean expression. Each
statement, the one dependent on the if and the one dependent on the else,
is a complete statement, so each ends with a semicolon.
Nesting if and if…else
Statements
Nesting if and if…else Statements
In Java, you can combine Boolean tests into a single expression using the
logical AND and OR operators. Such an expression is a compound
Boolean expression or a compound condition.
The AND Operator
The AND Operator
For an alternative to some nested if statements, you can use the logical
AND operator between two Boolean expressions to create a compound
Boolean expression that is true when both of its operands are true. In
Java, the AND operator is written as two ampersands ( && ).
The AND Operator
When you want some action to occur even if only one of two conditions is
true, you can use nested if statements, or you can use the logical Or
operator, which is written as ||. The logical OR operator is used to create a
compound Boolean expression that is true when at least one of its
operands is true.
The OR Operator
The expressions on each side of the && and || operators are evaluated
only as far as necessary to determine whether the entire expression is true
or false. This feature is called short-circuit evaluation. With the &&
operator, both Boolean expression operands must be true before the action
in the result statement can occur. When you use the && operator, if the
first tested expression is false, the second expression is never evaluated
because its value does not matter.
Short-Circuit Evaluation
By nesting a series of if and else statements, you can choose from any
number of alternatives. An alternative to using the series of nested if
statements is to use the switch statement. The switch statement is useful
when you need to test a single variable against a series of exact integer
(including int, byte, and short types), character, or string values.
Using the switch Statement
switch(year) { System.out.println("Junior");
You are not required
case 1: break;
to list the case values
System.out.println("Freshman"); case 4: in ascending order,
break; System.out.println("Senior"); although doing so
case 2: break; often makes a
System.out.println("Sophomore"); default: statement easier to
break; System.out.println("Invalid year"); understand.
case 3: }
Using the Conditional and
NOT Operators
Using the Conditional and NOT Operators
Besides using if statements and switch statements, Java provides one more
way to make decisions. The conditional operator requires three
expressions separated with a question mark and a colon; it is used as an
abbreviated version of the if…else statement. As with the switch
statement, you are never required to use the conditional operator; it is
simply a convenient shortcut. The syntax of the conditional operator is:
testExpression ? trueResult : falseResult;
Using the Conditional and NOT Operators
For example, suppose you want to assign the smaller of two values, a and
b, to a variable named smallerNum. The expression you can use is:
smallerNum = (a < b) ? a : b;
Using the Conditional and NOT Operators
You use the NOT operator, which is written as the exclamation point ( ! ),
to negate the result of any Boolean expression. Any expression that
evaluates as true becomes false when preceded by the NOT operator, and
accordingly, any false expression preceded by the NOT operator becomes
true.