Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science Selection Structures.

Similar presentations


Presentation on theme: "Computer Science Selection Structures."— Presentation transcript:

1 Computer Science Selection Structures

2 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

3 Relational Operators Relational operators are also called comparative operators because they compare things They 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

4 Comparing Numbers If you have two numbers: Let A=10 and B=5 Then:
A > B is true A < B is false A >= B is true A <= B is false A != B is true A == B is false

5 Comparison vs Assignment
Comparison 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

6 Logical Operators AND OR NOT
Logical operators are used to connect simple conditions into a more complex condition called a compound conditions. The logical operators are: AND OR NOT

7 If (X > 5) AND (X < 10) Then …
The AND Operator Joins two conditions Both 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.

8 If (X > 5) OR (X < 10) Then …
The OR Operator Joins two conditions One 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

9 The NOT Operator AND 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 statement If ( X > 100) AND NOT ( X == Y) Then… is true only if X is greater than 100 but not equal to the value of Y.

10 NOT A > 3 OR B < 3 AND A – B <= 0
Combining Operators Example: Let A = 3 and let B = 5 Is the following expression true or false? NOT A > 3 OR B < 3 AND A – B <= 0 (NOT(false)) OR ((false) AND (true)) (true) OR (false) true

11 Truth Tables X Y X OR Y X AND Y NOT X true false

12 Hierarchy of Operations
Type Operator Order Performed Arithmetic operations are performed first, in order shown ( ) ^ * / % + - 1st parentheses 2nd exponentiation 3rd: multiplication, division, modulus 4th: 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: NOT 2nd: AND 3rd: OR

13 A note on ASCII code Recall 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

14 Why mention ASCII? These schemes affect how operators handle strings in a programming language If 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

15 Making Choices From the flow chart activity we saw how we can create algorithms that make choices The way a program can handle these choices are known as Selection Structures or If-Else statements If a condition is true then a statement is executed, else it is not. Therefore the program selects to do one thing or the other.

16 Types of Selection Structures
Single-alternative (If-Then) A single block of statements to be executed or skipped Dual-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

17 Single Alternative – Simple If
The single alternative is the most basic structure. It uses one if statement If condition is true Then statement is executed otherwise the statement is skipped Pseudocode Example If Age >= 18 Set Eligibility = “Yes” Do other things… End If Condition Statement T F

18 Dual Alternative – two branch
The dual alternative uses two statements If conditions is true then the first statement is executed else the second statement is executed Pseudocode Example If Age >= 18 Set Eligibility = “Yes” Else Set Eligibility = “No” End If Condition Statement1 T F Statement2

19 Example in Java class 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"); }

20 Multi Alternative – Multi branch
The 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’;

21 Flowchart for a Multiple Alternative

22 Nested IF statements A nested statement is a statement within another statement You 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 Cond2 StmtN CondN StmtN+1 . . .

23 Multiple Alternative – Switch/Case
The 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 Example Switch Choice Case 1: Set Operation = “Add” Case 2: Set Operation = “Subtract” Case 3: Set Operation = “Multiply” End Case

24 Example in Java class 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); case 3:System.out.println(score * 0.2); } System.out.print("is the weighted score of test " + testNum + " with a grade of " + score);


Download ppt "Computer Science Selection Structures."

Similar presentations


Ads by Google