Week 5 6 7 - Selection - Control - Structures Updated
Week 5 6 7 - Selection - Control - Structures Updated
y
LOGO
Control Structures
Selection
Objectives
Sequence
statement1
statement2
statement3
Flow of Control: Selection Structures
if ( condition )
statement;
8
Boolean Expressions
9
Examples of Boolean Expressions
10
Logic of an if statement
false false
condition testScore < 40?
evaluated
true true
statement
System.out.println (“You failed”);
if (testScore<40)
System.out.println(“You failed”);
The if Statement
The condition is
Executed when if
evaluated first:
statement is true,
is testScore less
if no, it is skipped
than 40?
Executed either
if statement is
true / false
Java code example: != operator
Output:
Result : y not equal x
Java code example: >= operator
Output:
You will get 20% discount
Thank you for coming to CMART
Java code example: <= operator
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");
}
17
Block Statements
COMPARE WITH
P, Q: conditions
Logical (Boolean) Operation
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");
21
Precedence of Operators
The if-else Statement (2 way selection)
23
Logic of an if-else statement
condition true
evaluated
false
statement1 statement2
Logic of an if-else statement
true
testScore<40?
false
System.out.println(“You failed”);
System.out.println(“You passed”);
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
Example of Algorithm:
if/else Statement
DESIGN OF INTERACTIVE
PROGRAM: PSEUDOCODE & FLOW
Start
Start
CHART Input totalPrice
Input totalPrice
if(totalPrice > 100) False
totalPrice>100? discount =5.0/100*totalPrice
Calculate discount= 20.0/100 * totalPrice
else True
Calculate discount = 5.0/100 * totalPrice
Print “Discount given is RM” +discount discount = 20.0/100*totalPrice
End
Print “Discount given is
RM” +discount
End
Code: if/else Statement
SAMPLE
Start
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
else
{
System.out.println("Sorry, you did not get A.");
System.out.println("Try again next time.");
}
Multiple Selection
Syntax:
if (expression1)
statement1
else if (expression2)
statement2
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)
if(balance>=50.00)
{
if(interestRate>=0)
balance = balance + ((interestRate * balance)/12);
else
System.out.println("Cannot have negative interest");
}
else
balance = balance - penalty;