0% found this document useful (0 votes)
5 views59 pages

Unit 8P New

Uploaded by

ehtsalman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views59 pages

Unit 8P New

Uploaded by

ehtsalman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 59

Unit-8 Programming

Programming
Concepts in
Pseudocode
DECLARE Statement
• It is the way of introducing a variable
to the program/pseudocode.
DECLARE A: INTEGER
DECLARE B: REAL
COMMENTS in pseudocode
• It is used to label and documenting the
lines in pseudocode.
• It is represented by ‘//’ symbol.

// Pseudocode to add two numbers


DECLARE A: INTEGER
DECLARE B: INTEGER
DECLARE C: INTEGER
C A+B
VARIABLE
• It is a name given to a container
carrying a value(String, integer, Real,
Boolean, Char etc.) to which helps in
developing a program.
• The value of the variable can be
changed during the execution of the
program.
A 10 # 10 is assigned to variable
A
CONSTANT in Pseudocode
• It is a container which stores the
value permanently without being
changed during the exceution of
program.
Example:
• CONSTANT PI : 3.14
OUTPUT statement
• It is used to print the result from the
pseudocode in the form of integer
value, real value, string value etc.
onto the screen..
Example:
OUTPUT “Good Morning”
INPUT statement
• It is used to accept data from the
user into the pseudocode through the
keyboard.
Example:
OUTPUT “Enter the age of the person”
INPUT Age
BASIC DATA TYPES
1.Integer: It represents a whole
number value e.g: 10,20 etc.
2.Real: It represents a decimal
(floating) value e.g: 5.2, 9.6 etc
3.Char: It represents a single alphabet
value e.g: ‘a’, ‘b’, etc.
4.String: It stores the collection of
alphabets e.g: ‘ royal’ etc.
5.Boolean: It stores the value either
TRUE or FALSE.
Why do we need data types?
• In order for a computer system to
process and store data
effectively
• data to be stored in an
appropriate way,
• data to be manipulated
effectively,
• automatic validation in some
cases.
Computer Programming
Languages
• Machine language: Based on binary
number(0,1).
• Assembly language: Based on
symbols & hardware related english
words.
• High level language: It is based on
english words to develop a program. It
is easy to use and learn.
• e.g: PYTHON, JAVA, VISUAL BASIC etc.
# Pseudocode to find volume
of a cylinder
DECLARE Radius: INTEGER
DECLARE Height: REAL
DECLARE Vol: REAL
CONSTANT PI: 3.142
INPUT “Enter the radius”, Radius
INPUT “Enter the height”, Height
Vol (PI * Radius * Radius * Height)
OUTPUT” The volume of cylinder is ”, Vol
# Python program to find
volume of cylinder
CONSTANT PI = 3.142
Radius = float(input("Please enter the
radius of the cylinder "))
Length = float(input("Please enter the
length of the cylinder "))
Volume = Radius * Radius * Length * PI
print("Volume of the cylinder is ",
Volume)
Flow of Control
1. Sequence
2. Selection / Conditional
statements
3. Iteration / Loop /Repetitive
construct
Sequence
• An algorithm contains instructions that are
carried out one after another, and the
sequence determines the order that these
instructions are carried out. For example, a
basic algorithm for making a cup of tea
might look like this:
• 1. Boil the kettle.
• 2. Put a teabag in a mug.
• 3. Pour water into the mug.
• 4. Add sugar and milk.
• 5. Stir.
• 6. Drink the tea.
• #Pseudocode to find Volume of a
cuboid
• DECLARE L : INTEGER
• DECLARE B : REAL
• DECLARE H : INTEGER
• DECLARE Volume: REAL
• Volume (L * B * H)
• OUTPUT ”The volume of cuboid is :”,
Volume
Selection/ Conditional
statement
• Selection is a very useful technique,
allowing different routes through the
steps of a program

• Selection was demonstrated in


pseudocode with the use of IF and
CASE statements
IF…THEN…ELSE…ENDIF
It is the conditional statement to select
between two flow of control.

Example:

IF Age > 17
THEN
OUTPUT "You are an adult“
ELSE
OUTPUT "You are a Child“
ENDIF
CASE…OF…OTHERWISE…
ENDCASE
• Case statements are used when there are multiple
choices to be made or select from.
• Example:
• CASE OF OpValue
• "+" : Answer¨Number1 + Number2
• "-" : Answer¨Number1 - Number2
• "*" : Answer¨Number1 * Number2
• "/" : Answer¨Number1 / Number2
• OTHERWISE OUTPUT "Please enter a
valid choice“
• ENDCASE
Iteration/Loops
• When some actions performed as part of
an algorithm need repeating this is called
iteration. Loop structures are used to
perform the iteration.
• Types of loops
1. Count-controlled loops (for a set number
of repetitions)
2. Pre-condition loops – condition given at
the beginning & may have no iterations
3. Post-condition loops – condition given at
the end & always has at least one
iteration
TYPES OF LOOPS
• FOR…TO…NEXT
• WHILE … DO …ENDWHILE
• REPEAT … UNTIL
String Handling
• Strings are used to store text.
• Every string contains a number of
characters, from an empty string,
which has no characters stored, to a
maximum number specified by the
programming language.
• The characters in a string can be
labelled by position number starting
with either 0 or 1.
String Library Methods
• LENGTH(): It is used to count the characters
in a string
Example: LENGTH(“Python”)
Output will be 6

• SUBSTRING(): It is used to extract the part


of a string.
Example: SUBSTRING(“Computer
Science”,10,7)
Output will be Science
• UCASE(): It converts a string in uppercase.
Example:
UCASE(Computer)
Output will be COMPUTER

• LCASE(): It converts a string in lowercase.


Example:
LCASE(Computer)
Output will be computer
• ISUPPERCASE(): It checks whether
a string is in uppercase or not.
Example:
ISUPPERCASE(Computer)
Output will be False

• ISLOWERCASE(): It checks
whether a string is in lowercase or
not.
Example:
ISLOWERCASE(computer)
Output will be True
Arithmetic Operators
Logic Operators

All programming languages make use of


logical operators to decide which path to take
through a program.
Boolean operators
• All programming languages make use
of Boolean operators to decide
whether an expression is true or
false.
• # Pseudocode to find average marks
of FIVE subjects of a student.
• DECLARE Marks : INTEGER
• DECLARE Total: INTEGER
• DECLARE Average : REAL
• FOR I 1 TO 5
• INPUT “Enter the marks”, Marks
• Total Total+ Marks
• NEXT I
• Average Total/5
• OUTPUT” The Average marks is”,
Average
• # Pseudocode to find average marks of five
subjects of ten students.
• DECLARE Marks : INTEGER
• DECLARE Total: INTEGER
• DECLARE Average : REAL
• FOR I 1 TO 10 # no. of students
• FOR J 1 TO 5 # no. of subjects
• INPUT “Enter the marks”, Marks
• Total Total+ Marks
• NEXT J
• Average Total/5
• OUTPUT ”The Average marks of student”, I,
Average
• Average 0
• Total 0
NEXT I
Use of nested statements
• Selection and iteration statements
can be nested one inside the other.
This powerful method reduces the
amount of code that needs to be
written and makes it simpler for a
programmer to test their programs.

• One type of construct can be nested


within another – for example,
selection can be nested within a
SUBROUTINES(Procedures
and functions)
• When writing an algorithm, there are
often similar tasks to perform that make
use of the same groups of statements.
• Instead of repeating these statements
and writing new code every time they
are required, many programming
languages make use of subroutines,
also known as named procedures or
functions.
PROCEDURE
• A procedure is a set of programming
statements grouped together under a
single name that can be called to
perform a task at any point in a
program.
• It would not return any value to the
main program.
FUNCTION
• A function is a set of programming
statements grouped together under a
single name that can be called to
perform a task at any point in a
program.
• In contrast to a procedure, a function
will return a value back to the main
program.
PARAMETERS
• Parameters are the variables that
store the values of the arguments
passed to a procedure or function.
Some but not all procedures and
functions will have parameters.
Syntax of PROCEDURE(without
parameters)

Procedure will be called by CALL


statement
Syntax of PROCEDURE(with
parameters)
FUNCTIONS
• A function is just like a procedure except
it always returns a value. Just like a
procedure it is defined once and can be
called many times within a program.
• Unlike procedures, function calls are not
standalone and instead can be made on
the right-hand side of an expression.
• The keyword RETURN is used as one of
the statements in a function to specify
the value to be returned. This is usually
the last statement in the function
definition.
Syntax of FUNCTION(with
parameter)
• Because a function returns a value, it
can be called by assigning the return
value directly into a variable as
follows:

NOTE: Procedure calls are single


standalone statements. Function calls
are made as part of an expression, on
the right-hand side.
LOCAL VARIABLE
• A local variable can only be used by
the part of the program it has been
declared in – its scope is restricted to
that part of the program.
• It is defined in a FUNCTION or
PROCEDURE.
GLOBAL VARIABLE
• A global variable can be used by
any part of a program – its scope
covers the whole program.
Library routines
• The Funtions that are ready to be
incorporated into a program. These
routines are fully tested and ready for use.
• A programming language IDE usually
includes a standard library of functions
and procedures.
• These standard library routines perform
many types of task – such as the string
handling functions like SUBSTRING(),
LENGTH() etc.
• MOD – returns remainder of a
division
• DIV – returns the quotient (i.e. the
whole number part) of a division
• ROUND – returns a value rounded to
a given number of decimal places
• RANDOM – returns a random
number.
Here are some examples of these library
routines
• in pseudocode:
• Value1 MOD(10,3) returns the
remainder of10 divided by3
• Value2 DIV(10,3) returns the
quotient of10 divided by3
• Value3 ROUND(6.97354, 2) returns
the value rounded to2 decimal places
• Value4 RANDOM() returns a random
number between 0 and1 inclusive
• Value5 DIVMOD() returns both
answers,the first answer is DIV and the
second answer is MOD
Creating a maintainable program
A maintainable program should:
1. Always use meaningful identifier names
for variables, constants, arrays,
procedures, functions.
2. be divided into modules for each task
using:
– procedures
– functions
3. be fully commented using your
programming language’s commenting
feature.

Array
• An array is a data structure
containing several elements of the
same data type.
• These elements can be accessed
using the same identifier(array)
name.
• The position of each element in an
array is identified using the array’s
index(location)
One dimensional array
• A one-dimensional
array can be
referred to as a list.

Here is an example
of a list with 10
elements in it where
the first element has
an index of zero.
• When a one-dimensional array is
declared in
pseudocode( Declaration)
• the name of the array
• the first index value
• the last index value
• and the data type
How to declare a new array called MyList ?

Adding the number 27 to the fourth position in


the array MyList as follows:
How to input an ONE-DIMENSIONAL
array from the user?
How to input an TWO-
DIMENSIONAL array from the user?
File Handling
• Data stored in a file can thus be
accessed by the same program at a
later date or accessed by another
program.
• Computer programs store data that
will be required again in a file
Why do we need a file?
• While any data stored in RAM will be
lost when the computer is switched
off, when data is saved to a file it is
stored permanently.
WRITING INTO A FILE
udocode to write a text input by the user into
named “Mytext.txt”
READING FROM A FILE
Pseudocode to read the text from the file
named “Mytext.txt” and print it onto the
screen.

You might also like