You are on page 1of 30

Course : Introduction to Programming

Effective Period : September 2015

Selection Statement
Session 07
Outline
• Selection control structure
• If statement
• If-else Statement
• Nested-if Statement
• Switch-case Statement
• Conditional Statement
• String Selection

Bina Nusantara University 2


Selection Statement
• Choose actions with two or more alternative courses.
• Use conditions, which are Boolean expressions.
• Java provides several types of selection statements:
– One-way if statements
– Two-way if statements
– Nested if statements
– switch statements
– Conditional expressions
• The Statement inside Selection Statements is executed
when the condition is fulfilled.

Bina Nusantara 3
One-way if Statements
• Syntax:
if (Boolean-expression)
{
statement(s);
}

• Execution flow chart:

Bina Nusantara 4
Example of One-way if Statement

The Flow chart:

Bina Nusantara 5
Did You Know?
• The block braces can be omitted if they enclose a single
statement.
• E.g.

Bina Nusantara 6
Two-Way if Statements
• When you have to use it?
– If you want to take alternative actions when the
condition is false.
• Syntax:
if (Boolean-expression){
statement(s)-for-the-true-case;
}
else {
statement(s)-for-the-false-case
}

Bina Nusantara 7
Simple Two-Way if Statements
if (number % 2 == 0){
System.out.println(number + “ is even. “);
}
else {
System.out.println(number + “ is odd. “);
}

Bina Nusantara 8
Example of Two-Way if Statements (1)

Bina Nusantara 9
Example of Two-Way if Statements (2)
• Incorrect Output :

• Correct Output :

• Note:
– Try … Catch is useful for exception testing (try) and catching an
exception when found (catch).
– As depicted in example above, statement inside catch is executed
when character or word is entered.
– Try .. Catch is explained later at Exception Handling discussion.

Bina Nusantara 10
Nested if Statement
• Means there is an “if” or an “if-else” inside an “if” or an “if-else”.
• There is no limit to the depth of the nesting.
• E.g.

Equivalent

This is better

Bina Nusantara 11
Nested-if Statement

Bina Nusantara
Nested-if Statement

Bina Nusantara
Nested-if statement

Incorrect Output :

Correct Output :
Common Errors in Selection
Statements (1)
• Forgetting necessary braces

• Wrong semicolon at the if line


Logic error

• Redundant testing of Boolean values


This is better

Bina Nusantara Equivalent 15


Common Errors in Selection
Statements (2)
• Dangling else Ambiguity

This is better with correct indentation

Bina Nusantara 16
Switch Statements
• Switch-case executes block by its status.
• Syntax:
switch (switch-expression)
{
case value1: statement(s)1;
break;
case value2: statement(s)2;
break;

case valueN: statement(s)N;
break;
default: statement(s)-for-default;
}

Bina Nusantara 17
Switch Statements Flow Chart
status 1
Statement(s)1 break

status 2
Statement(s)2 break

status 3
Statement(s)3 break

status 4
Statement(s)4 break

default
Default actions

Bina Nusantara 18
Switch Statements Procedures
• The switch-expression must yield a value of char, byte,
short, or int type.
• The value, …, and valueN must have the same data type
as the value of switch-expression.
• The statements are executed until either break statement
or the end of switch statement is reached.
• The keyword break is optional.
• The default case is optional.
• The case statements are checked in sequential order.

Note: They keyword break is explained in detail in the next session

Bina Nusantara 19
Caution!
• Do not forget to use a break statement when one is
needed.

status 1
Statement(s)1

status 2
Statement(s)2

status 3
Statement(s)3

status 4
Statement(s)4

default
Default actions

Bina Nusantara 20
Sample of switch Statement

Bina Nusantara 21
Conditional Expressions
• A completely different style, with no explicit if in the
statement.
• Form?
– Boolean-expression ? expression1 : expression2;
• E.g.

Use the conditional Expression

Bina Nusantara 22
Did You Know?

Bina Nusantara
Did You Know?
Incorrect Output :

Correct Output :

Bina Nusantara
Advanced Learning
• String comparison can be done by using “equals” methods
• Syntax:
String1.equals(String2);
Its return value is boolean.
If content/word at String1 is equal to String2 then
True.
If not, False

• Example:
String word1 = “hello”;
String word2 = “hello”;

System.out.println(“Is the word1 and word2 same? “


+ word1.equals(word2));

Bina Nusantara
Advanced Learning

Bina Nusantara
Advanced Learning
if(word1.equals(word2)==true)
can be changed to
if(word1.equals(word2))
“if” can execute the block when its
parameter is true.

Bina Nusantara
Exercise
1. Suppose x = 3 and y = 2; show the output, if any, of the
following code. What is the output if x = 3 and y = 4? What is
the output of x = 2 and y = 2?

2. What is y after the following switch statement is executed?

Bina Nusantara University 28


Exercise
3. Use a switch statement to rewrite the following if statement

4. Use a ternary operator to rewrite the following if statement

Bina Nusantara University 29


References
• Introduction to Java Programming. 8ed. Liang. 2011.
Chapter 3
• Dasar Pemrograman Java 2. Abdul Kadir. 2004. p83-118
• Java Software Solutions. 5ed. Lewis & Loftus. 2007.
p238-262
• The Complete Reference Java. 5ed. Herbert Schildt.
2005. p77-84
• Java 2 Weekend Crash Course. Julio. 2002. Ch 8
• Java Control Flow Statements:
http://www.javabeginner.com/java-control-statements.htm

Bina Nusantara

You might also like