chapter_8_-_part1
chapter_8_-_part1
Structures
Variables
• Variable in a computer program , Constants
is a named data store than contains a value,that
and Arrays
may change during
the execution of a program. Putting data into variable is done using an Assignment Statement
Declaring Variable :
Syntax : DECLARE variable name : Data type
Example : DECLARE X : STRING
DECLARE Y : INTEGER
DECLARE Z : INTEGER
• Constant in a computer program is a named data store than contains a value that does not change
during the execution of a program.
Declaring the constants :
Syntax : CONSTANT constant name = value
Example : CONSTANT Pi= 3.14
• An array is a data structure containing several elements(List ) of the same data type; these elements
can be accessed using the same identifier name. The position of each element in an array is identified
using the array’s index.
Chapter 8 : nested statements
Nested statements : is one or more selection and /or iteration statements inside another
selection/iteration statement .
Could be :
• IF statement inside IF statement
• Or Loop inside IF statement
• Or IF inside Loop statement
• Or Loop inside Loop statement.
Chapter 8 : Subroutine
A Subroutine is a piece of code which is written within given program which has an
identifier (name). The Subroutine can be called from the same main program but can not
be called from other programs. In other words subroutines are local to program. Two forms
Procedures and functions .
Why ? When writing an algorithm, there are often similar repeated statements Instead of
repeating these statements and writing new code every time they are required, many
programming languages make use of subroutines.
It is helpful when dividing the system into subsystems
Chapter 8 :
Procedures, functions and parameters
Subroutine
A procedure is a set of programming statements grouped together under a single
name (identifier) that can be called to perform a task at any point in a program.
A parameter : is a value that is sent from the main program to the subroutine ( procedure or
function ) . Parameters are declared inside the brackets after the subroutine name .
When procedures and functions are defined, the first statement in the definition is a header,
which contains:
» the name of the procedure or function (identifier )
» any parameters passed to the procedure or function, and their data type
» the data type of the return value for a function.
Chapter 8 : Subroutine – procedure
Functions
Procedures :
• it is defined once and can be called
• it is defined once and can be called
many times within a program.
many times within a program.
• it can be defined with or without
• it can be defined with or without
parameters
parameters.
• A function always returns a value.
• a procedure doesn’t return a value.
• Function calls are made as part
• Procedure calls are single standalone
of an expression, on the right-hand
statements.
side.
Chapter 8 : Subroutine – procedure
Procedures : procedure it is defined once and can be called many times within a program. it can be defined with or
without parameters. Procedure calls are single standalone statements.
Example : 1
************
Procedure without parameter : 2
PROCEDURE Stars ************
3
OUTPUT"************" ************
ENDPROCEDURE 4
************
5
FOR C1 TO 6 ************
OUTPUT C 6
CALL Stars ************
NEXT C
For example, here is a function written in pseudocode to convert a temperature from Fahrenheit
to Celsius:
Examples :
» MOD – returns remainder of a division takes 2 parameters Ex. MOD(9,4)----1
» DIV – returns the quotient (i.e. the whole number part) of a division takes 2 parameters Ex. DIV(9,4)----2
» ROUND – returns a value rounded to a given number of decimal places takes one parameter
Ex. ROUND(6.97354, 2) returns the value rounded to 2 decimal places
» RANDOM – returns a random number.
Ex. two parameters : RANDOM(Integer1 : INTEGER, Integer2 : INTEGER) RETURNS INTEGER generates a random integer in
the range from Integer1 to Integer2 inclusive. For example: RANDOM(10, 12) returns either: 10, 11 or 12
» INT – returns the integer part of a number Ex. INT(9.6)-------9
Chapter 8 : Library routines
Library routines : String manipulating
» upper – converting all the letters in a string to uppercase. For example, the string "Computer Science" would become
"COMPUTER SCIENCE".
» lower – converting all the letters in a string to lowercase. For example, the string "Computer Science" would become
"computer science".
» length – finding the number of characters in the string. For example, the length of the string "Computer Science" is 16
characters as spaces are counted as a character.
Ex. Charlen Length(string)
» substring – extracting part of a string. For example, the substring "Science" could be extracted from "Computer Science".
Substring(string ,start , length) Ex. Value Substring(“Computer Science”,10,7)------- value “Science”
Chapter 8 : Library routines
Library routines : String manipulating
Convert between data types :
• Str(number) returns the value as string ------------
name str(3) returns “3” as string
• Int(string consists of numbers) returns the integer value
number int(“3”) returns 3 as number
• Float(string consists of numbers) returns the real value
Number float(“3.14”) returns 3.14 as number