programming language

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

Pseudo Code

Pseudo code

 It is easier to understand for the programmer or


non-programmer to understand the general working of the
program, because it is not based on any programming language.
Pseudo code

 It gives us the sketch of the program before actual coding.


 It is not a machine readable.
 Pseudo code can’t be compiled and executed.
 There is no standard syntax for pseudo code.
Guidelines for writing Pseudo code

1. Pseudocode is written using structured English.


2. The programmers may use their own style for writing a
Pseudo code, which can be easily understood.
3. It never use the syntax of any programming languages.
Guidelines for writing Pseudo code

4. It uses some keywords to denote programming process.


 Input : INPUT, GET, READ and PROMPT.
 Output : OUTPUT, PRINT, DISPLAY and SHOW.
 Processing : COMPUTE, CALCULATE, DETERMINE, ADD,
SUBTRACT, MULTIPLY and DIVIDE.
 Initialize : SET and INITIALISE
5. Keyword should be in capital letter.
Common keywords used in Pseudo code

The following gives common keywords used in pseudocode.


 // : This keyword used to represent a comment.
 BEGIN, END : Begin is the first statement and End is the last
statement.
 INPUT, GET, READ : The keyword is used to input the data.
Common keywords used in Pseudo code

 COMPUTE, CALCULATE : used for calculation of the result of


the given expression.
 ADD, SUBTRACT, INITIALIZE : used for addition, subtraction
and initialization.
 OUTPUT, PRINT, DISPLAY : It is used to display the output of
the program.
Common keywords used in Pseudo code

 IF, ELSE, ENDIF : used to make decision.


 WHILE, ENDWHILE : used for iterative statements.
 FOR, ENDFOR : Another iterative incremented/decremented
tested automatically.
Guidelines for writing Pseudo code

6. There are 3 Control Structures in Pseudo code.


 Sequential logic
 Selection logic
 Iteration logic
Sequence Logic

 It is used to perform instructions in a sequence, that is one


after another.
 For sequence logic, pseudo code instructions are written in an
order in which they are to be performed.
 The logic flow of pseudo code is from top to bottom.
Example : Find the Product of two numbers

BEGIN
READ the values of A and B
COMPUTE C by multiplying A with B
PRINT the result C
END
Selection Logic

 It is used for making decisions and for selecting the proper path
out of two or more alternative paths in program logic.
 It is also known as decision logic.
 Selection logic is depicted as either an IF..THEN or an IF…
THEN..ELSE Structure.
Example : Greatest of two numbers

BEGIN
READ the values of A and B
IF A is greater than B THEN
DISPLAY “A is greater”
ELSE
DISPLAY “B is greater”
END IF
END
Iteration Logic

 It is used to produce loops when one or more instructions may


be executed several times depending on some conditions.
 It uses structures called FOR, WHILE and REPEAT__UNTIL.
Example : Print n natural numbers

BEGIN
GET the value of N
INITIALIZE I to 1
FOR I less than or equal to N DO
PRINT the value of I
INCREMENT I by 1
ENDFOR
END
Advantages

1. Pseudo is independent of any language; it can be used by most


programmers.
2. It is easy to translate pseudo code into a programming language.
3. It can be easily modified as compared to flowchart.
4. Converting a pseudo code to programming language is very easy as
compared with converting a flowchart to programming language.
Disadvantages

1. It does not provide visual representation of the program’s logic.


2. There are no accepted standards for writing pseudocode.
3. It cannot be compiled nor executed.
4. For a beginner, It is more difficult to follow the logic or write
pseudocode as compared to algorithm and flowchart.
Examples: Algorithms, Pseudo Code, Flow Chart,
Programming Language
1. Write an algorithm to find Area of a Rectangle

Algorithm Pseudocode Flow Chart

Step 1 : Start BEGIN


Step 2 : Get l, b values READ l, b
Step 3 : Calculate A = l * b CALCULATE A = l * b
Step 4 : Display A DISPLAY A
Step 5 : Stop END
2. Write an algorithm for Calculating Area and Circumference of Circle

Algorithm Pseudocode Flow Chart

Step 1 : Start BEGIN


Step 2 : Get r value READ r
Step 3 : Calculate A=3.14*r*r CALCULATE A and C
Step 4 : Calculate C=2*3.14*r A = 3.14*r*r
Step 5 : Display A, C C = 2*3.14*r
Step 6 : Stop DISPLAY A, C
END
3. Write an algorithm for Calculating Simple Interest

Algorithm Pseudocode Flow Chart

Step 1 : Start BEGIN


Step 2 : Get P, N, R value READ P, n, r
Step 3 : Calculate CALCULATE S
SI = (P*N*R)/100 SI = (P*N*R)/100
Step 4 : Display SI DISPLAY SI
Step 5 : Stop END
4. To check greatest of two numbers

Algorithm Pseudocode Flow Chart

Step 1 : Start BEGIN


Step 2 : Get a, b value READ a, b
Step 3 : check if(a > b) IF (a > b) THEN
print a is greater DISPLAY a is greater
Step 4 : else b is greater ELSE
Step 5 : Stop DISPLAY b is greater
END IF
END
Factorial Number
Formula :
n! = 1*2*3*........*n
4! = 1*2*3*........*4 = 24
Find a Factorial of a Number Using
Recursion
 Algorithm for factorial of n numbers using recursion:

 Main function:
 Step1: Start
 Step2: Get n
 Step3: call factorial(n)
 Step4: print fact
 Step5: Stop

 Sub function factorial(n):


 Step1: if(n==1) then fact=1 return fact
 Step2: else fact=n*factorial(n-1) and return fact
25
26
BUILDING BLOCKS OF ALGORITHMS
Statements
A statement is a command given to the computer that instructs the computer to take a
specific action, such as display to the screen, or collect input. A computer program is
made up of a series of statements.
State
State is information your program manipulates to accomplish some task. It is data or
information that gets changed or manipulated throughout the runtime of a program.
Control Flow Statements
 if statements
 for loops
 while loops

Functions
A function is a block of organized, reusable code that is used to perform a single, related
action. Functions provide better modularity for your application and a high degree of
code reusing.
Other Terms: methods, sub-routines , procedure
27
Functions:

 Function is a sub program which consists of block of


code(set of instructions) that performs a particular task.

 For complex problems, the problem is been divided into


smaller and simpler tasks during algorithm design.

28
Example:

Algorithm for addition of two numbers using function


Main function()
Step 1: Start
Step 2: Call the function add()
Step 3: Stop

sub function add()


Step 1: Function start
Step 2: Get a, b Values
Step 3: add c=a+b
Step 4: Print c
Step 5: Return

29
30
The three building blocks are Sequence, Selection, and Iteration.
An Action is one or more instructions that the computer performs in sequential
order (from first to last).
Example: Compute 10 plus 30.
A Decision is making a choice among several actions.
Example: If value of A is greater than B, then print “A is Greater” otherwise
print “B is Greater”
A Loop is one or more instructions that the computer performs repeatedly.
Example: Print "HAVE A GOOD DAY" 50 times.

Building Block Common name

Sequence Action

Selection Decision

Iteration Repetition or Loop

31
Programming Language
Programming Language

 A Programming Language is a set of symbols and rules for


instructing a computer to perform specific tasks.
 The programmers have to follow all the specified rules before
writing program using programming language.
 The user has to communicate with the computer using
language which it can understand.
Programming Language
Types of programming languages

1. Machine language
2. Assembly language
3. High level language
Types of programming languages
1. Machine Level Language
 Machine language is only understand by the
computers.
 Data is represented in binary format (0s and 1s).
 It is very difficult to understand by the human
beings.
 Modifications and error fixing cannot be done.
 Machine language is very difficult to memorize.
Machine Level Language
 Execution is fast in machine language because all data is already
present in binary format.
 Machine language is hardware dependent.
2. Assembly language

To overcome the issues in programming language and make the


programming process easier, an assembly language is
developed which is logically equivalent to machine language but
it is easier for people to read, write and understand.
Assembly language

 Assembly language is symbolic representation of machine


language.
 Assembly languages are symbolic programming language that
uses symbolic notation to represent machine language
instructions.
 They are called low level language because they are so closely
related to the machines.
Assembler
Assembler is the program which translates assembly language
instruction in to a machine language.
3. High level language

 High Level Language contains English words and symbols.


 The specified rules are to be followed while writing program in
high level language.
 The interpreter or compilers are used for converting these
programs in to machine readable form.
High level language
High Level Language

The programming language usually refers to high-level languages, such as


 BASIC
C
 C++
 COBOL
 Java
 FORTRAN
 Ada and Pascal.
Translating high level language to machine language

The programs that translate high level language in to machine


language are called interpreter or compiler.
Architecture of Programming Conversion
Compiler
 A Compiler is a program which translates the source code
written in a high level language in to object code which is in
machine language program.
 Compiler reads the whole program written in high level language
and translates it to machine language.
 If any error is found it display error message on the screen.
Interpreter
 Interpreter translates the high level language program in line by
line manner.
 The interpreter translates a high level language statement in a
source program to a machine code and executes it immediately
before translating the next statement.
 When an error is found the execution of the program is halted
and error message is displayed on the screen.
Advantages
1. Readability
High level language is closer to natural language so they are
easier to learn and understand.
2. Machine independent
High level language program have the advantage of being
portable between machines.
3. Easy debugging
Easy to find and correct error in high level language.
Disadvantages
Less efficient
 The translation process increases the execution time of the
program.
 Programs in high level language require more memory and take
more execution time to execute.
5. To check greatest of three numbers

Algorithm

Step1 : Start
Step2 : Get A, B, C
Step3 : if (A > B) goto Step4 else goto Step5
Step4 : if (A > C) print A else print C
Step5 : if (B > C) print B else print C
Step6 : Stop
To check greatest of three numbers
Pseudocode

BEGIN ELSE
READ a, b, c IF(b>c) THEN
IF (a>b) THEN DISPLAY b is greater
IF(a>c) THEN ELSE
DISPLAY a is greater DISPLAY c is greater
ELSE END IF
DISPLAY c is greater END IF
END IF END
To check greatest of three numbers
Flowchart

You might also like