LBEPS Session 13

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 29

Logic Building and Effective

Problem Solving
Objectives

In this chapter, you will learn to:


Identify operators
Perform conditional execution
3.3 Let’s Practice

Let us perform the following exercises:


1. Find the result of the following expressions:
5+6*(4/2-1)
28/2+5-(6*3-5)
14/2+2>8

Solution:
11
6
True
3.3 Let’s Practice (Contd.)

2. Find the result of the following expressions:


((15/3)>2) OR ((12/4)>2)
((15/5)==(6-3)) AND ((6+12)>(6-8))

Solution:
True
True
3.3 Let’s Practice (Contd.)

3. Write a pseudocode to evaluate the following mathematical


expression:
nResult=n*(n-1)/n

Solution:
begin
numeric nResult,n
accept n
compute nResult = n*(n-1)/n
display ‘The result of expression is ‘ + nResult
end
Conditional Execution

Consider an example where two values have to be accepted


from a user. The values have to be then compared, and a
result indicating whether they are equal or not has to be
displayed.
The following Word document shows the flowchart for the
preceding example.

Compare Values
Conditional Execution
(Contd.)

While writing the pseudocode, you can check for conditions by


using the decision-making constructs.
The following types of decision-making constructs can be used
in a pseudocode:
if construct
switch…case construct
The if Construct

The if construct:
Specifies a conditional expression that needs to be evaluated.
Is of the following types:
Simple if construct
if…else construct
Nested if…else construct
The if Construct (Contd.)

The simple if construct:


In a pseudocode, a condition can be evaluated using the simple
if construct, as shown in the following pseudocode segment:
if <condition>
begin
<statements to be executed if condition is true>
end
The if Construct (Contd.)

For example, the following pseudocode segment compares two


numbers:
begin

if nNum1 == nNum2 // beginning of if statement
begin
display ‘The numbers are equal’
end
end
The if Construct (Contd.)

The if…else construct:


The if…else construct can be represented in a pseudocode, as
shown in the following pseudocode segment:
if <condition>
begin
<statements to be executed if condition is true>
end
else
begin
<statements to be executed if condition is false>
end
The if Construct (Contd.)

Consider an example of a pseudocode that evaluates whether


a number is even or odd.
Click the following link to view an animation showing the
pseudocode for the preceding example.

Even Odd
The if Construct (Contd.)

Nested if…else construct:


The following pseudocode represents the nested if…else
construct:
begin
if <condition> // beginning of if statement
begin
<statements to be executed if condition is true>
end
else
begin
The if Construct (Contd.)

if <condition>
begin
<statements to be executed if condition is
true>

end
else
begin
<statements to be executed if condition is
false>
end
end
end
The if Construct (Contd.)

Consider another example, where the discount percentage on a


TV needs to be decided on the basis of the type of TV. If the
TV is CRT (C), the discount will be 5% of the selling price
(SP). If the TV is LCD (L), then the discount will depend on
the size of the TV screen. For 14 inches screen, the discount is
8% of the SP. For 21 inches screen, the discount is 10% of the
SP. The following table summarizes the discount rates.

Discount Rates
Color Size Discount Rate
CRT - 5
LCD 14 8
LCD 21 10
The if Construct (Contd.)

The following Word document shows the pseudocode segment


for the preceding example.

Discount
The if Construct (Contd.)

Consider a scenario where all candidates have to take two tests


before appearing for an interview. A candidate is selected for the
interview round, based on the scores of the two tests. The
individual score in each test should be greater than 75, and the
average score for the two tests should be a minimum of 80. A
call letter for the interview is to be sent to candidates who have
been selected, and a rejection letter is to be sent to the rest.
Click the following link to view an animation for the preceding
problem.

Selection of
Candidates
The switch….case Construct

The switch…case construct:


Is used when a variable needs to be compared with multiple
values to make a decision.
Enables you to make a decision by selecting from a number of
choices.
Consists of one switch statement, a number of case
statements, and a default statement.
The switch….case Construct
(Contd.)

The switch…case construct is shown in the following


pseudocode segment:
begin

switch (expression)
begin
case constant 1:
//execute the statements within the
case //block
break
case constant 2:
//execute the statements within the
case //block
break
The switch….case Construct
(Contd.)

case constant 3:
//execute the statements within the
case //block
break
case constant 4:
//execute the statements within the
case //block
break
default:
//execute the statements within the default
//block
end

end
The switch….case Construct
(Contd.)

The following Word document shows the pseudocode segment


to accept a number from 1 to 7 and display the corresponding
day of week.

Days of the week


The switch….case Construct
(Contd.)

The same pseudocode can be implemented by using the


switch…case construct.
The following pseudocode segment depicts the steps required
to accomplish the requirements specified in the preceding
example:
begin
numeric nNum
display ‘Enter a number’
accept nNum
switch (nNum)
begin
case 1:
display ‘Sunday’
break
The switch….case Construct
(Contd.)

case 2:
display ‘Monday’
break
…………
…………
…………
case 7:
display ‘Saturday’
break
default:
display ‘You have entered a number
out of range’
end
end
The switch….case Construct
(Contd.)

Consider another example where you need to write a


pseudocode to accept two numbers and any one of the
operators: +, -, *, and /. Based on the operator entered,
the pseudocode should add, subtract, multiply, or divide the
numbers and display the result.
Click the following link to view an animation showing the
pseudocode for the preceding example.

Calculator
The switch….case Construct
(Contd.)

Consider the problem of automated telephone call transfer to


various departments of the company, such as Marketing,
Finance, Customer Care, Human Resource (HR), and
Information.
Click the following link to view an animation showing the
pseudocode for the preceding example.

Call Transfer
The switch….case Construct
(Contd.)

Consider another example where you need to write a


pseudocode that accepts a letter from the alphabet and checks
whether it is a vowel.
The following Word document shows the pseudocode segment
to accept an alphabet and display whether it is a vowel or
not.

vowel
Just a Minute

Which one of the following statements is used to exit from a


switch…case after the condition specified in the case
statement is met and the case block is executed?
1. switch statement
2. break statement
3. default statement
4. execute statement

Solution:
2. break statement
Summary

In this chapter, you learned that:


The following types of decision-making constructs can be used in
a pseudocode:
if construct
switch…case construct
The if decision-making construct specifies a conditional expression
that needs to be evaluated.
The switch…case construct is used when you need to evaluate
a large number of conditions.
What’s Next?

Before the next session, please ensure to:


Complete the read-through of LBEPS Chapter 3.
Complete the Lab@Home exercises as per the learning plan.
The learning plan is available on the technology page.
Read Best Practices of Knowledge Byte 3 through CloudScape.
Go through the further Additional Reference Links of LBEPS
Chapter 3 through CloudScape

You might also like