Академический Документы
Профессиональный Документы
Культура Документы
(3 Hrs)
Variables, Sequence
Boolean Logic & Selection
CTPS 2018
Department of CSE,Coimbatore
Objectives
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
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.
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
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).
IF (sunny_day)
THEN go body surfing at the beach
Department of CSE,Coimbatore
Boolean Expression
Department of CSE,Coimbatore
Boolean Expression
Department of CSE,Coimbatore
Boolean Expression
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
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
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:
▪ 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 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:
Department of CSE,Coimbatore
Department of CSE,Coimbatore
Selection from Multiple Choices
✓1
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:
Department of CSE,Coimbatore
4. Shop opening timings
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
• 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