0% found this document useful (0 votes)
9 views

Module - II.pptx

python

Uploaded by

vidhyapm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Module - II.pptx

python

Uploaded by

vidhyapm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

UCEST105 - ALGORITHMIC THINKING WITH PYTHON

Compiled By
Ms Divya K S
Assistant Professor
Dept. of AI & DS
Muthoot Institute of Technology & Science
Syllabus(9 Hours)
ALGORITHM AND PSEUDOCODE REPRESENTATION:-
Meaning and Definition of Pseudocode, Reasons for using
pseudocode, The main constructs of pseudocode - Sequencing,
selection (if-else structure, case structure) and repetition (for, while,
repeat-until loops), Sample problems*

FLOWCHARTS** :- Symbols used in creating a Flowchart - start


and end, arithmetic calculations, input/output operation, decision
(selection), module name (call), for loop (Hexagon), flow-lines,
on-page connector, off-page connector.
Course Outcome (CO2)
Design pseudocode to represent
simple problems by articulating
it before solving it.
Algorithms and pseudocodes
•Algorithm
• A step-by-step procedure for solving a problem.
• Uses pure English phrases or sentences.
•Pseudocode
• A high-level representation of an algorithm.
• Uses a mix of natural language and programming-like syntax.
• More structured than algorithms.
Ex: Evaluate d = a + b ∗ c.

Algorithm
Ex: Evaluate d = a + b ∗ c.

Pseudocode
Why pseudocodes?
•Wondering why pseudocodes are important? Here are a few
motivating reasons:
• Ease of understanding: Since the pseudocode is programming language
independent, novice developers can also understand it very easily.
• Focus on logic: A pseudocode allows you to focus on the algorithm’s
logic without bothering about the syntax of a specific programming
language.
• More legible: Combining programming constructs with English phrases
makes pseudocode more legible and conveys the logic precisely.
• Consistent: As the constructs used in pseudocode are standardized, it is
useful in sharing ideas among developers from various domains.
• Easy translation to a program: Using programming constructs makes
mapping the pseudocode to a program straightforward
• Identification of flaws: A pseudocode helps identify flaws in the solution
logic before implementation.
Difference between Algorithm and Pseudocode
Feature Algorithm Pseudocode
Definition A set of steps of a problem-solving A high-level representation of an
procedure or method. algorithm using a mixture of technical
format and human-readable text.
Formality Generally, it’s a more abstract and Less formal than code but more
informal concept, not tied to a particular structured than plain language. It uses
programming language or syntax. common language elements and
keywords.
Language Language-neutral; can be expressed in Language-neutral; it aims to be easily
Neutrality plain language or pseudocode. understood by humans as well as easily
converted into code in any
programming language.
Execution Not directly executable; it’s a plan for Not executable on its own; it provides
solving a problem. a high-level overview of the solution’s
logic. We can use it for a dry run.
Syntax No specific syntax rules; described in Uses structured language constructs
plain language or a diagram. like loops, conditionals, and functions
but without strict syntax rules.
Usage Used for planning, design, and Used for communicating and
communication of problem-solving documenting an algorithm before
approaches. writing code.
Constructs of a pseudocode
•A good pseudocode should follow the structured programming
approach.
•Structured coding aims to improve the readability of pseudocode
by ensuring that the execution sequence follows the order in which
the code is written.
•The main constructs are
1. Sequencing
2. Selection
3. Repetition (loop)
Constructs of a pseudocode - Sequence
•This is the most elementary construct where the instructions of the
algorithm are executed in the order listed.
•It is the logical equivalent of a straight line.
•Consider the code below.

S1 The statement S1 is executed first, which is then


S2 followed by statement S2, so on and so forth,
S3 Sn until all the instructions are executed.
.
.
No instruction is skipped and every instruction
.
Sn is executed only once
Constructs of a pseudocode – Sequence (Ex.)
Example: Write a pseudocode to find the area of a square

1. Start
2. Read the side length of the square
3. area: area = side_length * side_length
4. Print the area
5. End

All the statements in the given pseudocode will execute in


sequential
order, without skipping any line.
Constructs of a pseudocode - Decision or Selection
•A selection structure consists of a test condition together with one
or more blocks of statements.
•The result of the test determines which of these blocks is executed.
•There are mainly two types of selection structures
1. if structure
1. Simple if structure
2. If…else structure
3. if ..else if.. else structure
2. Case Structure
Simple if
•The general form of this structure is
If the test condition is evaluated to
True, the statements denoted by
TRUE_INSTRUCTIONS are
executed. Otherwise, those
statements are skipped.
•Ex:

The pseudocode CheckPositive(x)


checks if an input value x is
positive.
if else structure
•The general form of this structure is
If the test condition is met,
the first block (denoted by
TRUE_INSTRUCTIONS) is executed
and the algorithm skips over the
second block (denoted by
FALSE_INSTRUCTIONS).

If the test condition is not met, the first


block is skipped and only the second
block is executed.
if else structure - Example
The pseudocode PERSONTYPE(age) checks if a person is a major
or not.
if else if else structure
•When a selection is to be made out of a set of more than two
possibilities, you need to use the if else if else structure, whose
general form is given below:
Here, if condition1 is met,
TRUE_INSTRUCTIONS1 will
be executed. Else condition2 is
checked. If it evaluates to
True,
TRUE_INSTRUCTIONS2 will
be selected. Otherwise
FALSE_INSTRUCTIONS will
be executed.
if else if else structure – Example
The pseudocode COMPAREVARS(x, y) compares two variables x
and y and prints the relation between them.

NOTE:
Once a condition is evaluated to be TRUE, the corresponding block is
executed, and the rest of the structure is skipped. If none of the
conditions are met, the final else part is executed.
Programming the same in Python
•a = 33
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
Using elif and else
•a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
Case Structure
•The case structure is a refined alternative to if else if else structure.
•The general form of this structure is:

• First, the value of an expression (which could


also be a single variable) is compared to a
predefined value1
• If a match is found, the first block of code,
referred to as block1, is executed.
• Typically, each block ends with a break
statement, which exits the case structure.
• If there is no match with value1, the
expression or variable is then compared with
value2.
Case Structure

• If this comparison results in a match, block2 is


executed, and the case structure is exited with
the corresponding break statement.
• This comparison process continues for each
case until either a match is found, or all cases
are exhausted.
• If no matches are found for any of the cases,
the default block will be executed.
• This block handles cases where the expression
doesn't match any predefined values.

If a break statement is omitted from a block, the program will continue executing
the subsequent blocks, regardless of whether the subsequent cases match, until
a break is encountered or the end of the case structure is reached.
Case Structure – Example

The pseudocode
PRINTDIRECTION(dir)
prints the direction
name based on the
value of a character
called dir.
Constructs of a pseudocode - Repetition or loop
•When a certain block of instructions is to be repeatedly executed,
we use the repetition or loop construct.
•Each execution of the block is called an iteration or a pass.
•Definite iteration: If the number of iterations (how many times the
block is to be executed) is known in advance.
•Indefinite or conditional iteration: If the number of iterations is
not known in advance
•The block that is repeatedly executed is called the loop body.
•There are three types of loop constructs as discussed below:
1. while loop
2. repeat-until loop
3. for loop
Constructs of a pseudocode – while loop
•A while loop is generally used to implement indefinite iteration.
The general form of the while loop is as follows:

• Here, the loop body (TRUE_INSTRUCTIONS) is executed


repeatedly as long as condition evaluates to True.
• When the condition is evaluated as False, the loop body is
bypassed
Constructs of a pseudocode – repeat-until loop
•The second type of loop structure is the repeat-until structure.
•This type of loop is also used for indefinite iteration.
•Here the set of instructions constituting the loop body is repeated
as long as condition evaluates to False.
•When the condition evaluates to True, the loop is exited
Constructs of a pseudocode – repeat-until loop
•There are two major differences between while and repeat-until
loop constructs:
1. In the while loop, the pseudocode continues to execute as long as the
resultant of the condition is True; in the repeat-until loop, the looping
process stops when the resultant of the condition becomes True
2. 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.

You might also like