Вы находитесь на странице: 1из 67

LN #2

(3 Hrs)

Variables, Sequence
Boolean Logic & Selection

CTPS 2018

Department of CSE,Coimbatore
Objectives

❖To understand variables and their values.

❖To study the computational structure, form and

functional elements for sequence & selection.


❖To learn Boolean logic.

Department of CSE,Coimbatore
Variables
A variable is like a box , a place holder, a memory
location where is stored data in.

Department of CSE,Coimbatore
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

Department of CSE,Coimbatore
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"
Department of CSE,Coimbatore
Variable x

Department of CSE,Coimbatore
X=0
X = X+1

Department of CSE,Coimbatore
X=0
X = X+1

Department of CSE,Coimbatore
X=0
X = X+1

Department of CSE,Coimbatore
X=0
X = X+1

Department of CSE,Coimbatore
X=0
X = X+1

Department of CSE,Coimbatore
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
Department of CSE,Coimbatore 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

Department of CSE,Coimbatore
Department of CSE,Coimbatore
Robots are “dumb”…
• What does a robot need to know, to solve a maze?
• What commands and behaviors would be useful?

Department of CSE,Coimbatore
R

Department of CSE,Coimbatore
R

Department of CSE,Coimbatore
THERE IS EMPTY SPACE

R
MOVE FORWARD

Department of CSE,Coimbatore
Conditionals

Department of CSE,Coimbatore
Boolean Value

✓Anything that results in TRUE or FALSE can go into


the if part of a conditional
Department of CSE,Coimbatore
Boolean Logic

• A way to figure out the truth of an expression using the


simple concept of TRUE or FALSE.

• Boolean logic means you are working with stuff that is


either TRUE or FALSE.

Department of CSE,Coimbatore
• Consider sunny_day.
• A day can either be sunny or not. so, sunny_day can be
either TRUE 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…
Department of CSE,Coimbatore
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
Department of CSE,Coimbatore
• 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.

Department of CSE,Coimbatore
Boolean Expression

IF (sunny_day) AND (water_wings)


THEN go body surfing at the beach

Department of CSE,Coimbatore
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

Department of CSE,Coimbatore
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) is TRUE
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 ☺
Department of CSE,Coimbatore
Operator AND

✓Everything has to be TRUE

• true AND true = true


• true AND false AND true = false
• false AND false = false
Department of CSE,Coimbatore
Operator OR

✓Only one thing has to be TRUE

• true OR true = true


• true OR false OR true = true
• false OR false = false
Department of CSE,Coimbatore
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 Expression Value of
values expression
a=3 a ==b False
b=4 c< d True
c=5 (a==b) && (c<d) False
d=6 (a==b) && (c<d) True

result = (a==b) && (c<d) False


!result 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 for TRUE 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)

Department of CSE,Coimbatore
✓Can you say how many choices are available for selection?

Department of CSE,Coimbatore
Multiple Choices

One Choice
Two Choices

Multiple Choices

Department of CSE,Coimbatore
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).
Department of CSE,Coimbatore
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?

Department of CSE,Coimbatore
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
THEN actions
ENDIF
Figure: Selection - One Way

Department of CSE,Coimbatore
IF (I have a fishing pole)
THEN I am going fishing
ENDIF

IF (orderAmount < 40 )
THEN ShippingCost =10
ENDIF
Department of CSE,Coimbatore
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.

Department of CSE,Coimbatore
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

Department of CSE,Coimbatore
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
Department of CSE,Coimbatore
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.
Department of CSE,Coimbatore
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

Department of CSE,Coimbatore
Department of CSE,Coimbatore
Selection from Multiple Choices
✓1

✓ The problem of determining a students


letter grade given the numerical grade.

Department of CSE,Coimbatore
Consider the requirements for computing shipping cost based on
the information given in table(Shipping Cost Policy)

Department of CSE,Coimbatore
IF (orderAmount > 0 AND orderAmount < 20 )
THEN ShippingCost = 10
ELSE
IF (orderAmount >= 20 AND orderAmount < 40)
THEN ShippingCost = 5
ELSE ShippingCost = 0
ENDIF
ENDIF

Department of CSE,Coimbatore
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
Department of CSE,Coimbatore
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.

Department of CSE,Coimbatore
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
.
Department of CSE,Coimbatore
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

Department of CSE,Coimbatore
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.

Department of CSE,Coimbatore
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?

Department of CSE,Coimbatore
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.

Department of CSE,Coimbatore
Predict the output

Department of CSE,Coimbatore
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
Department of CSE,Coimbatore •Google images

Вам также может понравиться