Pseudo Code Slides

You might also like

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

Pseudo Code

What is Pseudo Code?


Pseudocode is a newer tool and has features that make it
more reflective of structured concepts.
It helps to explain the fundamentals of computer
programming.
Plain English programming code

2
Objectives
 Variable
 Output/Inputs
 Sequence
 Selection
 Iteration
 Flowcharts

3
Why we use Pseudo Code
• Syntactical Representation of a program
• It helps no programmers understand programming logic
• Debug and solve problems without actually writing a code
Variables
Variables are assigned using the = operator.

x=3
name=”Bob”

A variable is declared the first time a value is assigned. It


assumes the data type of the value it is given.
Type Cast
Casting Variables can be typecast using the int str and float
functions
▸ str(3) returns “3”
▸ int(“3”) returns 3
▸ float(“3.14”) returns 3.14
Output
Outputting to Screen print(string)
• Example: print(“hello”)

• Taking Input from User


• variable=input(prompt to user)
• Example: name=input(“Please enter your name”)
What are operators?
In Python, operators are special symbols that perform arithmetic or logical computation. The
operand is the value that the operator operates on.
For example: >>> 9+4 Output: 13
Here, + is the operator that performs addition. 9 and 4 are the operands and 13 is the
output of the operation.
Following are the types of Operators In Python
• Arithmetic Operators
• Comparison Operators
• Assignment Operators
• Logical Operators
• Identical Operators
• Membership Operator
Arithmetic operators
Arithmetic operators are used to performing mathematical operations like addition, subtraction,
multiplication, etc.

Operator Definition Example

+ Addition a+b

- Subtraction a-b

* Multiplication a*b

** Exponent a**b

/ Division (answer into float) a/b

// Division (integer answer) a//b

% Modulus (division remainder) a%b


Comparison operators
Values are compared using comparison operators. Depending on the condition, it returns True or False.

Operator Definition Example

> Greater than a>b

< Less than a<b

>= Greater than or equal to a>=b

<= Less than or equal to a<=b

== Equal to a==b

!= Not Equal a!=b


Assignment operators

• In Python, assignment operators are used to assigning values to variables.


• x = 15 is a simple assignment operator that assigns the value 15 on the right
to the variable ’x’ on the left.
• In Python, there are several compound operators, such as x+= 15, which adds
to the variable and then assigns it. It is the same as x = x + 15.
Assignment operators
Operator Meaning Example

Logical operators and If both operands are


true, then true.
x and y

The logical operators are the and, or, and not operators. or True if either operand x or y
is true.

not Give the inverse of not y


operand. Make it false
if it is true, and vice
versa.
Bitwise operators
Each bit of the first operand is compared to the corresponding bit of the second operand by
the bitwise AND operator (&). If both bits are 1, the result bit is also set to 1. Otherwise, the
associated result bit is set to 0.
Operator Name Description

&  AND If both bits are 1, this function sets each bit to 1.
| OR If one of two bits is 1, each bit is set to 1.
 ^ XOR If only one of two bits is 1, this function sets each bit to 1.
~  NOT All the bits are inverted.
<< Bitwise left shift Shift left by pushing zeros in from the right and letting the
bits on the left fall off.
>> bitwise right shift Shift right by copying the leftmost bit from the left and
letting the rightmost bits fall off.
How to write pseudo code
1.Arrange the sequence of tasks and write the pseudocode accordingly.
2.Start with the statement of a pseudo code which establishes the main
goal or the aim.
Example:
This program will allow the user to check
the number whether it's even or odd.

15
How to write pseudo code
1.The way the if-else, for, while loops are indented in a program, indent the statements likewise, as it
helps to comprehend the decision control and execution mechanism. They also improve readability
to a great extent.
Example:

if "1"
print response
"I am case 1"

if "2"
print response
"I am case 2"

16
How to write pseudo code
• Use appropriate naming conventions. The human tendency follows
the approach to follow what we see. If a programmer goes through a
pseudo code, his approach will be the same as per it, so the naming
must be simple and distinct.
• Use appropriate sentence casings, such as CamelCase for methods,
upper case for constants a,nd lower case for variables.
• Elaborate on everything which is going to happen in the actual code.
Don’t make the pseudo code abstract.

17
How to write pseudo code
• Use standard programming structures such as ‘if-then’, ‘for’, ‘while’,
and ‘cases’ the way we use it in programming.
• Check whether all the sections of a pseudo code is complete, finite,
and clear to understand and comprehend.
• Don’t write the pseudo-code in a complete programmatic manner. It is
necessary to be simple to understand even for a layman or client,
hence don’t incorporate too many technical terms.

18
Examples
If a student's grade is greater than or equal to 60
Print "passed"
else
Print "failed"

19
Examples
Set the total to zero
Set grade counter to one
While the grade counter is less than or equal to ten
Input the next grade
Add the grade to the total
Set the class average to the total divided by ten
Print the class average.

20
Examples

21
Flow charts

22

You might also like