ict 155 intro to programming

86
ICT 155 ICT 155 INTRODUCTION TO PROGRAMMING INTRODUCTION TO PROGRAMMING Instructor: R D Appiah

Upload: ike-mensah

Post on 10-Apr-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 1/86

ICT 155ICT 155

INTRODUCTION TO PROGRAMMINGINTRODUCTION TO PROGRAMMING

Instructor: R D Appiah

Page 2: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 2/86

R D Appiah

Computer Programming:

Programming is the art of writing lines of instructions (sometimes referred to as codes)that tell the computer how to solve a problem or carry out a task.

Computer Programme:

 A computer programme is a set of step-by-stepinstructions that tell a computer how to solve aproblem or carry out a task.

Page 3: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 3/86

R D Appiah

Coding:

Coding refers to entering a list of commandsthat become a computer programme.

Some Characteristics of a Computer 

Programme:

A computer programme is typically stored as afile and transferred into the RAM when needed.

A computer programme can also beembedded in computer hardware ± e.g in ROMchip.

Page 4: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 4/86

R D Appiah

Programming Languages (PLs):

1. A programming language is a set of predefined words that are combined into aprogramme according to predefined rules (or 

syntax).

2.  A programming language is a set of keywords and grammar rules designed for 

creating instructions that a computer canultimately process or carry out.

Page 5: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 5/86

R D Appiah

Examples of PLs:

BasicC 

C++

Pascal

Java

COBOL8088 Assembly

FortranFORTH

LISP

 APL

Visual Basic

C# (pron. C sharp)Smalltalk

Page 6: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 6/86

R D Appiah

Categories of PLs:

Criteria 1. Programming Languages can bedivided into two: Low-Level and High-Level

Languages.

Criteria 2. Programming Languages canalso be categorized by generations: First,

Second, Third, Forth and Fifth Generations.

Criteria 3. Programming Languages arealso categorized by paradigms: Procedural,

Object-Oriented, Declarative, Functional &

Event-Driven Paradigms.

Page 7: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 7/86

Page 8: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 8/86

R D Appiah

Generations of Languages

First Generation of PLs: These were thefirst programming languages available to

programmers, where commands were/ arerepresented as a series of 1s and 0s. Machinelanguages are examples of First Generation

PLs.

Page 9: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 9/86

R D Appiah

Second Generation of PLs: Here,programmers use abbreviated command words

rather than 1s and 0s in machine languages (animprovement over First Generation of PLs. Assembly Languages are examples of SecondGeneration of PLs.

Third Generation of PLs: This marked theentry point into the high-level programming era.They use easy-to-remember command wordsfor instructions. Examples include COBOL andFortran, which were used for business andscientific applications.

Page 10: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 10/86

Page 11: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 11/86

R D Appiah

Programming Paradigm:

This is the way in which a computer language looks at the problem to be

solved. Some programming languages support asingle paradigm while others support

many paradigms (usually referred to asmulti-paradigm languages)

Page 12: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 12/86

Page 13: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 13/86

R D Appiah

Object-Oriented &E

vent-DrivenParadigm: These formulate programmesas a series of objects and methods that

interact to perform a specific task.

Examples include: Smalltalk, C++, Java,

Visual Basic, C#.

Page 14: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 14/86

Page 15: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 15/86

R D Appiah

Functional Paradigm: This emphasizesthe evaluation of expressions, calledfunctions.

Examples include: Lisp, Scheme, Haskell.

Page 16: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 16/86

R D Appiah

TRANSLATION.

Programmes today are normally written in oneof the high-level languages.

To run the programme on a computer, the

programme needs to be translated  into themachine language of the computer (ie thetarget computer/ platform/ machine) on which itwill run.

The programme in a high-level language iscalled the source programme or  source

codes.

Page 17: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 17/86

R D Appiah

The translated programme in machinelanguage is called object programme.

METHODS OF TRANSLATION

The two methods used for translation are:

C ompilation and

Interpretation.

Page 18: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 18/86

Page 19: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 19/86

R D Appiah

The Translation Process:

The translation process involves thefollowing stages/ phases:

Lexical

Analyzer 

Syntax

Analyzer 

Semantic

Analyzer 

Code

Generator 

Source

File

Object

File

Page 20: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 20/86

R D Appiah

Lexical Analyzer:

 A lexical analyzer reads the source codes,symbol by symbol, and creates a list of tokens

in the source language.

 A token is the smallest individual unit in anyprogramming language.

For example, the five symbolsw,

h,

i ,

l ,

e areread and grouped together as the token w hile

in the C, C++ and Java languages.

Page 21: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 21/86

R D Appiah

Syntax Analyzer:

 A syntax analyzer is the means by which a setof tokens are used to form instructions.

For example, the tokens ³x´, ´=´, ´0´ are used tocreate the assignment statement: ³x = 0´ in the

C language.

Page 22: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 22/86

R D Appiah

Semantic Analyzer:

The semantic analyzer checks the sentencescreated by the syntax analyzer to ensure that

they contain no ambiguity.

Most computer languages are unambiguous ±

which means that this stage is either omitted ina translator, or its duty is minimal.

Page 23: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 23/86

R D Appiah

Code Generator:

 After unambiguous instructions are created bythe semantic analyzer, each instruction isconverted to a set of   machine language

instructions for the computer on which the programme will run.

This task is accomplished by the CodeGenerator .

Page 24: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 24/86

R D Appiah

Algorithm:

Definition: An ordered set of unambiguoussteps that produces a result and terminates in afinite time.

NOTE:

Algorithms are generally designed so as tobe independent of programming languages.

Algorithms usually accept input data andcreate output data.

Page 25: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 25/86

R D Appiah

Designing and Expressing an Algorithm:

Purpose: The purpose of an algorithm is toprovide a way to document one¶s ideas for  programme design.

Steps:

The several ways in which an algorithm can beexpressed include:

S tructured English Pseudocodes and Flow charts.

Page 26: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 26/86

R D Appiah

Structured English:

This is a subset of the English language with alimited selection of sentence structures thatreflect processing activities.

Pseudocodes:

Is an English-language-like representation of an

algorithm.

There is no standard for pseudocode!

Page 27: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 27/86

R D Appiah

Some people use a lot of detail, others useless.

Some people incorporate their favourite codeswhile others use syntax like those found in

programming languages.

This course emphasizes on the use of  algorithms and pseudocodes in describing andexpressing solutions to computer basedproblems.

Page 28: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 28/86

R D Appiah

Flowchart:

Flowchart is a graphical representation of theway a computer should progress from oneinstruction to the next when it performs a task.

Flowcharts are constructed from a set of  standardized symbols, called FlowchartSymbols.

Most algorithms can be represented usingflowcharts.

Page 29: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 29/86

R D Appiah

Example1

Problem: To calculate the price of an itemin a particular state in the US, the followingcriteria is employed: The state¶s sales tax is 4%;the city¶s sales tax is 1.5%; for luxury item suchas cars over $50, 000 there is a 10% luxury tax.

Task: Design an algorithm that calculates

the sales tax and the price of a car sold in theUS state above.

Page 30: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 30/86

R D Appiah

A sample algorithm is as follows:

1. Assume that:

sellingPrice is the selling price of an item in thestate before any tax is imposed;

salesTax represents the total tax imposed on an

item before being sold; stateSalesTax is the tax imposed by the state; citySalesTax is the tax collected by the city; luxuryTax represents the tax imposed if the item is

considered luxury; amountDue is the final selling price on an item in

the state.

Page 31: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 31/86

R D Appiah

A sample algorithm:

2 . Get the selling price of the item (before tax).

3. Determine whether the item is a luxury item

or not.

4. Find the state¶s tax using the formula:

stateSalesTax = sellingPrice * (4/100)

5. Find the city¶s tax using the formula:

citySalesTax = sellingPrice * (1.5/100)

Page 32: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 32/86

R D Appiah

6 . Find the luxury tax using the formula:

if( item is a luxury item)

luxuryTax = sellingPrice * (10/100)otherwise

luxuryTax = 0 

7. Find salesTax using the formula:

salesTax = stateSalesTax + citySalesTax +

luxuryTax 

8. Find amountDue using the formula:

amountDue = sellingPrice + salesTax 

Page 33: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 33/86

R D Appiah

Example2

Design an algorithm for the following programme:

Problem: A computer programme thataccepts as input given lengths expressed in feetand inches. The programme should thenconvert and output the lengths in centimetres.

 Assume that the given lengths in feet andinches are integers.

Page 34: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 34/86

R D Appiah

A sample algorithm:

1. Let ft and inch be the components of the

length in feet and inches respectively.

2 . Display: Prompt the user to enter the

distance in feet and inches as integers.

3. Input: Get the data ± ft and inch, as the

input values.

4. Echo the input ± in other words, output onthe screen what the programme read asinput, for certainty.

Page 35: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 35/86

R D Appiah

5. Find the total length in inches using thefollowing formula:

total_inches = (12 * ft  ) + inch

6 . Display: Output the total length in inches.

7. Convert the total length (in inches) to totallength (in centimetres) using the following

formula:

total_centimetres = total_inches * 2 .54

Page 36: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 36/86

Page 37: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 37/86

R D Appiah

Self Assessment 1: Write an algorithm thattakes as input, the length and breadth of a

rectangle and then calculates its area.Self Assessment 2: Write an algorithm thataccepts as input, a numeric grade (integer) and

changes it to an appropriate letter grade.Use the following grading system:

90 score 100 A80 score 89 B

70 score 79 C

60 score 69 D

0 score 59 E

Page 38: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 38/86

R D Appiah

Self Assessment 3

Scenario: 10 school children were tasked to fill acontainer, that has a volume capacity of 200 litres, withwater. Each child was given a gallon of volume 2.5

litres. At any point in time, all the children could arrive toempty their gallons of water into the container, BUT

only in a one-after-the-other fashion.

Problem:D

esign an algorithm for a computer programme, that monitors the container and thenannounces to the children, the status of the container.

Page 39: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 39/86

R D Appiah

Some Common Concepts

Page 40: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 40/86

R D Appiah

IdentifiersMost computer programmes are made up of 

featured data items and elements, usuallyreferred to as objects.

In order to uniquely recognize and control these

objects in programming settings, objects areassigned names, through identifiers.

Identifiers, thus, allow a programmer to name

objects in a programme.

This also helps the compiler to keep track of where objects are physically located.

Page 41: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 41/86

R D Appiah

Data Types

 A data type defines a set of values and a set of 

operations that can be applied to those values.

The set of values for each type is known as thedomain for that particular data type.

 At the highest level, most programminglanguages define two categories of data types:

Simple data types and

Composite data types.

Page 42: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 42/86

R D Appiah

Simple Data Types

 A simple data type (sometimes called an

atomic type, fundamental type, scalar type,or built-in type) is a data type that cannot befurther broken into smaller data types.

Several simple data types have been defined toinclude:An Integer type: Is a whole number, that is, anumber without a fractional part.

The range of values an integer can takedepends on the programming language. SomeLanguages support several integer sizes.

Page 43: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 43/86

Page 44: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 44/86

R D Appiah

Composite Data Types

 A composite data type is a set of elements in

which each element is a simple type or acomposite type.

Most languages define the following composite

types:1. An array:  A set of elements each

of the same type.

2. A record:  A set of elements inwhich the elements can be of differenttypes.

Page 45: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 45/86

R D Appiah

Variables

Variables are names for memory locations.

Every memory location in a computer has aunique address, called memory address;which the computer uses internally.

Importance of Variables

1. Physically, the programmer does not knowthe relative addresses of data items in

memory and through variables, theprogrammer gets indirect access to thecomputer¶s memory resources.

Page 46: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 46/86

R D Appiah

2. Secondly, a data item may occupy more thanone location in memory, which makes it

inconvenient for a programmer to control -and through variables, such internal detailsare hidden from the programmer.

We say that through variables, assubstitutes for memory addresses, a

programmer is freed to concentrate on

the level at which the programmeexecutes.

Page 47: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 47/86

R D Appiah

Normally, variables are created and managedso as to hold data items of a particular type.

Variable Declaration.

 As part of the process of creating variables,

variable declaration alerts the computer thata variable with a given name and type will beused in the programme.

In response, the computer reserves therequired storage area and names itaccordingly.

Page 48: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 48/86

R D Appiah

Example: The following illustrates thedeclaration of the variables response, num

and result :-1. In C, C++ and Java:

char response;

int num;double result ;

2. In Visual Basic:

Dim response As StringDim num As Integer 

Dim result As Double

Page 49: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 49/86

R D Appiah

In the C, C++ and Java example, response isdeclared as of type character ; num of type

integer and result of type real

In the second example, response is declaredto be of the type String; num of the type

Integer and result to be of the type double-precision floating point number.

In the examples above, response, num andresult are the names of the variables declared.

Page 50: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 50/86

Page 51: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 51/86

R D Appiah

Constants

 A constant, like a variable, is a named locationthat can store a value, but the value cannot bechanged after it has been defined at the

beginning of the programme.

For instance, if there is the need to alter thevalue of a constant used in a programme, we

only need to change just one line at thebeginning of the programme !

Page 52: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 52/86

R D Appiah

Consider an example in a C, or C++ programme: The tax rate to be utilized in a

programme can be defined at the beginningand used during the programme.

Illustration:

const float taxRate = 2.09;. . .cost = price * taxRate;

Note also that a constant, like a variable, has atype and must be defined when the constant isdeclared.

Page 53: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 53/86

R D Appiah

Input and Output

Input:Data is input by either a statement or apredefined function.

For example, a function called scanf is used inthe C language to read data from the keyboard,formats it, and stores it in a variable, as follows:

scanf(³%d´, &num)

Page 54: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 54/86

R D Appiah

When the programme encounters thisinstruction, it waits for the user to type an

integer.

The programme then stores the value in the

variable called num.Output:

Data is also output by either a statement or a

predefined function.For example, the C language uses a functioncalled printf to display strings on the monitor.

Page 55: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 55/86

Page 56: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 56/86

R D Appiah

Examples include:

Arithmetic Operators in C, C++ and Java

Operator Definition Example

+ Addition 3+4

- Subtraction 4-5

* Multiplication Num * 4

/ Division Sum/count

% Modulo Operator 7%2

++ Increment (adds 1 to the value) Count ++

-- Decrement (subtract 1 from the

value)

Count --

R l ti l O t i C C d J

Page 57: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 57/86

R D Appiah

Relational Operators in C, C++ and Java:

They are used to compare data to see if a

value is greater than, less than, or equal toanother.

Operator Definition Example

< Less than Num1 < 4<= Less than or equal to Num 1 <= 4

> Greater than Num 2 > 8

>= Greater than or equal to Num2 >= 8

== Equal to Num1 == num2

!= Not equal to Count != num2

L i l O t

Page 58: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 58/86

R D Appiah

Logical Operators:

They combine Boolean values (true or false) to

get a new value.

Operat

or

Definition Example

! Not ! (num1 < num2)

&& And (num1 < 5) && (num2>10)

|| Or (num1 < 5) || (num2>10)

The C language uses the following threelogical operators:

St t t

Page 59: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 59/86

R D Appiah

Statements

A statement causes an action to be

performed by the programme. Usually, a statement translates directly intoone or more executable computer instructions.

Many languages define several types of statements, such as:

Assignment statements Compound statements

Control statements

A i t St t t

Page 60: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 60/86

R D Appiah

Assignment Statements

 An assignment statement assigns a value to a

variable.We can also say that an assignment statementstores the value in the variable, which has

already been created in the declaration sectionof a programme

In algorithms, we can use the symbol

to define assignment.

Most programming languages like C C++ and

Page 61: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 61/86

R D Appiah

Most programming languages like C, C++, andJava use the symbol = for assignment.Other programming languages, like Ada andPascal, the symbol := is used for assignment.

The following illustrates the declarationfollowed by assignment statements involving

variables and operators in C and C++:int count, x, y;

char ch;

ch = µY¶;

count = x + 34;

count = (x + y)/10;

C d St t t

Page 62: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 62/86

R D Appiah

Compound Statements

In programming, a compound statement is a

unit of code consisting of zero or morestatements.

 A compound statement is also known as a

block. A compound statement allows a group of statements to be treated as a single unit.

Normally, a compound statement consist of anopening brace, a statement section,followed by a closing brace.

Ill t ti

Page 63: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 63/86

R D Appiah

Illustration:

if( x == 1){

x++;

y = x + 24;}

The portion of the construct above, in bold, isa compound statement.

C t l St t t

Page 64: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 64/86

R D Appiah

Control StatementsControl statements are statements that specify

the sequence in which a programme isexecuted.

Most programming languages have three

types of control statements:1. Sequence Control Statement

2.S

election ControlS

tatement

3. Repetition Control Statement

Page 65: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 65/86

In the previous example by executing the

Page 66: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 66/86

R D Appiah

In the previous example, by executing theGOTO command, the computer is directed to a

different part of the programme, labelledmySkip.

The example can be Illustrated using a

flowchart presentation, as shown on the nextslide:

Page 67: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 67/86

R D Appiah

Start

End

Print This is the first line

Print This is the first line

Print All Done !

GOTO mySkip

mySkip

Selection Control Statements

Page 68: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 68/86

R D Appiah

Selection Control Statements

These are used to tell the computer what to

do, based on whether a condition is TRUE or FALSE.

 A simple example of a selection controlstructure is the IF « THEN « ELSEcommand.

Example: Consider the following programmeconstruct, in which a selection control is

used to test a number  ± and based on theoutcome, a particular output statement isprinted on the screen:

Page 69: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 69/86

R D Appiah

Input ³Enter a number from 1 to 40: ´; num

If num > 10 Then

Print ³The number is greater than 10.´

Else

Print ³The number is 10 or less.´

End

The following diagram illustrates how a

computer follows instructions in a selectiondecision structure, as depicted in the aboveexample:

Page 70: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 70/86

R D Appiah

Start

End

Input a number between 1 and 40

Print The number is

greater than 10

Print The number is

10 or less

Is

number

> 10

YESNO

Repetition Control Statements

Page 71: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 71/86

R D Appiah

Repetition Control Statements

These are used to direct the computer to

repeat one or more instructions until acertain condition is met or satisfied.

The section of code that repeats is usually

referred to as a loop or an iteration.

Some of the most widely used repetitioncommands are:

The FOR « NEXT loop The DO « UNTIL loop and WHILE « WEND loop

Page 72: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 72/86

Page 73: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 73/86

Example1 using the FOR NEXT Loop

Page 74: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 74/86

R D Appiah

Example1 using the FOR « NEXT Loop

For N =1 To 10

Print ³There is no place like home´

Next N

End

 Above is a simple programme in basic usingthe For « Next command to print the

message ³There is no place like home´ tentimes.

Example2 using the FOR NEXT Loop

Page 75: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 75/86

R D Appiah

Example2 using the FOR « NEXT Loop

for(x=1; x 10; x++)

printf(³There is no place like home´);

Example2 illustrates how the same task canbe accomplished in the C programminglanguage. Thus, executing the above loop in C 

will print out ten times, the statement ³There isno place like home´

One very important application of loops is in

Page 76: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 76/86

R D Appiah

One very important application of loops is inthe handling and processing of a special

variable type called an array.Arrays These are special variable types used to holdor store a group of related data items all of 

the same type. Each of the data items is called an element

of the array.

Application(s) of arrays: Arrays are very useful in many situations; andthe following describe some of them:

1. In sorting a list of items either in an

Page 77: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 77/86

R D Appiah

1. In sorting a list of items either in anascending or descending order.

2. In processing a long list of numbers, as inperforming such operations like summingup the numbers, searching for themaximum and minimum numbers in a list.

3. In accepting a list of data items thatrequires further processing, especiallywhere such processing may involve all suchdata items at the same time.

4. In finding an average score of a set of data;as in student scores, temperatures, etc.

Self Assessment:

Page 78: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 78/86

R D Appiah

Self Assessment:

List 10 different scenarios, in real life, whereyou think the use of an array will be Appropriate and beneficial.

Array Name and Array Elements

Just as in other ordinary variables, arrays mustbe uniquely identified in a programme.

 Arrays, thus, are given names which areusually specified during their declarations.

For example the following illustrates the

Page 79: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 79/86

R D Appiah

For example, the following illustrates thedeclaration of an array in Visual Basic:

Dim scores(15)

Dim scores(15) As Integer 

The first declaration creates an array calledscores; and it is big enough to hold up to 15elements.

Here, the array scores can be used to storedata of any type.

In the second declaration the arra scores is

Page 80: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 80/86

R D Appiah

In the second declaration, the array scores isspecified to hold data of type integer.

We can refer to the elements in the array asthe first element, second element, and soforth until we get to the last element.

Normally, an element is represented by thename of the array followed by a number 

enclosed in a pair of square brackets.

The number (referred to as an index) withinthe square brackets is used to indicate theposition of an element within the array.

Page 81: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 81/86

Subprogrammes

Page 82: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 82/86

R D Appiah

Subprogrammes

Generally speaking, this is a very important

concept more in procedure-orientedprogramming paradigms than it is in object-oriented programming paradigms.

In procedure-oriented programming, the maincomputer programme is divided into ³smaller´programmes, referred to as subprogrammes.

Each subprogramme is so designed to handleor to solve a specific problem ± whichrepresents a well-defined task.

Thus a subprogramme is considered to be a

Page 83: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 83/86

R D Appiah

Thus, a subprogramme is considered to be asmaller programme within a bigger/ main

programme, to perform a specific task.Some advantages of subprogrammes:

1. They make programming easier 

2. They help in making a programme moreorganized and structured

3. They help programmers to avoid repeatingwriting same source codes over and over again.

4. Also, detecting errors in a subprogramme iseasier than in the main programme.

Recursion

Page 84: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 84/86

R D Appiah

Recursion

This is a programming concept and technique

used mostly in advanced programming.

Recursion occurs in a computer programme if the programme calls itself within its own

structure or definition in handling a task.

 A classic example is the calculation of thefactorial of a number.E.g.

4! = 4*3*2*1

To solve the previous example recursively with

Page 85: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 85/86

R D Appiah

To solve the previous example recursively witha computer programme, we will have thefollowing:

4! = 4 * 3!

From a closer look, one can say that ³in findinga value for 4 factorial, the computer calls on 3

factorial.

In other words, we have a programme calledfactorial on the left-hand side of theassignment statement(represented by !)calling itself on the right-hand sideof the assignment statement.

Thus recursion occurs whenever a programme

Page 86: Ict 155 Intro to Programming

8/8/2019 Ict 155 Intro to Programming

http://slidepdf.com/reader/full/ict-155-intro-to-programming 86/86

Thus, recursion occurs whenever a programmecalls itself within the same expression or 

assignment statement.