0% found this document useful (0 votes)
25 views14 pages

Chapter 8 - Part1

The document summarizes key programming concepts including data structures, sequence, selection, iteration, and operators. It discusses variables, constants, arrays, and converting between data types. It also covers sequence, selection using if/case statements, and different types of iterations like for, while, and repeat loops. The document discusses nested statements, totaling, counting, subroutines like procedures and functions, scope of variables, and library routines for common tasks.

Uploaded by

p8q4jkz4p5
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)
25 views14 pages

Chapter 8 - Part1

The document summarizes key programming concepts including data structures, sequence, selection, iteration, and operators. It discusses variables, constants, arrays, and converting between data types. It also covers sequence, selection using if/case statements, and different types of iterations like for, while, and repeat loops. The document discusses nested statements, totaling, counting, subroutines like procedures and functions, scope of variables, and library routines for common tasks.

Uploaded by

p8q4jkz4p5
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/ 14

Chapter 8 : Programming

8.1 Programming concepts


There are five basic constructs to use and understand when developing a program:
Data structure – variables, constants and arrays
sequence – order of steps in a task
selection – choosing a path through a program
iteration – repetition of a sequence of steps in a program
operator use – arithmetic for calculations, logical and Boolean for decisions.
Chapter 8 : Programming concepts –Data Structures
Data structures :
• variable in a computer program is a named data store than contains a value that may change during the
execution of a program. Putting data into variable is done using an Assignment Statement
• constant in a computer program is a named data store than contains a value that does not change
during the execution of a program.
• 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.

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
Chapter 8 : Programming concepts
Sequence : is a series pf statements that are executed once in the order they are written .

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

Totaling : is adding together set of values you should :


• Initialize the total to 0
• Add the values together : total total + value
Counting : is how many of something there are
• Initialize the a counter to 0
• Increase the counter by 1 each time an item is entered /found : Counter Counter+1
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 : 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 function 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. In contrast
to a procedure, a function will return a value back to the main 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 C1 TO 6 ************
OUTPUT C 6
CALL Stars ************
NEXT C

Procedure with parameter : *


PROCEDURE Stars (Number : INTEGER) *
FOR Counter ← 1 TO NUMBER *
OUTPUT "*" *
NEXT Counter *
ENDPROCEDURE *
CALL Stars(7) *
Chapter 8 : Subroutine functions
For example, here is a function written in pseudocode to convert a temperature from Fahrenheit to Celsius:

FUNCTION Celsius (Temperature : REAL) RETURNS REAL If MyTemp=20


RETURN (Temperature – 32) / 1.8 the result will be : -6.7
ENDFUNCTION
MyTemp ← Celsius(20) call the function using the variable MyTemp

FUNCTION ConvertToCm(Inches: REAL) RETURNS REAL


RETURN Inches * 2.4 call the function using the variable Inches
ENDFUNCTION
Chapter 8 : Subroutine – scope
Scope of a variable : is the area within a program that it can be accessed
Local and global variables
A global variable can be used by any part of a program – its scope covers the whole program.
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. when that part finishes the variable will not be declared and cant be used anymore
For example, in this algorithm the variables Number1, Number2 and Answer are declared both locally and globally, whereas
Number3 is only declared locally.
Chapter 8 : Library routines
• routines that are ready to be used into a program.
• These routines are fully tested and ready for use.
• includes functions and procedures.

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”

Example : output each letter of a string each character at a time


INPUT “ Enter a message” , StringInput
FOR Count 1 to LENGTH(StringInput)
Character  SUBSTRING(StringInput, Count,1)
OUTPUT Character
NEXT Count

You might also like