LN #2 (3 Hrs) Variables, Sequence Boolean Logic & Selection CTPS 2018
Objectives To understand variables and their values. To study the computational structure, form and functional elements for sequence & selection. To learn Boolean logic.
Variables A variable is like a box, a place holder, a memory location where is stored data in.
Let answer be a variable Get the value from user. Let us say the user types 2 The value 2 is stored in the variable answer
Access the value 2 stored in variable answer and do some processing if (answer is equal to 2) then print "You chose to go left " else print " a most dangerous path"
Variable x x
X = 0 X = X+1
X = 0 X = X+1
X = 0 X = X+1
X = 0 X = X+1
X = 0 X = X+1
Sequence Is a series of steps that take place one after another. 1. Enter the name 2. Enter mark1 3. Enter mark2 4. Enter mark3 5. Total = mark1+mark2+mark3 6. Average = Total / 3 7. Print Total, Average
Normally, statements in a program are executed sequentially, in the order that they were written.
Many solutions feature several choices or decisions. These decisions lead to different paths. These paths represent the result of making a choice.
Selection Is a decision Decisions may be answered as: Yes or No True or False Based on the answer ( Y/N or T/F) a path is chosen
Robots are dumb What does a robot need to know, to solve a maze? What commands and behaviors would be useful? R
R
R
THERE IS EMPTY SPACE R MOVE FORWARD
Conditionals
Boolean Value Anything that results in TRUE or FALSE can go into the if part of a conditional
Boolean Logic A way to figure out the truth of an expression using the simple concept oftrue or FALSE. Boolean logic means you are working with stuff that is eithertrue or FALSE.
Consider sunny_day. A day can either be sunny or not. so, sunny_day can be eithertrue or FALSE. sunny_day is a Boolean Variable that can have a Boolean Value(TRUE or FALSE). Now, say you want to go body surfing at the beach, but you don't want to go when it's cold or rainy, just when it is sunny. You can use Boolean logic
Boolean Expression IF (sunny_day) THEN go body surfing at the beach The Boolean expression sunny_day will have a truth value of TRUE or FALSE
Now let's say you don't swim so well. You need to have your water_wings on too. You now add water_wings to the conditional.
Boolean Expression IF (sunny_day) AND (water_wings) THEN go body surfing at the beach
Boolean Expression IF (sunny_day) AND (water_wings) THEN go body surfing at the beach The Boolean expression will have a truth value of TRUE or FALSE
Boolean Expression IF (sunny_day) AND (water_wings) THEN go body surfing at the beach If both of sunny_day and water_wings are TRUE, then the output (truth value) istrue ie, you head to the beach.. But if even one of these are FALSE, then the output (truth value) is FALSE ie,you stay home and dream of the beach
Operator AND Everything has to be TRUE true AND true = true true AND false AND true = false false AND false = false
Operator OR Only one thing has to be TRUE true OR true = true true OR false OR true = true false OR false = false
Expressions An expression is simply one or more variables and/or constants joined by operators An expression is evaluated and produces a result The result of all arithmetic expressions are either integers or reals An expression can also yield a result that is either true or false- BOOLEAN Such an expression is called a relational expression The result reflects how something "relates to something else. 32 Department of CSE
"Is the value of x greater than the value of y? Note that the preceding poses a question. Relational expressions are usually intended to answer yes/no, or true/false, questions. Obviously, boolean values and boolean variables play an important role in relational expressions. 33 Department of CSE
Operators To build relational expressions, two types of operators are used, relational operators and logical operators Relational operators Logical operators 34 Department of CSE
Expression Value of expression 3 < 4 True 7.6 <= 9 True 4 == 7 False 8.3!= 2.1 True Initial values a = 3 b=4 c=5 d=6 Expression a ==b c< d (a==b) && (c<d) (a==b) && (c<d) Value of expression False True False True result = (a==b) && (c<d)!result False true 35 Department of CSE
Truth assignment: TRUE or FALSE Let, (a < b (a >= b && c == d)) be statement 1 (a < b c == d) be statement 2 In the statements 1 and 2: a < b, c == d, a >= b are conditions to be checked fortrue or FALSE to determine the truth value of the entire expression
Logical expression (a < b (a >= b && c == d)) statement 1 (a < b c == d).. statement 2 # A. Let, a is less than b be True We inspect the first of the two conditions (a < b) to see if it is true It is true in both statements 1 and 2 TRUE is returned by both the statements
# B. Let, a is less than b be FALSE In statement 1 : We inspect the second of the two conditions (a >= b && c == d) to see if it is true We are asking whether both a >= b AND c == d are true If a < b is false, then a >= b is of course true
Therefore whether true or false is returned entirely depends on the condition : c == d If c == d is true then true is returned [ as a < b is false, a >= b is true ] the statement 1 returns true If c == d is false and false is returned [ as a < b is false, a >= b is true ] the statement 1 returns false
In statement 2 : We inspect the second of the two conditions c == d If c == d is true then true is returned a < b is false, the statement 2 returns true If c == d is false and false is returned as a < b is false, the statement 2 returns false
Evaluate the Boolean expressions 1. (T AND (NOT F) OR (T AND (NOT F))) 2. ((NOT F) OR (F AND (NOT T)) AND F)
Can you say how many choices are available for selection?
Multiple Choices One Choice Two Choices Multiple Choices
Selection Statements Selection statement gives a program the ability to choose which instruction(s) to next execute based on conditions. Types 1-Way selection (if statement). 2-Way selection (if-else statement). Multi-Way selection (or n-way selection, switch/case).
Considerations What is the form and type of expression that controls the selection? How are clauses specified if at all? If nesting is allowed, how is it specified and implemented?
One-Way Selection The if structure is a one-way selection structure. When a control expression in an if statement is evaluated to be true, the statements associated with the structure are executed. IF condition ENDIF THEN actions Figure: Selection - One Way
IF (I have a fishing pole) ENDIF THEN I am going fishing IF (orderamount < 40 ) ENDIF THEN ShippingCost =10
Model the following using one-way selection structure: England will qualify for the quarter-finals with Portugal if they win or draw with Romania. If the shop has Croissants buy croissants and leave the shop. If you like salty food then add the amount of salt you prefer.
IF England wins/draws with Romania THEN They will qualify for quarter-finals with Portugal IF the shop has Chips THEN buy Chips IF you like salty food THEN add the amount of salt you prefer
Two-Way Selection The if/else structure is a two-way selection structure. If the control expression in the if statement evaluates to true, one block of statements is executed; otherwise (else), another block is executed. IF condition THEN if-true actions ELSE if- false actions ENDIF Figure: Selection - Two Way
IF (the sun is shining) THEN I will go fishing ELSE I will play computer games IF (orderamount < 40 ) THEN ShippingCost = 10 ELSE ShippingCost = 0 ENDIF
Model using Two-Way selection structure: If there is a fire outside the door then go to another exit. Otherwise, remove the flap covering the handle, turn the handle, pull the door into the plane and throw it out the doorway, as far away as possible.
IF there is a fire outside the door THEN Go to another exit. ELSE 1. Remove the flap covering the handle. 2. Turn the handle. 3. Pull the door into the plane 4. Throw it out the doorway, as far away as possible
Complex Conditionals
Selection from Multiple Choices 1 The problem of determining a students letter grade given the numerical grade.
Consider the requirements for computing shipping cost based on the information given in table(shipping Cost Policy)
IF (orderamount > 0 AND orderamount < 20 ) THEN ShippingCost = 10 ELSE IF (orderamount >= 20 AND orderamount < 40) THEN ShippingCost = 5 ELSE ShippingCost = 0 ENDIF ENDIF
Find the largest of the three numbers A,B and C IF ( A >B ) THEN IF ( A > C ) THEN print A is largest ELSE print Cis largest END IF ELSE IF ( B > C ) THEN print B is largest ELSE print C is largest END IF END IF
Model using Multi-Way selection structure: The phone systems giving you instructions in this form. The message will say something like this: 1. If you wish to buy a ticket then press 1. 2. If you wish to listen to some music then press 2. 3. If you wish to listen to recorded information then press 3. 4. If you wish to speak to an operator then press 4. 5. If you wish to exit press 5.
if ( you wish to buy a ticket ) then press 1 else if ( you wish to listen to some music ) then press 2 else if ( you wish to listen to recorded information ) then press 3 else if ( you wish to speak to the operator ) then press 4 else press 5.
Calculate the sales tax and total amount due for different tax codes. tax code 0: sales_tax = 0 tax code 1: sales_tax = purch_amt * 0.03 tax code 2: sales_tax = purch_amt * 0.05 tax code 3: sales_tax = purch_amt * 0.07
Model using appropriate selection structure: 1. IF the user has struck the Confirm Purchase button, THEN initiate billing procedures. 2. IF the camera sensor detects adequate exposure, THEN close the camera shutter. 3. IF a song has just finished playing, THEN begin playing the next song in the playlist.
4. Shop opening timings 5. Which selection structure is suitable for allowing the users to perform basic operations(add, sub, mul, div, modulo division)?...can you model it?
4. if the day is Monday then the opening hours are 8am-8pm. elseif the day is Tuesday then opening hours are 8am-12pm. elseif the day is Wednesday then opening hours are 8am-8pm. elseif the day is Thursday then opening hours are 8am-8pm. elseif the day is Friday then opening hours are 8am-8pm. elseif the day is Saturday then opening hours are 9am-5pm. else the day is Sunday and the opening hours are 10am-4pm.
Predict the output
What has been described? Variables and values Sequence and Selection Expressions & Operators Boolean logic IF construct Credits Programming Languages,2nd edition,tucker and Noonan www.cse.msu.edu/ Organization of Programming Languages-Cheng (Fall 2004) Computing Without Computers,A Gentle Introduction to Computer Programming,Data Structures and Algorithms,Version 0.15,Paul Curzon Google images