[go: up one dir, main page]

0% found this document useful (0 votes)
10 views11 pages

SelectionLogical&Nesting Part2 PDF

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views11 pages

SelectionLogical&Nesting Part2 PDF

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Selection/Making Decisions – Part 2

(Ms Swart)
Decisions using compound conditions
Simple relational expressions can be COMBINED with logical operators to give
compound/complex expressions.
When these complex expressions are evaluated, they still give a TRUE or a FALSE as an answer.
Logical operators (3):
Java
not !
and &&
or ||

Relational operators work with basic type operands ( eg. int >= int )
These 3 logical operators work with boolean operands ( eg. true && false )

Truth tables: X and Y below represent ordinary relational expressions/conditions


eg. (sum > 70) && (sum < 150)

X Y X && Y X Y X || Y X !X
F F F F F F F T
F T F F T T T F
T F F T F T
T T T T T T
Using logical operators, it is now possible to describe a numeric interval, not only an open-ended
set of values like (total < 150)

So, in addition to: “All values more than 10” → ( x > 10 )

we can now also describe: “All values between 0 and 100, including both”

( in Maths, we write [ 0 ; 100 ] or ( 0 ≤ x ≤ 100) ) → ( x >= 0 ) && ( x <= 100 )

To describe all the values INSIDE a closed interval, eg. ( 12 ≤ num < 50 ), always use the AND

operator to combine the 2 “half” conditions → (num >= 12) && (num < 50)

To describe all the values OUTSIDE an interval, eg. either ( num ≤ 1 ) or ( num > 50 ), always use

the OR operator to combine the 2 “half” conditions → (num <= 1) | | (num > 50)
if-statements are called “Two-way branching”.

They can become “Multi-way branching” (more complex) by using if-statements INSIDE other if-
statements = “nesting”

Example: (testing for intervals)


A parcel costs a certain amount to be delivered, depending on its weight. The table below is used
to determine the cost of sending the parcel:
Weight Price / kg
< 2kg 10.00
2kg – 4kg 20.00
4kg – 6kg 30.00
> 6kg 50.00
A program is needed that reads in the weight of the parcel and displays the amount that it will cost
to send that parcel.
JAVA:
:
: what we know
if (weight <= 0) ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ (invalid weight)
System.out.println("Invalid weight value – cannot proceed”);
else ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ (valid weight > 0)
{ if (weight >= 2)
{ if (weight >= 4)
{ if (weight > 6)
totCost = weight * 50; ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ (weight > 6)
else
totCost = weight * 30; ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ (4 < weight <= 6)
} // end of true block of (weight >= 4)
else
totCost = weight * 20; ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ (2 <= weight < 4)
} // end of true block of (weight >= 2)
else
totCost = weight * 10; ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ (weight < 2)
System.out.println("The item will cost R"+totCost+" to send.");
} // end of false block of (weight <= 0)
xxxxxxxx
Example: (Testing for single values)

- Can use nesting

- Can use the switch-statement (next lesson)

Write a program that displays a menu with 5 options (A, B, C, D and E), and then asks the user to
type in the letter of the choice they made. The choice must be displayed appropriately. Any other
letter entered must cause an error message to be displayed.
USING NESTING - in the false part (else)
JAVA:
:
Scanner kbd = new Scanner(System.in);

char choice;

System.out.print("A. Fish & Chips");


System.out.print("B. Hamburger");
System.out.print("C. Pizza");
System.out.print("D. Waffle");
System.out.print("E. Pancake");
System.out.print("Please enter your choice (A/B/C/D/E): ");
choice = kbd.next().charAt(0);

if (choice == 'A')
System.out.println("You selected Fish & Chips");
else if (choice == 'B')
System.out.println("You selected a Hamburger");
else if (choice == 'C')
System.out.println("You selected a Pizza");
else if (choice == 'D')
System.out.println("You selected a Waffle");
else if (choice == 'E')
System.out.println("You selected a Pancake");
else // choice is neither A, nor B, nor C, nor D, nor E
System.out.println("Invalid choice entered");
Another example: The traditional "Input a mark, display the corresponding symbol" problem
shows that "there are many ways to skin a cat".
Mark Symbol
80 – 100 A
70 – 79 B
60 – 69 C
50 – 59 D
below 50 F
0 - 49
Option 1: Using multiple if statements, each describing one interval, ALL ON THE SAME LEVEL

if ((mark >= 80) && (mark <= 100))


System.out.println("Symbol: A");
if ((mark >= 70) && (mark <= 79))
System.out.println("Symbol: B");
if ((mark >= 60) && (mark <= 69))
System.out.println("Symbol: C");
if ((mark >= 50) && (mark <= 59))
System.out.println("Symbol: D");
if ((mark >= 0) && (mark <= 49))
System.out.println("Symbol: F");

if ((mark < 0) || (mark >100)


VERY inefficient, because EVERY condition (consisting out of 2 sub-conditions!) will be
evaluated/tested EVERY time  Even if mark is 85, all 10 conditions will be tested!
DO NOT EVER USE THIS METHOD – IT WILL COST YOU MARKS!
Option 2: Using NESTED if statements, each describing one interval

if ((mark >= 80) && (mark <= 100))


System.out.println("Symbol: A");
else if ((mark >= 70) && (mark <= 79))
System.out.println("Symbol: B");
else if ((mark >= 60) && (mark <= 69))
System.out.println("Symbol: C");
else if ((mark >= 50) && (mark <= 59))
System.out.println("Symbol: D");
else if ((mark >= 0) && (mark <= 49))
System.out.println("Symbol: F");
else
System.out.println("Invalid mark entered");

The nesting makes it much more efficient, because for a mark of 85, only the 2 conditions in the
first compound condition will be tested, the others are all under the else (false) part.
BUT, a mark of 30 or a -10 value, will still cause ALL 10 conditions to be tested…..
Option 3a: Using NESTING, but in such a way, that we DO NOT have to describe the WHOLE
interval each time

if (mark <= 100)


if (mark <= 79)
if (mark <= 69)
if (mark <= 59)
if (mark <= 49)
if (mark < 0)
System.out.println("Invalid mark entered");
else // mark is not < 0, it's between 0 and 49
System.out.println("Symbol: F");
else // mark is not <= 49, it's between 50 and 59
System.out.println("Symbol: D");
else // mark is not < 59, it's between 60 and 69
System.out.println("Symbol: C");
else // mark is not < 69, it's between 70 and 79
System.out.println("Symbol: B");
else // mark is not < 79, it's between 80 and 100
System.out.println("Symbol: A");
else // mark is not < 100, it's > 100
System.out.println("Invalid mark entered");

The nesting, as well as the way we structured the conditions, makes it even more efficient. For a
mark of 85, only the first 2 single conditions will be tested.
A mark of 30 or a -10 value, will cause 6 conditions to be tested (not 10 like before).
Option 3b: Using NESTING, but in such a way, that we DO NOT have to describe the WHOLE
interval each time

if (mark > 0)
if (mark > 49)
if (mark > 59)
if (mark > 69)
if (mark >79)
if (mark > 100)
System.out.println("Invalid mark entered");
else // mark is not > 100, it's between 80 and 100
System.out.println("Symbol: A");
else // mark is is not > 79, it's between 70 and 79
System.out.println("Symbol: B");
else // mark is is not > 69, it's between 60 and 69
System.out.println("Symbol: C");
else // mark is is not > 59, it's between 50 and 59
System.out.println("Symbol: D");
else // mark is is not > 49, it's between 0 and 49
System.out.println("Symbol: F");
else // mark is is not > 0, it's < 0
System.out.println("Invalid mark entered");

For a mark of 85, all 6 single conditions will be tested.


A mark of 30 will cause 2 conditions to be tested (not 10 like before).
A -10 value, will cause only 1 condition to be tested (not 10 like before)

You might also like