You are on page 1of 12

Fundamentals of Java

◆ Identify the need for decision-making statements


◆ List the different types of decision-making statements
◆ Explain the if statement
◆ Explain the various forms of if statement
◆ Explain the switch-case statement
◆ Explain the use of strings and enumeration in the
switch-case statement
◆ Compare the if-else and switch-case statement

© Aptech Ltd. Decision-Making Constructs/Session 4 2


◆ Enable us to change the flow of execution of a Java program.
◆ Evaluates a condition and based on the result of evaluation, a
statement or a sequence of statements is executed.
◆ Different types of decision-making statements supported by Java
are as follows:

if Statement Switch-case
Statement

© Aptech Ltd. Decision-Making Constructs/Session 4 3


◆ is the most basic form of decision-making statement.
◆ evaluates a given condition and based on the result of evaluation
executes a certain section of code.
◆ If the condition evaluates to true, then the statements present
within the if block gets executed.
◆ If the condition evaluates to false, the control is transferred
directly to the statement outside the if block.

Syntax
if (condition) {

// one or more statements;

© Aptech Ltd. Decision-Making Constructs/Session 4 4


Syntax
if (condition) {
// one or more statements;
}
else {
// one or more statements;
}

© Aptech Ltd. Decision-Making Constructs/Session 4 5


Syntax

if(condition) {

if(condition)
true-block statement(s);
else
false-block statement(s);
}

else {
false-block statement(s);
}

© Aptech Ltd. Decision-Making Constructs/Session 4 6


Syntax
if(condition) {
// one or more statements
}

else if (condition) {
// one or more statements
}

else {
// one or more statements
}

© Aptech Ltd. Decision-Making Constructs/Session 4 7


◆ Alternative for too many if statements representing multiple selection
constructs.
◆ Contains a variable as an expression whose value is compared against
different values.
◆ Results in better performance.
◆ Can evaluate different primitive data types, such as byte, short, int, and
char.

Enhancements to switch-case statement in Java SE 7

• Supports the use of strings in the switch-case statement.


• Supports use of objects from classes present in the Java API.
• The classes whose objects can be used are Character, Byte,
Short, and Integer.
• Supports the use of enumerated types as expression.

© Aptech Ltd. Decision-Making Constructs/Session 4 8


Syntax
switch (<expression>) {
case value1:
// statement sequence
break;
case value2:
// statement sequence
break;
. . .
. . .
. . .
case valueN:
// statement sequence
break;
default:
// default statement sequence
}

© Aptech Ltd. Decision-Making Constructs/Session 4 9


◆ The constraint with an enum expression is that:
 All case constants must belong to the same enum variable
◆ For example:
public class TestSwitchEnumeration {
enum Cards { Spade, Heart, Diamond, Club }
public static void main(String[] args) {
Cards card = Cards.Diamond;
switch (card) {
case Spade:
System.out.println(“SPADE”); break;
case Heart:
System.out.println(“HEART”); break;
case Diamond:
System.out.println(“DIAMOND”); break;
case Club:
System.out.println(“CLUB”); break;
}
}
}

© Aptech Ltd. Decision-Making Constructs/Session 4 10


if switch-case
Each if statement has its own Each case refers back to the original value
logical expression to be evaluated of the expression in the switch statement
as true or false
The variables in the expression The expression must evaluate to a byte,
may evaluate to a value of any type short, char, int, or String
Only one of the blocks of code is If the break statement is omitted, the
executed execution will continue into the next block

© Aptech Ltd. Decision-Making Constructs/Session 4 11


◆ A Java program is a set of statements, which are executed sequentially in the
order in which they appear.
◆ The three categories of control flow statements supported by Java programming
language include: conditional, iteration, and branching statements.
◆ The if statement is the most basic decision-making statement that evaluates a
given condition and based on result of evaluation executes a certain section of
code.
◆ The if-else statement defines a block of statements to be executed when a
condition is evaluated to false.
◆ The multiple if construct is known as the if-else-if ladder with conditions evaluated
sequentially from the top of the ladder.
◆ The switch-case statement can be used as an alternative approach for multiple
selections. It is used when a variable needs to be compared against different
values. Java SE 7 supports strings and enumerations in the switch-case statement.
◆ A switch statement can also be used as a part of another switch statement. This is
known as nested switch-case statements.

© Aptech Ltd. Decision-Making Constructs/Session 4 12

You might also like