Chapter 8 - Part1
Chapter 8 - Part1
Example : Total 0
Count 0
Min 1000
Selection :
• a condition is checked and this determines which , if any , code is run .
• Conditions need logical operators
• there are two forms : IF Statement , and CASE Statement
IF the condition true or false .
CASE Allow to take one variable and different actions .
Chapter 8 : Programming concepts
Iterations : or loop is the construct where statements are run either a finite number of
times, until a condition is true or while a condition is true.
Three types :
1. Count-Controlled loop : the most common type is FOR Loop .
2. Pre-Condition Loop(condition-controlled ) : the most common type is WHILE Loop,
tests the condition before running the code , if the condition is false the code inside
the loop will not run, it loops while the condition is true.
3. Post-condition Loop(condition-controlled) : the most common type is REPEAT Loop,
the condition will be checked after running the code ate the end of the loop , it should
be true to exit the loop .
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 : Totaling , counting
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 : Subroutine
Procedures, functions and parameters
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
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”