0% found this document useful (0 votes)
93 views

Computer Programming 1: Making Decisions

1) The document discusses different methods for making decisions in Java code including if statements, if-else statements, nested if statements, switch statements, logical operators like AND and OR, and the conditional operator. 2) If statements execute code if a boolean expression is true, if-else statements allow different code to execute if the expression is false, and nested if statements allow decisions within other decisions. 3) Switch statements allow checking a variable against multiple possible values, and logical operators combine boolean expressions.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views

Computer Programming 1: Making Decisions

1) The document discusses different methods for making decisions in Java code including if statements, if-else statements, nested if statements, switch statements, logical operators like AND and OR, and the conditional operator. 2) If statements execute code if a boolean expression is true, if-else statements allow different code to execute if the expression is false, and nested if statements allow decisions within other decisions. 3) Switch statements allow checking a variable against multiple possible values, and logical operators combine boolean expressions.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 42

Computer

Programming 1

Making Decisions
Learning Objectives
Learning Objectives

At the end of the chapter, students are expected to:


a) Make decisions with if and if…else structure.
b) Familiarized with the used of multiple statements and Nest if or if..
else structure
c) Familiarized with the used of switch statement.
d) Use the conditional, AND, OR and Not operators
The if and if…else Statements
The if and if…else Statements

In Java, when you want to take an action if a Boolean expression is true,


you use an if statement. If you want to take an action when a Boolean
expression is true but take a different action when the expression is false,
you use an if…else statement.
The if Statement
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

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

For example, suppose you have declared an integer variable named


quizScore, and you want to display a message when the value of
quizScore is 10. The if statement makes the decision whether to produce
output.

Note that the double equal sign ( == ) is used to determine


equality; it is Java’s equivalency operator.
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

A Java if statement always includes parentheses. Within the parentheses,


you can place any Boolean expression. Most often you use a comparison
that includes one of the relational operators (<,>,==). However, you can
use any expression that evaluates as true or false, such as a simple boolean
variable or a call to a method that returns a boolean value.
The if…else Statement
The if…else Statement

In Java, the if…else statement provides the mechanism to perform one


action when a Boolean expression evaluates as true and a different action
when a Boolean expression evaluates as false. In other words, you use an
if…else statement for a dual-alternative selection. For example, you
would use an if…else statement if you wanted to display one message
when the value of quizScore is 10 and a different message when it is not.
The if…else 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

Within an if or an else clause, you can code as many dependent statements


as you need, including other if and else statements. Statements in which a
decision is contained inside either the if or else clause of another decision
are nested if statements. Nested if statements are particularly useful when
two or more conditions must be met before some action is taken.
Nesting if and if…else Statements

if(itemsSold >= MIN_ITEMS)


if(totalValue >= MIN_VALUE)
bonus = SALES_BONUS;
Using Logical AND and OR
Operators
Using Logical AND and OR Operators

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

if(itemsSold > MIN_ITEMS)


if(totalValue >= MIN_VALUE)
bonus = SALES_BONUS;

if(itemsSold > MIN_ITEMS && totalValue >= MIN_VALUE)


bonus = SALES_BONUS;
The OR Operator
The OR 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

if(itemsBought >= MIN_ITEMS)


discountRate = DISCOUNT;
else
if(itemsValue >= MIN_VALUE)
discountRate = DISCOUNT;

if(itemsBought >= MIN_ITEMS || itemsValue >= MIN_VALUE)


discountRate = DISCOUNT;
Short-Circuit Evaluation
Short-Circuit Evaluation

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

The || operator also uses short-circuit evaluation. In other words, because


only one of the Boolean expressions in an || expression must be true to
cause the dependent statements to execute, if the expression to the left of
the || is true, then there is no need to evaluate the expression to the right of
the ||. However, when you use the || operator, if the first tested expression
is false, then the second expression must be evaluated.
Using the switch Statement
Using the switch Statement

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

The switch statement uses four keywords:


● switch starts the statement and is followed immediately by a test
expression enclosed in parentheses.
● case is followed by one of the possible values for the test expression
and a colon.
● break optionally terminates a switch statement at the end of each case.
● default optionally is used prior to any action that should occur if the
test variable does not match any case.
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

The first expression, testExpression, is a Boolean expression that is


evaluated as true or false. If it is true, the entire conditional expression
takes on the value of the expression following the question mark
(trueResult). If the value of the testExpression is false, the entire
expression takes on the value of 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

When evaluating the expression a < b, if a is less than b, the entire


conditional expression takes the value to the left of the colon, a, which
then is assigned to smallerNum. If a is not less than b, then the expression
assumes the value to the right of the colon, and b is assigned to
smallerNum.
Using the Conditional and NOT Operators

You could achieve the same results with the following


if…else statement:
if(a < b)
smallerNum = a;
else
smallerNum = b;
Using the Conditional and NOT Operators

The disadvantage to using the conditional operator is that, at least at first,


it is more difficult to read. The advantage is the conciseness of the
statement.
Using the NOT Operator
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.

You might also like