CSE110: Object Oriented
Programming in Java
Selection
Rabea Khatun
Lecturer
Dept. Of CSE
East West University
Rabea Khatun, Lecturer,CSE, East West University 1
Control Statement
There are three control statement in java,
▪ Selection statement.
▪ If
▪ Switch
▪ Iteration statement.
▪ While
▪ Do-while
▪ For
▪ Jump statement.
▪ Break
▪ Continue
Rabea Khatun, Lecturer,CSE, East West University 2
Control Statement(Selection)
A Selection (conditional) statement allows us to choose which
statement (or block of statements) will be executed next.
Java selection statements are:
if statement - allows one option
if-else statement - allows two options
switch statement - allows multiple options
Rabea Khatun, Lecturer,CSE, East West University 3
If statement
• The if statement has the following syntax:
The condition must be a
boolean expression. It must
if is a Java evaluate to either true or false.
reserved
word
if (condition)
{
statementBlock;
};
If the condition is true, the statementBlock is
executed.
If it is false, the statementBlock is skipped.
Rabea Khatun, Lecturer,CSE, East West University 4
If statement
Statement
1 grade = 70;
2 If (grade>= 90)
3 System.out.println("You got an
condition "A");
evaluated 4 System.out.println("This is line 4");
tru fals
e e
Statement Block
1 grade = 95;
2 If (grade>= 90)
3 System.out.println("You got an
"A");
Statement 4 System.out.println("This is line 4");
Rabea Khatun, Lecturer,CSE, East West University 5
Boolean Expressions
A condition often uses one of Java's equality operators or
relational operators, which all return boolean results:
== equal to
!= not equal to
< less than
> greater than
<= less than or equal to
>= greater than or equal to
Note the difference between the equality operator (==) and
the assignment operator (=)
Rabea Khatun, Lecturer,CSE, East West University 6
Boolean Expressions
• Expressions that use logical operators can form
complex conditions
if (total < MAX + 5 && !found)
System.out.println ("Processing…");
• Mathematical operators have higher precedence
than the Relational and Logical operators
• Relational operators have higher precedence than
Logical operators
Rabea Khatun, Lecturer,CSE, East West University 7
Boolean Expressions
• Specific expressions can be evaluated using truth
tables
• Given X = total < MAX && !found
What is the values of X ?
total < MAX !found X = total < MAX && !found
true true true
true false false
false true false
false false false
Rabea Khatun, Lecturer,CSE, East West University 8
Operator Precedence
+, - unary operators
(type) Casting and parenthesis
! Not
*, /, % Math operators
+, - Math operators
<, <=, >, >= Relational operators
==, != Relational equality
^ Exclusive OR
&& Logical AND
|| Logical OR
=, +=, -=, *=, /=, %= Assignment
operators
Rabea Khatun, Lecturer,CSE, East West University 9
Operator Precedence
Operator Precedence
Applying operator precedence and associativity rule to
the expression: 3 + 4 * 4 > 5 * (4 + 3) - 1
Rabea Khatun, Lecturer,CSE, East West University 10
Operator
ThePrecedence
if-else Statement
• An else clause can be added to an if statement to
make an if-else statement
if ( condition )
statementBlock1;
else
statementBlock2;
• If the condition is true, statementBlock1 is
executed; if the condition is false, statementBlock2
is executed
• One or the other will be executed, but not both
Rabea Khatun, Lecturer,CSE, East West University 11
Operator
ThePrecedence
if-else Statement
Statement
condition
evaluated
tru fals
e e
StatementBlock1 StatementBlock2
Statement
Rabea Khatun, Lecturer,CSE, East West University 12
The if-else Statement
Rabea Khatun, Lecturer,CSE, East West University 13
Else-if ladder
Else-if ladder/chain
if(condition) statement 1
else if(condition) statement 2
.
.
.
else statement
Rabea Khatun, Lecturer,CSE, East West University 14
Else-if ladder
Rabea Khatun, Lecturer,CSE, East West University 15
Operator Precedence Operator
The Conditional
Java has a conditional operator that uses a boolean condition to
determine which of two expressions is evaluated
Its syntax is:
condition ? expression1 : expression2
If the condition is true, expression1 is evaluated; if it is
false, expression2 is evaluated
The conditional operator is ternary because it requires three
operands
Rabea Khatun, Lecturer,CSE, East West University 16
Operator Precedence Operator
The Conditional
The conditional operator is similar to an if-else statement,
except that it is an expression that returns a value
For example:
larger = ((num1 > num2) ? num1 :
num2);
If num1 is greater than num2, then num1 is assigned to
larger; otherwise, num2 is assigned to larger
Same as if (num1 > num2)
larger = num1;
else
larger = num2;
Rabea Khatun, Lecturer,CSE, East West University 17
Operator Precedence Operator
The Conditional
• Another example:
System.out.println ("Your change is " + count +
((count == 1) ? "Dime" : "Dimes"));
• If count equals 1, then "Dime" is printed
• If count is anything other than 1, then "Dimes" is
printed
Rabea Khatun, Lecturer,CSE, East West University 18
Operator Precedence
Nested if Statements
The statement
Nested If-else
executed as a result of
an if statement or if(condition)
else clause could be {
if(condition) statement 1
another if statement else statement 2
}
Else
{
if(condition)
.
.
}
Rabea Khatun, Lecturer,CSE, East West University 19
Operator Precedence
Nested if Statements
// MinOfThree.java
import java.util.Scanner;
public class MinOfThree
{
public static void main (String[] args)
{ int num1, num2, num3, min = 0;
Scanner scan = new Scanner (System.in);
System.out.println ("Enter three integers: ");
num1 = scan.nextInt();
num2 = scan.nextInt();
num3 = scan.nextInt();
if (num1 < num2)
if (num1 < num3)
min = num1;
else
min = num3;
else
if (num2 < num3)
min = num2;
else
min = num3;
System.out.println ("Minimum value: " + min);
} Rabea Khatun, Lecturer,CSE, East West University 20
}
Control Statement(Selection)
Task 01 :
Write a program in java to determine a given number is even or
odd
Rabea Khatun, Lecturer,CSE, East West University 21
Switch Statement
•The switch statement provides another way to decide which
statement to execute next
•The switch statement evaluates an expression, then
attempts to match the result to one of several possible cases
(options)
•Each case contains a value and a list of statements
•The flow of control transfers to statement associated with
the first case value that matches
Rabea Khatun, Lecturer,CSE, East West University 22
Switch Statement
• The general syntax of a switch statement is:
switch switch (expression)
and {
case case value1:
are statement_List1
reserve break;
d case value2:
statement_List2
words break; If expression
case value3: matches
statement_List3 value2,
break;
case ... control jumps
to here
default:
statement_List
Rabea Khatun, Lecturer,CSE, East West University 23
}
Switch Statement: break
•Often a break statement is used as the last statement in each
case's statement list
•A break statement causes control to transfer to the end of
the switch statement
•If a break statement is not used, the flow of control will
continue into the next case
•Sometimes this may be appropriate, but often we want to
execute only the statements associated with one case
Rabea Khatun, Lecturer,CSE, East West University 24
Switch Statement
Trace switch statement
Suppose day is 2:
switch (day) { //day is of type int
case 1:
case 2:
case 3:
case 4:
case 5: System.out.println("Weekday"); break;
case 6:
case 7: System.out.println("Weekend");
}
Rabea Khatun, Lecturer,CSE, East West University 25
Switch Statement
Trace switch statement
Match case 2:
switch (day) { //day is of type int
case 1:
case 2:
case 3:
case 4:
case 5: System.out.println("Weekday"); break;
case 6:
case 7: System.out.println("Weekend");
}
Rabea Khatun, Lecturer,CSE, East West University 26
Switch Statement
Trace switch statement
Match case 2:
switch (day) { //day is of type int
case 1:
case 2:
case 3:
case 4:
case 5: System.out.println("Weekday"); break;
case 6:
case 7: System.out.println("Weekend");
}
Rabea Khatun, Lecturer,CSE, East West University 27
Switch Statement
Trace switch statement
Fall Through 3
switch (day) { //day is of type int
case 1:
case 2:
case 3:
case 4:
case 5: System.out.println("Weekday"); break;
case 6:
case 7: System.out.println("Weekend");
}
Rabea Khatun, Lecturer,CSE, East West University 28
Switch Statement
Trace switch statement
Fall through case 4
switch (day) { //day is of type int
case 1:
case 2:
case 3:
case 4:
case 5: System.out.println("Weekday"); break;
case 6:
case 7: System.out.println("Weekend");
}
Rabea Khatun, Lecturer,CSE, East West University 29
Switch Statement
Trace switch statement
Fall through case 5
switch (day) { //day is of type int
case 1:
case 2:
case 3:
case 4:
case 5: System.out.println("Weekday"); break;
case 6:
case 7: System.out.println("Weekend");
}
Rabea Khatun, Lecturer,CSE, East West University 30
Switch Statement
Trace switch statement
Print out weekday
switch (day) { //day is of type int
case 1:
case 2:
case 3:
case 4:
case 5: System.out.println("Weekday"); break;
case 6:
case 7: System.out.println("Weekend");
}
Rabea Khatun, Lecturer,CSE, East West University 31
Switch Statement
Trace switch statement
Encounter break
switch (day) { //day is of type int
case 1:
case 2:
case 3:
case 4:
case 5: System.out.println("Weekday"); break;
case 6:
case 7: System.out.println("Weekend");
}
Rabea Khatun, Lecturer,CSE, East West University 32
Switch Statement
Trace switch statement
Exit the statement
switch (day) { //day is of type int
case 1:
case 2:
case 3:
case 4:
case 5: System.out.println("Weekday"); break;
case 6:
case 7: System.out.println("Weekend");
}
Rabea Khatun, Lecturer,CSE, East West University 33
Switch Statement: default case
A switch statement can have an optional default case
The default case has no associated value and simply uses the
reserved word default
If the default case is present, control will transfer to the default
case if no other case value matches
If there is no default case, and no other value matches, control
falls through to the statement after the switch statement
Rabea Khatun, Lecturer,CSE, East West University 34
Switch Statement: default case
switch (option) //option is of type char
{
case 'A':
aCount = aCount + 1;
break;
case 'B':
bCount = bCount + 1;
break;
case 'C':
cCount = cCount + 1;
break;
default:
System.out.println ("Invalid
Option…")
}
Rabea Khatun, Lecturer,CSE, East West University 35
Switch Statement
Rabea Khatun, Lecturer,CSE, East West University 36
Switch Statement expressions
The expression of a switch statement must result in an
integer type (byte, short, int, long) or a char type.
It cannot be a boolean value or a floating point value (float
or double)
You cannot perform relational checks with a switch
statement
Rabea Khatun, Lecturer,CSE, East West University 37
Switch Statement
Tasks:
1) Write a program in java to determine the grade of a
given number
2) Write a program in java to determine given char is vowel
or consonant
Rabea Khatun, Lecturer,CSE, East West University 38
Some useful hints
if (i > 0) {
System.out.println("i is positive");
}
Same as
if (i > 0)
System.out.println("i is positive");
Rabea Khatun, Lecturer,CSE, East West University 39
Some useful hints
Rabea Khatun, Lecturer,CSE, East West University 40
Some useful hints
Rabea Khatun, Lecturer,CSE, East West University 41
Some useful hints
Rabea Khatun, Lecturer,CSE, East West University 42