Lecture 4 - Selection
Lecture 4 - Selection
Selections
1
Outline
1. Flow of Control
2. Conditional Statements
3. The if Statement
4. The if-else Statement
5. The Conditional operator
6. The Switch Statement
7. Useful Hints
2
Md. Morshed Ali, Lecturer, Dept. of CSE, UU
1. Flow of Control
3
Md. Morshed Ali, Lecturer, Dept. of CSE, UU
2. Selection/Conditional Statements
4
Md. Morshed Ali, Lecturer, Dept. of CSE, UU
3. The if Statement
if (condition)
{
statementBlock;
};
Stateme
nt
1 grade = 70;
2 If (grade>= 90)
condition 3 System.out.println("You got an
evaluated "A");
4 System.out.println("This is line 4");
true false
Statement
Block 1 grade = 95;
2 If (grade>= 90)
3 System.out.println("You got an
"A");
4 System.out.println("This is line 4");
Stateme
nt
6
Md. Morshed Ali, Lecturer, Dept. of CSE, UU
Boolean Expressions
== 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 (=)
7
Md. Morshed Ali, Lecturer, Dept. of CSE, UU
Example - if Statement
An example of an if statement:
// Age.java
import java.util.Scanner;
public class Age
{
public static void main (String[] args)
{
final int MINOR = 21;
Scanner scan = new Scanner (System.in);
System.out.print ("Enter your age: ");
int age = scan.nextInt();
System.out.println ("You entered: " + age);
10
Md. Morshed Ali, Lecturer, Dept. of CSE, UU
Expressions
12
Md. Morshed Ali, Lecturer, Dept. of CSE, UU
Logical Operators
boolean a !a
true false
false true
13
Md. Morshed Ali, Lecturer, Dept. of CSE, UU
Logical Operators
a && b
is true if both a and b are true, and false otherwise
The logical OR expression
a || b
is true if a or b or both are true, and false otherwise
The logical XOR expression
a ^ b
is true if and only if a and b are different.
14
Md. Morshed Ali, Lecturer, Dept. of CSE, UU
Logical Operators
Since &&, ||, and ^ each have two operands, there are four possible
combinations of conditions a and b (boolean expressions)
a b a && b a || b a ^ b
true true true true false
true false false true true
false true false true true
false false false false false
15
Md. Morshed Ali, Lecturer, Dept. of CSE, UU
Boolean Expressions
16
Md. Morshed Ali, Lecturer, Dept. of CSE, UU
Boolean Expressions
17
Md. Morshed Ali, Lecturer, Dept. of CSE, UU
Operator Precedence
! Not
*, /, % Math operators
+, - Math operators
<, <=, >, >= Relational operators
==, != Relational equality
^ Exclusive OR
&& Logical AND
|| Logical OR
=, +=, -=, *=, /=, %= Assignment operators
18
Md. Morshed Ali, Lecturer, Dept. of CSE, UU
Operator Precedence
3 + 4 * 4 > 5 * (4 + 3) - 1
(1) inside parentheses first
3 + 4 * 4 > 5 * 7 – 1
(2) multiplication
3 + 16 > 5 * 7 – 1
(3) multiplication
3 + 16 > 35 – 1
(4) addition
19 > 35 – 1
(5) subtraction
19 > 34
(6) greater than
false
19
Md. Morshed Ali, Lecturer, Dept. of CSE, UU
4. The if-else Statement
Statement
condition
evaluated
true false
StatementBlock
StatementBlock2
1
Statement 21
Md. Morshed Ali, Lecturer, Dept. of CSE, UU
Trace if-else statement
22
Md. Morshed Ali, Lecturer, Dept. of CSE, UU
Trace if-else statement
23
Md. Morshed Ali, Lecturer, Dept. of CSE, UU
Trace if-else statement
24
Md. Morshed Ali, Lecturer, Dept. of CSE, UU
Trace if-else statement
25
Md. Morshed Ali, Lecturer, Dept. of CSE, UU
Trace if-else statement
26
• See Wages.java example next slide.
Md. Morshed Ali, Lecturer, Dept. of CSE, UU
Example
// Wages.java
import java.text.NumberFormat;
import java.util.Scanner;
public class Wages
{
public static void main (String[] args)
{
final double RATE = 8.25; //regular pay rate
final int STANDARD = 40; //weekly hours
Scanner scan = new Scanner (System.in); //scanner object
double pay = 0.0; // initialization
System.out.print ("Enter the number of hours worked: ");
//prompt
int hours = scan.nextInt(); //read input value
System.out.println (); //print blank line
// Pay overtime at "time and a half"
if (hours > STANDARD)
pay = STANDARD * RATE + (hours-STANDARD) * (RATE * 1.5);
else
pay = hours * RATE;
NumberFormat fmt = NumberFormat.getCurrencyInstance();//format
System.out.println ("Gross earnings: " +
fmt.format(pay));//output
}
}
27
Md. Morshed Ali, Lecturer, Dept. of CSE, UU
Indentation - Revisited
28
Md. Morshed Ali, Lecturer, Dept. of CSE, UU
Block Statements
29
Md. Morshed Ali, Lecturer, Dept. of CSE, UU
Block Statements
// Guessing.java
import java.util.*;
public class Guessing
{
public static void main (String[] args)
{
final int MAX = 10;
int answer, guess;
Scanner scan = new Scanner (System.in); //scanner object
Random generator = new Random(); //number generator object
answer = generator.nextInt(MAX) + 1; //generate a number
System.out.print ("I'm thinking of a number between 1"
+ "and " + MAX + ". Guess what it is: ");
guess = scan.nextInt(); //read user input
if (guess == answer)
System.out.println ("You got it! Good guessing!");
else
{
System.out.println ("That is not correct!");
System.out.println ("The number was " + answer);
}
}
}
31
Md. Morshed Ali, Lecturer, Dept. of CSE, UU
5. The Conditional Operator
32
Md. Morshed Ali, Lecturer, Dept. of CSE, UU
The Conditional Operator
33
Md. Morshed Ali, Lecturer, Dept. of CSE, UU
The Conditional Operator
Another example:
34
Md. Morshed Ali, Lecturer, Dept. of CSE, UU
Nested if Statements
35
Md. Morshed Ali, Lecturer, Dept. of CSE, UU
Example
// 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 < num2) min = num1;
if (num1 < num3) else
min = num1; min = num2;
else
min = num3;
else if (num3 < min)
if (num2 < num3) min = num3;
min = num2;
else
min = num3;
System.out.println ("Minimum value: " + min);
} 36
} Md. Morshed Ali, Lecturer, Dept. of CSE, UU
6. Switch Statement
The flow of control transfers to statement associated with the first case
value that matches
37
Md. Morshed Ali, Lecturer, Dept. of CSE, UU
Syntax
default:
statement_List
} 38
Md. Morshed Ali, Lecturer, Dept. of CSE, UU
break Statement
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
39
Md. Morshed Ali, Lecturer, Dept. of CSE, UU
Trace switch statement
Suppose day is 2:
40
Md. Morshed Ali, Lecturer, Dept. of CSE, UU
Trace switch statement
Match case 2
switch (day) {
case 1:
case 2:
case 3:
case 4:
case 5: System.out.println("Weekday"); break;
case 6:
case 7: System.out.println("Weekend");
}
41
Md. Morshed Ali, Lecturer, Dept. of CSE, UU
Trace switch statement
Match case 2
switch (day) {
case 1:
case 2:
case 3:
case 4:
case 5: System.out.println("Weekday"); break;
case 6:
case 7: System.out.println("Weekend");
}
42
Md. Morshed Ali, Lecturer, Dept. of CSE, UU
Trace switch statement
switch (day) {
case 1:
case 2:
case 3:
case 4:
case 5: System.out.println("Weekday"); break;
case 6:
case 7: System.out.println("Weekend");
}
43
Md. Morshed Ali, Lecturer, Dept. of CSE, UU
Trace switch statement
switch (day) {
case 1:
case 2:
case 3:
case 4:
case 5: System.out.println("Weekday"); break;
case 6:
case 7: System.out.println("Weekend");
}
44
Md. Morshed Ali, Lecturer, Dept. of CSE, UU
Trace switch statement
switch (day) {
case 1:
case 2:
case 3:
case 4:
case 5: System.out.println("Weekday"); break;
case 6:
case 7: System.out.println("Weekend");
}
45
Md. Morshed Ali, Lecturer, Dept. of CSE, UU
Trace switch statement
Printout Weekday
switch (day) {
case 1:
case 2:
case 3:
case 4:
case 5: System.out.println("Weekday"); break;
case 6:
case 7: System.out.println("Weekend");
}
46
Md. Morshed Ali, Lecturer, Dept. of CSE, UU
Trace switch statement
Encounter break
switch (day) {
case 1:
case 2:
case 3:
case 4:
case 5: System.out.println("Weekday"); break;
case 6:
case 7: System.out.println("Weekend");
}
47
Md. Morshed Ali, Lecturer, Dept. of CSE, UU
Trace switch statement
switch (day) {
case 1:
case 2:
case 3:
case 4:
case 5: System.out.println("Weekday"); break;
case 6:
case 7: System.out.println("Weekend");
}
48
Md. Morshed Ali, Lecturer, Dept. of CSE, UU
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
49
Md. Morshed Ali, Lecturer, Dept. of CSE, UU
Example
51
Md. Morshed Ali, Lecturer, Dept. of CSE, UU
Example
import java.util.Scanner;
public class GradeReport
{ public static void main (String[] args)
{ ... Some other code here
grade = scan.nextInt();
category = grade / 10;
System.out.print ("That grade is ");
switch (category)
{
case 10:
System.out.println ("a perfect score, well done.");
break;
case 9:
System.out.println ("well above average. Excellent.");
break;
case 8:
System.out.println ("above average. Nice job.");
break;
case 7:
System.out.println ("average.");
break;
case 6:
System.out.println ("below average. Do better!");
break;
default:
System.out.println ("not passing.");
}
} 52
} Md. Morshed Ali, Lecturer, Dept. of CSE, UU
7. Useful Hints
if i > 0 {
System.out.println("i is positive"); //wrong
}
if (i > 0) {
System.out.println("i is positive"); //correct
}
===================================================
if (i > 0) {
System.out.println("i is positive");
}
Same as
if (i > 0)
System.out.println("i is positive");
53
Md. Morshed Ali, Lecturer, Dept. of CSE, UU
Useful Hints
The else clause matches the most recent if clause in the same block.
int i = 1; int i = 1;
int j = 2; int j = 2;
int k = 3; int k = 3;
Equivalent
if (i > j) if (i > j)
if (i > k) if (i > k)
System.out.println("A"); System.out.println("A");
else else
System.out.println("B"); System.out.println("B");
56
Md. Morshed Ali, Lecturer, Dept. of CSE, UU
Thank You
57