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

Logic Building and Effective

Problem Solving
Objectives

In this session, you will learn to:


Perform conditional execution
Implement iterative processes
3.4 Let’s Practice

Let us perform the following exercises:


1. Write a pseudocode to accept five numbers and to display the
highest number.

Solution:
The following Word document shows the solution.

Exercise 1
3.4 Let’s Practice (Contd.)

2. Write a pseudocode to accept hours and number of resources


and display the total number of productive hours. (Total number
of productive hours = hours * number of resources).

Solution:
begin
numeric nHrs, nResources, nPrdHrs
accept nHrs
accept nResources
compute nPrdHrs = nHrs * nResources
display ‘The total productive hours is ‘ + nPrdHrs
end
3.4 Let’s Practice (Contd.)

3. Write a pseudocode to accept three numbers and to display the


numbers in the ascending order.

Solution:
The following Word document shows the solution.

Exercise 3
3.4 Let’s Practice (Contd.)

4. Write a pseudocode to accept the age of the user and then,


display the category of the user. Based on the age, the
categories of the users are:
05-12 Children
13-19 Teenagers
20-35 Young adults
36-45 Mature adults
46-55 Elders
56 and above Seniors
Solution:
The following Word document shows the solution.

Exercise 4
Implementing Iterative
Processes

A loop is a sequence of instructions that is repeated more


than once.
A loop must always perform certain steps in a specified
sequence.
The types of loops that can be used in a pseudocode to
perform repeated tasks are:
for
while
repeat…until
The for Loop

The for loop:


Provides a compact way of specifying the statements that control
the repetition of the steps within the loop.
Consists of the following three parts, each separated by a
semicolon:
Initialization expression
Test expression
Increment/decrement expression
Has the following syntax:
begin
for (Initialization expression; Test expression;
Increment/decrement expression)
begin
//Body of the loop
end
end
The for Loop (Contd.)

The following Word document shows the flowchart that depicts


the for loop syntax.

Syntax

The following Word document shows the flowchart to display the sum
of 10 numbers.

Sum of 10
Numbers
The for Loop (Contd.)

The following pseudocode segment represents the preceding


flowchart:

begin
numeric nNum, nSum, nCounter
nCounter = 0, nSum = 0
for(nCounter = 0;nCounter < 10;nCounter = nCounter +
1)
begin
accept nNum
nSum = nSum + nNum
end
display nSum
end
Just a Minute

The for loop consists of _______ parts, each separated by a


semicolon.
1. two
2. three
3. four
4. five

Solution:
1. three
The while loop

The while loop:


Executes as long as the condition specified with the loop is true.
Checks the condition before starting the loop, and the statements
inside the loop execute as long as the specified condition is true.
Has the following syntax:
:
:
while (condition) //while loop with test expression.
begin
//Body of the while loop
end
:
:
The while Loop (Contd.)

The following Word document shows the flowchart that depicts


the while loop syntax.

Syntax

Consider an example where you have been assigned the task


of preparing the test performance report of the candidates
appearing for an interview. All the candidates have to take
three tests. The individual score in each test has to be
greater than 75, and the average score across the three tests
should be a minimum of 80. A candidate is selected based
on the preceding criteria. The number of candidates selected
and rejected needs to be displayed. In addition, a message
for sending call letters to the selected candidates needs to be
displayed.
The while Loop (Contd.)

The following Word document shows the flowchart for the


preceding example.

Selection
Flowchart

The following Word document shows the pseudocode segment


for the preceding example.

Selection
Pseudocode
4.1 Let’s Practice

Let us perform the following exercises:


1. Write a pseudocode to display the sum of the first 10 odd
numbers using the for loop. Draw a flowchart for the same.

Solution:
The following Word document shows the flowchart to display the sum
of the first 10 odd numbers.

Exercise 1
Flowchart
The following Word document shows the pseudocode to display the
sum of the first 10 odd numbers.

Excercise 1
Pseudocode
Summary

In this session, you learned that:


A loop is a sequence of instructions that is repeated more than
once.
The for loop construct provides a compact way of specifying the
statements that control the repetition of the steps within the loop.
The while loop executes as long as the condition specified with
the loop is true.
What’s Next?

Before the next session, please ensure to:


Read LBEPS Chapter 4.
Complete the Lab@Home exercises as per the learning plan. The
learning plan is available on the technology page.
Go through Additional Reference Links of LBEPS Chapter 4
through CloudScape.

You might also like