selection control structures
DESCRIPTION
Chapter 4TRANSCRIPT
![Page 1: Selection Control Structures](https://reader036.vdocuments.mx/reader036/viewer/2022062300/555c3e81d8b42a2c068b4bff/html5/thumbnails/1.jpg)
Control Structures 1:
Selection
![Page 2: Selection Control Structures](https://reader036.vdocuments.mx/reader036/viewer/2022062300/555c3e81d8b42a2c068b4bff/html5/thumbnails/2.jpg)
Chapter Goals
Be able to use the selection control structure
Be able to solve problems involving repetition.
Understand the difference among various selection & loop structures.
Know the principles used to design effective selection & loops (next topic).
Improve algorithm design skills.
![Page 3: Selection Control Structures](https://reader036.vdocuments.mx/reader036/viewer/2022062300/555c3e81d8b42a2c068b4bff/html5/thumbnails/3.jpg)
3 Types Flow of Control
Sequential (we had learn 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)
Repetition (we will learn in next topic)
![Page 4: Selection Control Structures](https://reader036.vdocuments.mx/reader036/viewer/2022062300/555c3e81d8b42a2c068b4bff/html5/thumbnails/4.jpg)
Flow of Control: Sequential Structures
statement1
statement2
statement3
![Page 5: Selection Control Structures](https://reader036.vdocuments.mx/reader036/viewer/2022062300/555c3e81d8b42a2c068b4bff/html5/thumbnails/5.jpg)
If the boolean expression evaluates to true, the statement will be executed. Otherwise, it will be skipped.
Flow of Control: Selection Structures
![Page 6: Selection Control Structures](https://reader036.vdocuments.mx/reader036/viewer/2022062300/555c3e81d8b42a2c068b4bff/html5/thumbnails/6.jpg)
There are 3 types of Java selection structures:
if statementif-else statementswitch statement
Flow of Control: Selection Structures
![Page 7: Selection Control Structures](https://reader036.vdocuments.mx/reader036/viewer/2022062300/555c3e81d8b42a2c068b4bff/html5/thumbnails/7.jpg)
The if Statement The if statement has the following syntax:
7
if ( condition ) statement;
if is a Javareserved word
The condition must be aboolean expression. It mustevaluate to either true or false.
If the condition is true, the statement is executed.If it is false, the statement is skipped.
![Page 8: Selection Control Structures](https://reader036.vdocuments.mx/reader036/viewer/2022062300/555c3e81d8b42a2c068b4bff/html5/thumbnails/8.jpg)
Logic of an if statement
conditionevaluated
statement
truefalse
![Page 9: Selection Control Structures](https://reader036.vdocuments.mx/reader036/viewer/2022062300/555c3e81d8b42a2c068b4bff/html5/thumbnails/9.jpg)
if Statement
if (amount <= balance) balance = balance - amount;
![Page 10: Selection Control Structures](https://reader036.vdocuments.mx/reader036/viewer/2022062300/555c3e81d8b42a2c068b4bff/html5/thumbnails/10.jpg)
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
10
![Page 11: Selection Control Structures](https://reader036.vdocuments.mx/reader036/viewer/2022062300/555c3e81d8b42a2c068b4bff/html5/thumbnails/11.jpg)
The if Statement
if (total > MAX) charge = total * MAX_RATE;System.out.println ("The charge is " + charge);
First the condition is evaluated -- the value of total is either greater than the value of MAX
If the condition is true, the assignment statement is executed -- if it isn’t, it is skipped.
Either way, the call to println is executed next
![Page 12: Selection Control Structures](https://reader036.vdocuments.mx/reader036/viewer/2022062300/555c3e81d8b42a2c068b4bff/html5/thumbnails/12.jpg)
Java code example
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");
}
}
![Page 13: Selection Control Structures](https://reader036.vdocuments.mx/reader036/viewer/2022062300/555c3e81d8b42a2c068b4bff/html5/thumbnails/13.jpg)
OutputResult : y not equal x
![Page 14: Selection Control Structures](https://reader036.vdocuments.mx/reader036/viewer/2022062300/555c3e81d8b42a2c068b4bff/html5/thumbnails/14.jpg)
Block Statements Several statements can be grouped
together into a block statement delimited by braces
14
if (total > MAX){ System.out.println ("Error!!"); errorCount++;}
![Page 15: Selection Control Structures](https://reader036.vdocuments.mx/reader036/viewer/2022062300/555c3e81d8b42a2c068b4bff/html5/thumbnails/15.jpg)
Block Statementif (amount <= balance){
balance = balance - amount; System.out.println(“Acct new balance = “ + balance);
}
COMPARE WITH
if (amount <= balance) balance = balance - amount; System.out.println(“Acct new balance = “ + balance);
![Page 16: Selection Control Structures](https://reader036.vdocuments.mx/reader036/viewer/2022062300/555c3e81d8b42a2c068b4bff/html5/thumbnails/16.jpg)
Logical Operators
Expressions that use logical operators can form complex conditions
16
if ((income > MIN_LEVEL ) && (age <50)) System.out.println (“Can Apply Loan");
All logical operators have lower precedence than the relational operators
Logical NOT has higher precedence than logical AND and logical OR
![Page 17: Selection Control Structures](https://reader036.vdocuments.mx/reader036/viewer/2022062300/555c3e81d8b42a2c068b4bff/html5/thumbnails/17.jpg)
Logical (Boolean) Operation in Java
![Page 18: Selection Control Structures](https://reader036.vdocuments.mx/reader036/viewer/2022062300/555c3e81d8b42a2c068b4bff/html5/thumbnails/18.jpg)
Precedence of Operators
![Page 19: Selection Control Structures](https://reader036.vdocuments.mx/reader036/viewer/2022062300/555c3e81d8b42a2c068b4bff/html5/thumbnails/19.jpg)
Logical Operators
if ((amount <= 1000.0) && (amount <= balance)){
balance = balance - amount; System.out.println(“Acct new balance = “ + balance);
}
EXAMPLE:New withdrawal condition:Withdrawal amount of more than RM1000.00 is not allowed.
![Page 20: Selection Control Structures](https://reader036.vdocuments.mx/reader036/viewer/2022062300/555c3e81d8b42a2c068b4bff/html5/thumbnails/20.jpg)
The if-else Statement (2 way selection) An else clause can be added to an if
statement to make an if-else statement
20
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
![Page 21: Selection Control Structures](https://reader036.vdocuments.mx/reader036/viewer/2022062300/555c3e81d8b42a2c068b4bff/html5/thumbnails/21.jpg)
Logic of an if-else statement
conditionevaluated
statement1
true false
statement2
![Page 22: Selection Control Structures](https://reader036.vdocuments.mx/reader036/viewer/2022062300/555c3e81d8b42a2c068b4bff/html5/thumbnails/22.jpg)
if/else Statement
![Page 23: Selection Control Structures](https://reader036.vdocuments.mx/reader036/viewer/2022062300/555c3e81d8b42a2c068b4bff/html5/thumbnails/23.jpg)
if/else Statementif (amount <= balance)
balance = balance - amount;else
balance = balance - OVERDRAFT_PENALTY;
Purpose:
To execute a statement when a condition is true or false
![Page 24: Selection Control Structures](https://reader036.vdocuments.mx/reader036/viewer/2022062300/555c3e81d8b42a2c068b4bff/html5/thumbnails/24.jpg)
Block Statement
if (amount <= balance){
balance = balance - amount; System.out.println(“Acct new balance = “ + balance);
}else{ balance = balance - OVERDRAFT_PENALTY;
System.out.println(“TRANSACTION NOT ALLOWED”);}
![Page 25: Selection Control Structures](https://reader036.vdocuments.mx/reader036/viewer/2022062300/555c3e81d8b42a2c068b4bff/html5/thumbnails/25.jpg)
Combine with Boolean operators
if ((age >= 25) && (age <= 50)){
System.out.println(“You are qualified to apply”);}else { System.out.println(“You are NOT qualified to apply”); }
EXAMPLE:Loan Processing. Can apply if age is between 25 to 50.
![Page 26: Selection Control Structures](https://reader036.vdocuments.mx/reader036/viewer/2022062300/555c3e81d8b42a2c068b4bff/html5/thumbnails/26.jpg)
Multiple Selection (nested if)
Syntax:if (expression1)
statement1
else
if (expression2)
statement2
else
statement3
![Page 27: Selection Control Structures](https://reader036.vdocuments.mx/reader036/viewer/2022062300/555c3e81d8b42a2c068b4bff/html5/thumbnails/27.jpg)
Java code (multiple selection)if (a>=1){
System.out.println ("The number you enter is :" + a); System.out.println ("You enter the positive number");
}else if (a<0){
System.out.println ("The number you enter is :" + a); System.out.println ("You enter the negative number");
}else {
System.out.println ("The number you enter is :" + a); System.out.println ("You enter the zero number");
}
![Page 28: Selection Control Structures](https://reader036.vdocuments.mx/reader036/viewer/2022062300/555c3e81d8b42a2c068b4bff/html5/thumbnails/28.jpg)
OutputEnter the number : 15The number you enter is :15You enter the positive number Enter the number : -15The number you enter is :-15You enter the negative number Enter the number : 0The number you enter is :0You enter the zero number
![Page 29: Selection Control Structures](https://reader036.vdocuments.mx/reader036/viewer/2022062300/555c3e81d8b42a2c068b4bff/html5/thumbnails/29.jpg)
Multiple SelectionsExample 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.
![Page 30: Selection Control Structures](https://reader036.vdocuments.mx/reader036/viewer/2022062300/555c3e81d8b42a2c068b4bff/html5/thumbnails/30.jpg)
Multiple Selectionsif (mark >= 90)
grade = ‘A ’;
else if (mark >= 80)
grade = ‘B’;
else if (mark >= 70)
grade = ‘C’;
else if (mark >= 60)
grade = ‘D ’;
else
grade = ‘F ’;
![Page 31: Selection Control Structures](https://reader036.vdocuments.mx/reader036/viewer/2022062300/555c3e81d8b42a2c068b4bff/html5/thumbnails/31.jpg)
Equivalent code with series of if statements
if ((mark >= 90) && (mark <=100))
grade = ‘A ’;
if ((mark >= 80) && (mark >= 89))
grade = ‘B’;
if ((mark >= 70) && (mark >= 79))
grade = ‘C’;
if ((mark >= 60) && (mark >= 69))
grade = ‘D ’;
if ((mark >= 0) && (mark >= 59))
grade = ‘F ’;
![Page 32: Selection Control Structures](https://reader036.vdocuments.mx/reader036/viewer/2022062300/555c3e81d8b42a2c068b4bff/html5/thumbnails/32.jpg)
switch Structures (multiple selection)
switch (expression){case value1: statements1
break;case value2: statements2
break; ...case valuen: statementsn
break;default: statements}
Expression is also known as selector.
Value can only be integral.
If expressionmatches value2,control jumpsto here
![Page 33: Selection Control Structures](https://reader036.vdocuments.mx/reader036/viewer/2022062300/555c3e81d8b42a2c068b4bff/html5/thumbnails/33.jpg)
switch Structures
![Page 34: Selection Control Structures](https://reader036.vdocuments.mx/reader036/viewer/2022062300/555c3e81d8b42a2c068b4bff/html5/thumbnails/34.jpg)
The switch 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
![Page 35: Selection Control Structures](https://reader036.vdocuments.mx/reader036/viewer/2022062300/555c3e81d8b42a2c068b4bff/html5/thumbnails/35.jpg)
Control flow of switch statement with and without the break statements
![Page 36: Selection Control Structures](https://reader036.vdocuments.mx/reader036/viewer/2022062300/555c3e81d8b42a2c068b4bff/html5/thumbnails/36.jpg)
Switch/Break Examples
int m = 2;switch (m){ case 1 : System.out.println(“m=1”); break; case 2 : System.out.println(“m=2”); break; case 3 : System.out.println(“m=3”); break; default: System.out.println(“default”);}
int m = 2;switch (m){ case 1 : System.out.println(“m=1”); break; case 2 : System.out.println(“m=2”); break; case 3 : System.out.println(“m=3”); break; default: System.out.println(“default”);}
Output: m=2
char ch = ‘b’;switch (ch){ case ‘a’ : System.out.println(“ch=a”);
case ‘b’ : System.out.println(“ch=b”);
case ‘c’ : System.out.println(“ch=c”);
default: System.out.println(“default”);}
char ch = ‘b’;switch (ch){ case ‘a’ : System.out.println(“ch=a”);
case ‘b’ : System.out.println(“ch=b”);
case ‘c’ : System.out.println(“ch=c”);
default: System.out.println(“default”);}
Output: ch=b ch=c default