Selection Structures. Relational & Logical Operators In the previous unit we learned about...

24
Computer Science Selection Structures

Transcript of Selection Structures. Relational & Logical Operators In the previous unit we learned about...

Page 1: Selection Structures. Relational & Logical Operators In the previous unit we learned about arithmetic operators. They allowed us to generate new values.

Computer ScienceSelection Structures

Page 2: Selection Structures. Relational & Logical Operators In the previous unit we learned about arithmetic operators. They allowed us to generate new values.

Relational & Logical Operators

In the previous unit we learned about arithmetic operators. They allowed us to generate new values in a program

In this unit you will be introduced to relational and logical operators

Relational operators enable you to compare values and from that comparison make decisions

Logical operators also known as Boolean operators decide whether an expression is true or false and enable you to combine conditions

Page 3: Selection Structures. Relational & Logical Operators In the previous unit we learned about arithmetic operators. They allowed us to generate new values.

Relational OperatorsRelational operators are also called comparative

operators because they compare thingsThey symbols used in programming for these operators

are:= = equal to (comparison)!= not equal to< less than> greater than<= less than or equal to>= greater than or equal to

Page 4: Selection Structures. Relational & Logical Operators In the previous unit we learned about arithmetic operators. They allowed us to generate new values.

Comparing NumbersIf you have two numbers:

Let A=10 and B=5 Then:

A > B is trueA < B is falseA >= B is trueA <= B is falseA != B is trueA == B is false

Page 5: Selection Structures. Relational & Logical Operators In the previous unit we learned about arithmetic operators. They allowed us to generate new values.

Comparison vs AssignmentComparison Assignment

= = =What is the difference?

The Assignment Operator (=) Changes the value of the variable on the right side to the left side

Example: Given A=3 and B=5 then for A=B, A becomes 5

The Comparison Operator (= =) Compares the right side to the left sides and asks if they are equal or if

its true Example: Example: A=3 and B=5 then for A==B, then the equation

is FALSE

Page 6: Selection Structures. Relational & Logical Operators In the previous unit we learned about arithmetic operators. They allowed us to generate new values.

Logical Operators

Logical operators are used to connect simple conditions into a more complex condition called a compound conditions.

The logical operators are:

AND OR NOT

Page 7: Selection Structures. Relational & Logical Operators In the previous unit we learned about arithmetic operators. They allowed us to generate new values.

The AND Operator

Joins two conditionsBoth conditions must be true for the statement to be

true. It is false if even one of the conditions is false. Example: The statement:

If (X > 5) AND (X < 10) Then …is true only if X is 6, 7, 8, or 9.

Page 8: Selection Structures. Relational & Logical Operators In the previous unit we learned about arithmetic operators. They allowed us to generate new values.

The OR Operator

Joins two conditionsOne condition must be true for the statement to be

true. It is false only if both conditions are false. Example: The statement:

If (X > 5) OR (X < 10) Then …is true for all numbers

Page 9: Selection Structures. Relational & Logical Operators In the previous unit we learned about arithmetic operators. They allowed us to generate new values.

The NOT OperatorAND and OR affect 2 simple conditions.NOT negates the condition. It makes the condition the

opposite of what is given. A condition with the NOT operator is true only if the

condition is false.Example: The statement:

NOT ( A < B) is true only if B is greater than or equal to A.

Example: The statementIf ( X > 100) AND NOT ( X == Y) Then…

is true only if X is greater than 100 but not equal to the value of Y.

Page 10: Selection Structures. Relational & Logical Operators In the previous unit we learned about arithmetic operators. They allowed us to generate new values.

Combining Operators

Example:Let A = 3 and let B = 5Is the following expression true or false?

NOT A > 3 OR B < 3 AND A – B <= 0

1. (NOT(false)) OR ((false) AND (true))2. (true) OR (false)3. true

Page 11: Selection Structures. Relational & Logical Operators In the previous unit we learned about arithmetic operators. They allowed us to generate new values.

Truth Tables

X Y X OR Y X AND Y NOT X

true true true true false

true false true false false

false true true false true

false false false false true

Page 12: Selection Structures. Relational & Logical Operators In the previous unit we learned about arithmetic operators. They allowed us to generate new values.

Hierarchy of OperationsType Operator Order Performed

Arithmetic operations are performed first, in order shown

( )^

* / % + -

1st parentheses2nd exponentiation3rd: multiplication, division, modulus4th: addition, subtraction

Relational operations are performed second

== != < <= > >=

All relational operators have equal precedence

Logical operations are performed last, in the order shown

NOT AND (&&)OR (||)

1st: NOT2nd: AND 3rd: OR

Page 13: Selection Structures. Relational & Logical Operators In the previous unit we learned about arithmetic operators. They allowed us to generate new values.

A note on ASCII codeRecall all data, including characters are stored in the

computer’s memory in binary form.A programming language uses a method or scheme to

associate each character with a number so that it can be converted into binary

One such scheme is called ASCII code American Standard Code for Information Interchange

-- pronounced “askey.”

In ASCII each character is associated with a number from 0 to 127

For a complete list see text: Prelude to Programming (Venit) on page 131

Page 14: Selection Structures. Relational & Logical Operators In the previous unit we learned about arithmetic operators. They allowed us to generate new values.

Why mention ASCII?These schemes affect how operators handle strings in a

programming languageIf you look at the ASCII table you will notice the

following is true:“a” > “A” “1” <= “5”“2” >= “2”

The program compares ASCII numbers, not the characters you write

Character ASCII #

a 97

A 65

1 49

2 50

5 53

Page 15: Selection Structures. Relational & Logical Operators In the previous unit we learned about arithmetic operators. They allowed us to generate new values.

Making ChoicesFrom the flow chart activity we saw how we can create

algorithms that make choicesThe way a program can handle these choices are known

as Selection Structures or If-Else statementsIf a condition is true then a statement is executed, else it

is not.Therefore the program selects to do one thing or the

other.

Page 16: Selection Structures. Relational & Logical Operators In the previous unit we learned about arithmetic operators. They allowed us to generate new values.

Types of Selection StructuresSingle-alternative (If-Then)

A single block of statements to be executed or skippedDual-alternative (If-Then-Else)

Two blocks of statements, one of which is to be executed, while the other one is to be skipped

Multiple-alternative (If-Then-Else-If or Case) More than two blocks of statements, only one of which is to be

executed and the rest skipped

Page 17: Selection Structures. Relational & Logical Operators In the previous unit we learned about arithmetic operators. They allowed us to generate new values.

Single Alternative – Simple IfThe single alternative is the most basic structure. It uses

one if statement If condition is true Then statement is executed otherwise

the statement is skippedPseudocode ExampleIf Age >= 18Set Eligibility = “Yes”Do other things…

End If

Condition

Statement

T

F

Page 18: Selection Structures. Relational & Logical Operators In the previous unit we learned about arithmetic operators. They allowed us to generate new values.

Dual Alternative – two branchThe dual alternative uses two statements

If conditions is true then the first statement is executed else the second statement is executed

Pseudocode ExampleIf Age >= 18Set Eligibility = “Yes”ElseSet Eligibility = “No”End If

Condition

Statement1

T F

Statement2

Page 19: Selection Structures. Relational & Logical Operators In the previous unit we learned about arithmetic operators. They allowed us to generate new values.

Example in Javaclass IfDemo{ public static void main (String[] args) { System.out.println("Please give one integer"); int first = In.getInt(); System.out.println("and a second"); int second = In.getInt(); if (first == second) System.out.println("The values are equal"); else System.out.println("The values are not equal"); }}

Page 20: Selection Structures. Relational & Logical Operators In the previous unit we learned about arithmetic operators. They allowed us to generate new values.

Multi Alternative – Multi branchThe multi alternative uses more than two statements

If condition is true then the first statement is executed else the second statement is executed else the third statement is executed and so on..

Pseudocode Example if (score >= 90) grade = ‘A’; else if (score >= 80) grade = ‘B’; else if (score >= 70) grade = ‘C’; ... else if (score >= 60) grade = ‘D’; else grade = ‘F’;

Page 21: Selection Structures. Relational & Logical Operators In the previous unit we learned about arithmetic operators. They allowed us to generate new values.

Flowchart for a Multiple Alternative

Page 22: Selection Structures. Relational & Logical Operators In the previous unit we learned about arithmetic operators. They allowed us to generate new values.

Nested IF statementsA nested statement is a statement within another

statementYou can nest if statements to determine if both of two

Boolean expressions are true, or if either of the expressions is true.

Cond1

Stmt1

T F

Stmt2

Cond2T F

StmtN

CondNT F

StmtN+1

. . .

Page 23: Selection Structures. Relational & Logical Operators In the previous unit we learned about arithmetic operators. They allowed us to generate new values.

Multiple Alternative – Switch/CaseThe switch statement is similar to an if /else, if /else

statement. It compares that value to two or more other values to

determine which code to execute. Pseudocode ExampleSwitch Choice

Case 1:Set Operation = “Add”

Case 2:Set Operation = “Subtract”

Case 3:Set Operation = “Multiply”

End Case

Page 24: Selection Structures. Relational & Logical Operators In the previous unit we learned about arithmetic operators. They allowed us to generate new values.

Example in Javaclass AvgTest{ public static void main(String[] args) {System.out.println("Please enter the test number.");int testNum = In.getInt();System.out.println("Please enter the score.");int score = In.getInt();switch (testNum){ case 1: System.out.println(score * 0.3); break; case 2:System.out.println(score * 0.1); break; case 3:System.out.println(score * 0.2); break;}System.out.print("is the weighted score of test " + testNum + " with a grade of " + score); }}