Module2 ktuspecial.in
Module2 ktuspecial.in
JOIN WITH US
Start
Input two
numbers, a and b
Sum = a + b
Print Sum
Stop
Pseudocode
Pseudocode, on the other hand, is a text-based method for describing algorithms using
a combination of natural language and simplified programming constructs. It serves as an
intermediate step between human language and actual code, allowing developers to outline
the logic and structure of an algorithm without adhering to the strict syntax of a particular
programming language. Pseudocode typically includes common programming elements such
as loops, conditionals, and function calls, but expresses them in a more readable and
language- agnostic manner. This approach is particularly useful for planning and
communicating algorithm designs among team members, as it focuses on the core logic and
functionality without getting bogged down in implementation details. Pseudocode and more
examples are covered in detail in Section 2.3.
EXAMPLE 2.3: Develop the pseudocode for finding sum of two numbers a and b
PRINT "Enter first number: "
INPUT a
PRINT "Enter second number: "
INPUT b
sum = a + b
PRINT "Sum =: ", sum
No
2.2.3 Considerations in flow charting
Five rules to follow while developing a flow chart are:-
1. Flow charts for programs should only use the standard symbols specified for various
operations.
2. The flow from left to right and from top to bottom should be shown in the program
logic.
3. Except decision symbol, every symbol used in a flowchart should only have one entry
point and one exit point. This can be called the 'single rule.
4. Symbols of the flow chart should convey the operations inside them without reference
specific programming language. Flow chart is independent of any programming
language.
5. Each decision-making branch needs to have clear labels.
3 There are no rules to follow for Developing flow chart requires some
developing an algorithm rules
4 It involves listing various steps in simple It involves pictorial representation of
language, using plain text various steps and involves graphical
representation or shapes
5 Debugging errors in algorithm is Debugging errors is easier in flow
challenging chart
Input/Output Input is used to receive data from the user INPUT name
or an external source, while Output is used OUTPUT "Hello"
to display information to the user or send it OUTPUT name
to an external destination PRINT "Hi", name
SYNTAX:
SWITCH (variable)
CASE value1:
// code to execute if variable == value1
BREAK
CASE value2:
// code to execute if variable == value2
BREAK
CASE value3:
// code to execute if variable == value3
BREAK
DEFAULT:
// code to execute if variable doesn't match any case
ENDSWITCH
The figure shows the control flow of a switch- case structure in a flowchart begins with the
evaluation of a key expression or variable. This value is then compared against multiple
predefined cases. When a match is found, the flow follows the corresponding branch,
executing the associated actions or processes. If no match is found among the specified cases,
the flow may proceed to a default case, if one exists. After the execution of a matched case's
actions, the flow typically breaks out of the structure, bypassing all other cases. This differs
from nested if-else statements as it allows for more efficient and readable branching when
dealing with multiple possible values of a single variable.
EXAMPLE 2.11: Develop the pseudocode for print the direction name based on the value of a
character called dir.
INPUT dir
SWITCH (dir)
CASE “N”:
PRINT(“North”)
BREAK
CASE “S”:
PRINT(“South”)
BREAK
CASE “E”:
PRINT(“East”)
BREAK
CASE “W”:
PRINT(“West”)
BREAK
DEFAULT :
PRINT(“Invalid direction code”)
ENDSWITCH
2.3.4 Repetition or loop (for, while, repeat-until)
When a certain block of instructions is to be repeatedly executed, we use the repetition
or loop construct. This is essential for tasks that require iteration, such as processing lists of
data, performing calculations until a condition is met. Each execution of the block is called an
iteration or a pass. If the number of iterations (how many times the block is to be executed)
is known as definite iteration. Otherwise, it is called indefinite or conditional iteration. The
block that is repeatedly executed is called the loop body. They reduce code redundancy and
allow for efficient processing of data. Three types of loop FOR, WHILE and REPEAT-UNTIL.
A) FOR loop
The for loop implements definite iteration. There are three variants of the for loop. All for loop
constructs use a variable (call it the loop variable) as a counter that starts counting from a
specific value called begin and updates the loop variable after each iteration. The loop body
repeats execution until the loop variable value reaches end.
SYNTAX:
FOR variable = start_value TO end_value [step_value]
statement(s)
ENDFOR
Example: Pseudocode for finding the sum of first 10 numbers using FOR loop.
SET sum = 0
FOR i = 1 TO 10
sum = sum + i
ENDFOR
PRINT sum
B) Condition-Controlled Loops: This loop is executed based on a condition and needed when
the number of iterations is not known in advance.
❖ WHILE loops: A while loop is generally used to implement indefinite iteration. Test
the condition before executing the loop body. The loop body is executed repeatedly
as long as condition evaluates to True. When the condition is evaluated as False, the
loop body is exited. We use brackets or indentation to improve the readability of the
algorithms.
SYNTAX:
WHILE condition
statement(s)
ENDWHILE
Example: Pseudocode for finding the sum of first 10 numbers using WHILE loop.
SET sum=0
SET counter1
WHILE counter <= 10
SET sum = sum + counter
INCREMENT counter
ENDWHILE
PRINT "The sum of the first 10 numbers is:", sum
❖ REPEAT-UNTIL: A repeat-until loop is used for indefinite iteration and it is executing a
block of code repeatedly until a certain condition becomes true. It's similar to a while
loop, but the condition is checked at the end of the loop instead of the beginning.
SYNTAX:
REPEAT
statement(s)
UNTIL condition
Example: Pseudocode for finding sum of first 10 numbers using REPEAT- UNTIL loop.
SET sum = 0
SET count=1
REPEAT
SET sum = sum + count
INCREMENT count
UNTIL count > 10
PRINT "The sum of the first 10 numbers is:", sum
2. WHILE loop: For implementation with WHILE, we initialise the loop variable value to 1 (as
the initial value) outside the loop. Inside the loop, value of i is printed and i value is
incremented by 1. Condition of loop is set as while (i<=10).
SET count = 0
WHILE count < 11
PRINT count
INCREMENT count
ENDWHILE
In this WHILE loop, we initialize i to 1 before the loop starts. The loop continues as long as
value of i is less than or equal to 10. We need to remember to increment i inside the loop to
avoid an infinite loop.
3. REPEAT - UNTIL loop: With the REPEAT - UNTIL loop, we also initialize i to 1 before the loop.
The loop body executes, printing and incrementing i, and then checks if i is greater than 10. If
it is, the loop terminates.
SET i = 1
REPEAT
PRINT i
INCREMENT i
UNTIL i > 10
From this example, it is very clear that, WHILE and REPEAT UNTIL loops check the
condition and continue the iterations, instead of the count value, which we check in FOR loop.
The FOR loop is most suitable for this task because we know exactly how many times,
we want the loop to run. The WHILE and REPEAT-UNTIL loops require manual initialization and
incrementation of the loop variable. The REPEAT-UNTIL loop will always execute at least once,
which is fine in this case but might not always be desirable. The condition is checked on
• WHILE loop checks the condition at the beginning of each iteration, while REPEAT -
UNTIL loop checks the condition at the end of each iteration. Therefore, WHILE is
called entry-controlled loop and REPEAT UNTIL loop is called exit-controlled loop.
• WHILE loops may execute zero times if the initial condition is false, while REPEAT-
UNTIL loops always execute at least once.
In the while loop, the condition is tested at the beginning, in the repeat-until loop,
the condition is tested at the end. For this reason, the WHILE loop is known as an
entry-controlled loop and the REPEAT-UNTIL loop is known as an exit-controlled loop.