Topic 4 - Control - Structures-Selection
Topic 4 - Control - Structures-Selection
CONTROL
STRUCTURE
S
Selection
1
1. To learn how to
use the selection
control structure.
2. To understand
OBJECTIVE
S
various selection
structures.
3. To improve
algorithm design
skills.
3 TYPES FLOW OF
CONTROL
Sequential (we had learned in previous
topic)
• The statements in a program are executed in
sequential order
Selection
• allow the program to select one of multiple paths
of execution.
• The path is selected based on some conditional
criteria (boolean expression).
Conditions
If do not
If exceed 250:
exceed 250:
Give warning with
Close the door and
BEEP sound
go up/down
Actions
FLOW OF There are 3 types
CONTRO of Java selection
L: structures:
SELECTI o if statement
ON o if-else statement
STRUCTU o switch statement
RES
THE IF STATEMENT
if ( condition )
statement;
== equal to
!= not equal to
< less than
> greater than
<= less than or equal to
>= greater than or equal to
9
EXAMPLES OF BOOLEAN
EXPRESSIONS
Boolean Descriptions
Expressions
testScore < 80 returns TRUE if testScore is less than 80
a == b returns TRUE if a equals to b
numb1 != numb2 returns TRUE if numb1 not equal to
numb2
testScore * 2 > 350 returns TRUE if testScore * 2 is greater
than 350
w * (h*h) < 30 returns TRUE if w*(h*h) is less than 30
a * a <=c returns TRUE if a*a is less than or equal
to c
1
0
LOGIC OF AN IF
STATEMENT
false false
condition testScore < 40?
evaluated
true true
if (testScore<40)
System.out.println(“You failed”);
THE IF STATEMENT
The condition is
evaluated first: Executed when if
is testScore less than statement is true,
40? if no, it is skipped
Executed either
if statement is
true / false
JAVA CODE EXAMPLE: !=
OPERATOR
public class Count
{
public static void main (String args[])
{
double y=15.0;
double x=25.0;
if (y!=x)
System.out.println("Result : y not equal x");
}
}
Output:
Output:
You will get 20% discount
Thank you for coming to CMART
Java code example: <= operator
JAVA CODE EXAMPLE: <=
OPERATOR
public class DetermineResult
{
public static void main (String args[])
{
double totalMarks = 60;
if(totalMarks <=40)
System.out.println("You failed the subject!");
System.out.println("Good luck for next sem");
}
}
Output:
Good luck for next sem
THINK..
if (testScore<40)
{
System.out.println ("You failed");
System.out.println ("Try harder next time");
}
1
7
BLOCK STATEMENTS
COMPARE WITH
if (testScore < 40)
System.out.println ("You failed");
System.out.println ("Try harder next time");
1
8
LOGICAL (BOOLEAN)
OPERATION
Operators Names Descriptions
P, Q: conditions
LOGICAL (BOOLEAN)
OPERATION
P, Q: conditions, e.g: if((carryMark<10) && (finalMark<20))
P Q
P Q P&&Q P||Q !P
false false false false true
false true false true true
true false false true false
true true true true false
LOGICAL OPERATORS
if (!(testScore <40))
System.out.println ("You passed the exam");
2
1
PRECEDENCE OF
OPERATORS
THE IF-ELSE STATEMENT (2
WAY SELECTION)
An else clause can be added to an if statement to
make an if-else statement
if ( condition )
statement1;
else
statement2;
If the condition is true, statement1 is executed; if the
condition is false, statement2 is executed
One or the other will be executed, but not both
2
3
LOGIC
OF AN condition true
evaluated
IF-ELSE false
ENT
LOGIC true
OF AN testScore<40?
IF-ELSE
false System.out.pr
intln(“You
failed”);
STATEM System.out.pr
intln(“You
passed”);
ENT
IF/ELSE STATEMENT
if (testScore<40)
System.out.println("You failed");
else
System.out.println("You passed");
Purpose:
To execute a statement when a condition is true or
false
DESIGN OF INTERACTIVE
PROGRAM: PSEUDOCODE &
FLOW CHART
Example of Algorithm: if/else
Statement
Start Start
Input totalPrice
Input totalPrice
if(totalPrice > 100)
Calculate discount= 20.0/100 * False
totalPrice totalPrice>100? discount =5.0/100*totalPrice
else True
End
SAMPLE CODE
import java.util.Scanner;
Sample Run
Enter total price:
90
Discount given is RM 4.50
BLOCK STATEMENT
if (testScore<40)
{
System.out.println ("You failed");
System.out.println ("Try harder next
time");
}
else
{
System.out.println ("You passed");
System.out.println ("Keep it up!");
}
FLOW CHART FOR IF ELSE
STATEMENT: WITH BLOCK
Start
Input testScore
true
testScore<40?
End
PSEUDO CODE FOR IF ELSE
STATEMENT: WITH BLOCK
Start
Input testScore
if (testScore<40)
Print "You failed"
Print "Try harder"
else
Print "You passed"
Print "Keep it up!"
End
COMBINATION OF BLOCK
STATEMENT AND LOGICAL
OPERATORS
EXAMPLE:
Determine Grade, if testScore is higher or equal to
85%, the achieved grade is A
if ((testScore >=85) && (testScore<=100 ))
{
System.out.println("Congrats! Your grade is A.");
System.out.println("Keep it up!");
}
else
{
System.out.println("Sorry, you did not get A.");
System.out.println("Try again next time.");
}
Syntax:
MULTIPL if (expression1)
E statement1
ON else
statement3
FLOW CHART FOR
MULTIPLE SELECTION
Start
Input testScore
true
testScore>90? Print “Very high grade”
Print “Keep it up!”
false
false
End
MULTIPLE SELECTION
if (testScore>90)
{
System.out.println ("Very high grade");
System.out.println ("Keep it up!");
}
else if (testScore>50 && testScore <60)
{
System.out.println ("Moderate");
System.out.println ("Can improve");
}
else
{
System.out.println ("Not very high or moderate");
}
JAVA CODE (MULTIPLE
SELECTION)
int a;
System.out.println("Enter the number:");
a = scan.nextInt();
if (a>=1)
{
System.out.println ("The number you entered is :" +
a);
System.out.println ("You entered a positive number");
}
else if (a<0)
{
System.out.println ("The number you entered is :" +
a);
System.out.println ("You entered a negative number");
}
else
{
System.out.println ("The number you entered is :" +
a);
System.out.println ("You entered zero");
}
Sample Run
Output1
Enter the number : 15
The number you entered is :15
You entered a positive number
Output2
Enter the number : -15
The number you entered is :-15
You entered a negative number
Output3
Enter the number : 0
The number you entered is :0
You entered zero
MULTIPLE SELECTIONS
Example
The grading scheme for a course is given as below:
Mark Grade
90 - 100 A
80 – 89 B
70 – 79 C
60 – 69 D
0 - 59 F
Read a mark & determine the grade.
MULTIPLE SELECTIONS
Sample run:
if( a == 50 ) {
if( b <= 10 ) {
System.out.print("a = 50 and b <= 10");
}
}
}//main
} //class
MULTIPLE SELECTION
(NESTED IF)
Example
int numb = 50;
if(numb%2 == 0)
{
if(numb >0)
{
System.out.println("The even number is a positive number");
}
}
else
{
System.out.println("The number is odd");
}
MULTIPLE SELECTION
(NESTED IF)
double balance = 90.00, penalty=10.00;
int interestRate = 2;
if(balance>=50.00)
{
if(interestRate>=0)
balance = balance + ((interestRate * balance)/12);
else
System.out.println("Cannot have negative interest");
}
else
balance = balance - penalty;
Selection control
structure.
Various selection
structures.
46