0% found this document useful (0 votes)
46 views74 pages

MATH 269 All Units

Uploaded by

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

MATH 269 All Units

Uploaded by

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

Introduction To Programming For Mathematics

MATH 269: Introduction To Programming For Mathematics

E. Owusu – Ansah

Department of Mathematics
KNUST - Ghana

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 1 / 74


Outline

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 2 / 74


Uses Of Computers

Think about some of the different ways that people use computers.
In school, students use computers for tasks such as
writing papers,
searching for articles,
sending email,
and participating in online classes.
At work, people use computers to
analyze data,
make presentations,
conduct business transactions,
communicate with customers and
coworkers, control machines in manufacturing
facilities, and many other things.

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 3 / 74


Uses Of Computers
At home, people use computers for tasks such as
paying bills,
shopping online,
communicating with friends and family, and
playing computer games.
Other uses include, listening to music, cash withdrawal from an ATM
machine etc

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 4 / 74


Basic Terminologies
Terminologies
Computer Program: A set of instructions that direct the computer
to perform a certain task.
Programs are commonly referred to as software. Software is
essential to a computer because without software, a computer can
do nothing.
All software are created by individuals known as programmers or
software developers.
A Programmer Or Software Developer is a person with the training
and skills necessary to design, create, and test computer
programs.
Computer Programming: The art of writing a computer program
Computer programming is an exciting and rewarding career.
Programmers work almost everywhere, in businesses, medicine,
law enforcement, academics, entertainment, agriculture etc.
Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 5 / 74
Hardware

The term hardware refers to all of the physical devices, or


components, that a computer is made of.
A computer is not one single device, but a system of devices that
all work together.
A typical computer system consists of the following major
components:
The central processing unit (CPU)
Main memory
Secondary storage devices
Input devices
Output devices

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 6 / 74


Typical Components of a Computer System
The central processing unit (CPU): the part of a computer that actually
runs programs. The CPU is the most important
component in a computer because without it, the
computer could not run software.
Main Memory/Random Access Memory(RAM): You can think of main
memory as the computer’s work area. This is where the
computer stores a program while the program is running,
as well as the data that the program is working with.
Secondary storage: A type of memory that can hold data for long
periods of time, even when there is no power to the
computer. Programs are normally stored in secondary
memory and loaded into main memory as needed.
Input: Any data the computer collects from people and from
other devices.
Output: Any data the computer produces for people or for other
devices.
Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 7 / 74
Typical Components of a Computer System

Figure : Typical components of a computer system

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 8 / 74


How Computers Store Data

All data that is stored in a computer is converted to sequences of


0s and 1s.
A computer’s memory is divided into tiny storage locations known
as bytes.
One byte is only enough memory to store a letter of the alphabet or
a small number.
In order to do anything meaningful, a computer has to have lots of
bytes.
Most computers today have millions, or even billions, of bytes of
memory.
Each byte is divided into eight smaller storage locations known as
bits.
The term bit stands for binary digit.
Computer scientists usually think of bits as tiny switches that can be
either on or off.

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 9 / 74


How Computers Store Data
In most computer systems, bits are tiny electrical components that
can hold either a positive or a negative charge.
Computer scientists think of a positive charge as a switch in the on
position, and a negative charge as a switch in the off position.
A computer scientist might think of a byte of memory: as a
collection of switches that are each flipped to either the on or off
position.

Figure : Thinking of a byte as eight switches


Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 10 / 74
How Computers Store Data

When a piece of data is stored in a byte, the computer sets the


eight bits to an on/off pattern that represents the data.
For example, the pattern shown on the left in Figure below shows
how the number 77 would be stored in a byte,
and the pattern on the right shows how the letter A would be
stored in a byte.

Figure : Bit patterns for the number 77 and the letter A

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 11 / 74


How Computers Store Data
Storing Numbers

A bit can be used in a very limited way to represent numbers.


Depending on whether the bit is turned on or off, it can represent
one of two different values.
In computer systems, a bit that is turned off represents the
number 0 and a bit that is turned on represents the number 1
corresponding to the binary numbering system.
An example of a number that is written in binary is 10011101

Figure : The values of binary digits 10011101


Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 12 / 74
How Computers Store Data
Storing Numbers

To determine the value of a binary number you simply add up the


position values of all the 1s.
For example, in the binary number 10011101, the position values
of the 1s are 1, 4, 8, 16, and 128, shown in the Figure below.
The sum of all of these position values is 157. So, the value of the
binary number 10011101 is 157.

Figure : The value of 10011101 and the bit pattern for 157

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 13 / 74


How Computers Store Data
Storing Large Numbers

When all of the bits in a byte are set to 0 (turned off), then the
value of the byte is 0.
When all of the bits in a byte are set to 1 (turned on), then the byte
holds the largest value that can be stored in it. Thus the largest
value 255 because there are only eight bits in a byte.
What if you need to store a number larger than 255? The answer
is simple: use more than one byte.
For example, suppose we put two bytes together. That gives us 16
bits, shown in Figure below, with a maximum value of 65,535.

Figure : Two bytes used for a large number


Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 14 / 74
How Computers Store Data
Storing Characters

Any piece of data that is stored in a computer’s memory must be


stored as a binary number.
That includes characters, such as letters and punctuation marks.
When a character is stored in memory, it is first converted to a
numeric code.
The numeric code is then stored in memory as a binary number.
Over the years, different coding schemes have been developed to
represent characters in computer memory.
Historically, the most important of these coding schemes is ASCII
(pronounced askee), which stands for the American Standard
Code for Information Interchange.

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 15 / 74


How Computers Store Data
Storing Characters

ASCII is a set of 128 numeric codes that represent the English


letters, various punctuation marks, and other characters.
For example, the ASCII code for the uppercase letter A is 65.
When you type an uppercase A on your computer keyboard, the
number 65 is stored in memory (as a binary number, of course).
This is shown in Figure below.

Figure : The letter A is stored in memory as the number 65

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 16 / 74


Storing Characters: ASCII Codes from 32 to 127

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 17 / 74


Storing Characters: ASCII Codes

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 18 / 74


Storing Characters: Other Standard Codes

The ASCII code is not the only computer code, but it is typical,
and it is used in most personal computers.
The Extended Binary Coded Decimal Interchange Code, or
EBCDIC, is an eightbit code that is used in IBM mainframe
computers. In these computers, the principle is the same–every
character is stored in a byte as a series of binary digits.
However, the actual values used are different. For example, in
EBCDIC, an A is 11000001, or 193.
Another code used by languages such as Java and C# is Unicode;
with this code, 16 bits are used to represent each character.
The character A in Unicode has the same decimal value as the
ASCII A, 65, but it is stored as 0000000001000001.
Using two bytes provides many more possible combinations than
using only eight bits – 65,536 to be exact. With Unicode, enough
codes are available to represent all English letters and digits, as
well as characters from many international alphabets

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 19 / 74


Machine Language
The CPU performs simple operations on pieces of data.
The CPU does nothing on its own. It has to be told what to do, and
that’s the purpose of a program.
A program is nothing more than a list of instructions that cause the
CPU to perform operations.
Each instruction in a program is a command that tells the CPU to
perform a specific operation. Here is an example of an instruction
that might appear in a program: 10110000.
This is only a series of 0s and 1s for human beings, to a CPU,
however, this is an instruction to perform an operation.
It is written in 0s and 1s because CPUs only understand
instructions that are written in machine language, and machine
language instructions are always written in binary.
A machine language instruction is a binary number that
represents a command that tells the CPU to perform an operation.
Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 20 / 74
How A Program Works

A computer’s CPU can only understand instructions that are


written in machine language.
Because people find it very difficult to write entire programs in
machine language, other programming languages have been
invented.
The CPU is an electronic device that is designed to do specific
things. In particular, the CPU is designed to perform operations
such as the following:
Reading a piece of data from main memory
Adding two numbers
Subtracting one number from another number
Multiplying two numbers
Dividing one number by another number
Moving a piece of data from one memory location to another
Determining whether one value is equal to another value
And so forth . . .

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 21 / 74


How Are Programs Executed By CPUs?

Programs are usually stored on a secondary storage device such


as a disk drive.
Although a program can be stored on a secondary storage device
such as a disk drive, it has to be copied into main memory, or
RAM, each time the CPU executes it.
For example, suppose you have a word processing program on
your computer’s disk. To execute the program you use the mouse
to double–click the program’s icon. This causes the program to be
copied from the disk into main memory. Then, the computer’s
CPU executes the copy of the program that is in main memory.

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 22 / 74


Illustration: How Programs Are Executed By CPUs

Figure : A program is copied into main memory and then executed

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 23 / 74


Fetch–Decode–Execute Cycle
When a CPU executes the instructions in a program, it is engaged
in a process that is known as the fetch-decode-execute cycle.
This cycle, which consists of three steps, is repeated for each
instruction in the program. The steps are:

Figure : The fetch-decode-execute cycle

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 24 / 74


From Machine Language to Assembly Language
A program can have thousands, or even a million or more binary
instructions, and writing such a program would be very tedious
and time consuming.
Programming in machine language would also be very difficult
because putting a 0 or a 1 in the wrong place will cause an error.
Although a computer’s CPU only understands machine language,
it is impractical for people to write programs in machine language.
Assembly language is an alternative to machine language that
uses short words that are known as mnemonics instead of using
binary numbers for instructions.
For example, in assembly language, the mnemonic add typically
means to add numbers, mul typically means to multiply numbers,
and mov typically means to move a value to a location in memory.
When a programmer uses assembly language to write a program,
he or she can write short mnemonics instead of binary numbers.
Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 25 / 74
Assembler
Assembly language programs cannot be executed by the CPU,
however.
The CPU only understands machine language, so a special
program known as an assembler is used to translate an assembly
language program to a machine language program.

Figure : An assembler translates an assembly language program to a


machine language program

The machine language program that is created by the assembler


can then be executed by the CPU.
Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 26 / 74
Low–Level Language

Although assembly language makes it unnecessary to write binary


machine language instructions, it is not without difficulties.
Assembly language is primarily a direct substitute for machine
language, and like machine language, it requires that you know a
lot about the CPU.
Assembly language also requires that you write a large number of
instructions for even the simplest program.
Because assembly language is so close in nature to machine
language, it is referred to as a low-level language.

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 27 / 74


High Level Languages

In the 1950s, a new generation of programming languages known


as high–level languages began to appear.
A high–level language allows you to create powerful and complex
programs without knowing how the CPU works, and without
writing large numbers of low–level instructions.
In addition, most high-level languages use words that are easy to
understand.
Examples of well known high–level languages include: Ada,
BASIC, FORTRAN, COBOL, Pascal, C and C++, C#, Java,
JavaScript, MATLAB, MAPLE, MATHEMATICA, Python, Ruby,
Visual Basic etc

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 28 / 74


Compilers and Interpreters

Because the CPU understands only machine language instructions,


programs that are written in a high-level language must be translated
into machine language.
Once a program has been written in a high-level language, the
programmer will use a compiler or an interpreter to make the translation.
A compiler is a program that translates a high-level language program
into a separate machine language program. The machine language
program can then be executed any time it is needed.
An interpreter is a program that both translates and executes the
instructions in a high–level language program. As the interpreter reads
each individual instruction in the program, it converts it to a machine
language instruction and then immediately executes it.
NOTE: Compiled programs generally execute faster than interpreted
programs because it is already translated entirely to machine language
when it is executed. A program that is interpreted must be translated at
the time it is executed.
Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 29 / 74
Illustration: Compilation and Execution of a
High–Level Program

Figure : Compiling a high-level program and executing it

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 30 / 74


Illustration: Execution of a High–Level Program By An
Intepreter

Figure : Executing a high–level program with an interpreter

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 31 / 74


Integrated Development Environments

The statements that a programmer writes in a high-level language


are called source code, or simply code.
Typically, the programmer types a program’s code into a text editor
[such as Notepad, Notepad++, Textpad etc] and then saves the
code in a file on the computer’s disk.
Most programmers use specialized software packages called
integrated development environments or IDEs. Most IDEs
combine the following programs into one software package:
A text editor that has specialized features for writing statements in a
high-level programming language
A compiler or interpreter
Useful tools for testing programs and locating errors

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 32 / 74


Types of Software

Programs generally fall into one of two categories: system


software or application software.
System software is the set of programs that control or enhance the
operation of a computer.
Application software makes a computer useful for everyday tasks.
System software typically includes the following types of
programs:
Operating Systems which controls internal operations of the
hardware, manages all connected devices, permits data storage
and retrieval and allows other programs to run.
Utility Programs which performs a specialized task that enhances
the computer’s operation or safeguards data. Example: Virus
scanners, file compression programs etc
Software Development Tools programs that programmers use to
create, modify, and test software. Assemblers, compilers, and
interpreters are examples

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 33 / 74


Application Software

Programs that make a computer useful for everyday tasks are


known as application software.
These are the programs that people normally spend most of their
time running on their computers.
Examples of commonly used applications are Microsoft Word: a
word processing program, and Microsoft PowerPoint: a
presentation program, Microsoft Excel: a spreadsheet program,
email programs, Web browsers, and game programs.

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 34 / 74


A General Problem-Solving Strategy

To create a suitable plan of action to solve a particular problem, it is


often useful to apply the following general problem-solving strategy:
1 Try to understand the problem completely. If you don’t fully
understand the problem, it will be difficult, or even impossible, to
create a viable plan to solve it.
2 Devise a plan of action to solve the problem. Provide precise
step–by–step instructions for doing so.
3 Carry out the plan.
4 Review the results. Did the plan work? Did it solve the given
problem?

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 35 / 74


Creating Computer Programs
The Program Development Cycle

The general process for creating a computer program mimics the


general problem solving strategy that we have just outlined:
understand the problem, devise a plan, carry out the plan, and
review the results.
When you solve a problem with a computer program, this strategy
takes the following form:
Step 1: Problem Analysis The problem is analysed by
1 First making sure that the problem to be solved is
fully understood
2 Clearly stating the problem to solved.
3 Defining the inputs required by the program and the
outputs to be expected from the program.
Step 2: Program Design Design the algorithm that you intend to
implement in the program.

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 36 / 74


Creating Computer Programs
The Program Development Cycle

Step 3: Program Coding Turn the algorithm into desired high–level


computer programming language (e.g. MATLAB, C++,
Java, Visual Basic) statements.
Step 4: Program Testing Test the resulting program. Does the
program give the expected output?
If not, GO BACK TO Step 2, look for and fix the errors

Figure : The program development cycle

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 37 / 74


Analysing The Problem

The problem analysis stage involves asking asking ourselves the


following important questions:
1 What results are we trying to obtain? That is,
what is the required output?
2 How will we obtain the required output from the given input? That
is,
what formulas and processes are needed carry out in order to get
the desired results.
3 What data needs to be given? That is,
what is the necessary input?

Choose variables to represent the given input and the required


output. Decide on appropriate variable
variable names
data types eg. String , Numeric – Integer / floating point, arrays etc

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 38 / 74


Analysing The Problem
Understanding Variables & Datatypes

A data item’s data type is a classification that describes the


following:
What values can be held by the item
How the item is stored in computer memory
What operations can be performed on the data item
All programming languages support two broad data types;
numeric describes data that consists of numbers which comprises
integers(whole numbers) and
floating–point(real numbers)
and string describes data that is nonnumeric.
String constants, unlike numeric constants, appear within single(’ ’) or
double(" ") quotation marks in computer programs.
String values are also called alphanumeric values because they can
contain alphabetic characters as well as numbers and other
characters.
Although strings can contain numbers, numeric values cannot
contain alphabetic characters.
Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 39 / 74
Analysing The Problem: Working With Variables
Variable Declaration

Variables are named memory locations whose contents can vary


or differ over time.
Most programming languages demand apriori declarationn of any
variable before its usage.
A declaration is a statement that provides a data type and an
identifier for a variable.
An identifier is a program component’s name
Declaring a variable provide both the data type and an identifier of
the variable.
Optionally, one can declare a starting/initial value for any variable.
Declaring a starting value is known as initializing the variable.

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 40 / 74


Analysing The Problem: Working With Variables
Naming Variables

Every computer programming language has its own set of rules


for creating identifiers.
Most languages allow alphabets/letters(a–z, A–Z) and digits(0–9)
within identifiers in addition to some few other printable characters
eg. underscore, period
Each programming language has a few (perhaps 100 to 200)
reserved keywords that are not allowed as variable names
because they are part of the language’s syntax.
Different languages put different limits on the length of variable
names, although in general, the length of identifiers in newer
languages is virtually unlimited.
In many languages, identifiers are case sensitive, so
HoUrLyWaGe, hourlywage, and hourlyWage are three separate
variable names.
Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 41 / 74
Analysing The Problem: Working With Variables
Naming Variables

Common variable naming conventions include the following:


Camel casing The first letter of an identifier is lowercase and the
first letter of each subsequent concatenated word is
capitalized, eg hourlyWage, iPhone, macOS etc.
Pascal casing/Upper Camel Case first letter of each
concatenated word is capitalized, eg HourlyWage,
BackColor,FirstName,ComputerRamSize etc.
Hungarian notation a variable’s data type is part of the identifier,
eg numHourlyWage or stringLastName.
Preferred naming convention often depends on the programming
language or standards implemented by one’s employers.
The following rules are the same for all languages:
All variable names must be one word(spaces are never allowed).
Variable names starting with a letter(common convention).
Variable names shall not begin with a digit
Variable names should be meaningful
Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 42 / 74
Planning The Logic
Algorithm

The heart of the programming process lies in planning the program’s


logic which programmers generally refer to as "developing an algorithm."
An algorithm is a step-by-step procedure for solving a problem.
An algorithm may be expressed as a Flowchart or Pseudocode
Flowchart: A visual or graphical representation of an algorithm.
Employs a series of blocks and arrows, each of which
represents a particular operation or step in the
algorithm.
The arrows represent the sequence in which the
operations are implemented.
Pseudocode: (pronounced "sue doe code") Uses code-like statements
in place of the graphical symbols of the flowchart. The
word pseudo means fake, so pseudocode is fake code. It
is an informal language that has no syntax rules, and is
not meant to be compiled or executed.

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 43 / 74


Flowchart Symbols

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 44 / 74


Modular/Top-Down Programming Design

Programming Problem Design Process


Decomposition of computer programs into small subprograms, or
modules, that can be developed and tested separately.
Advantages
Makes the underlying logic easier to devise and to understand for
both the developer and the user.
Development is facilitated because each module can be perfected
in isolation.
Increases the ease with which a program can be debugged and
tested because errors can be more easily isolated.
Program maintenance and modification are facilitated.

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 45 / 74


Flow Diagram of Modular/Top-Down Programming
Design

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 46 / 74


Unstructured Programming
Spaghetti Code

Logically snarled program


statements is spaghetti code,
because the logic is as hard to
follow as one noodle through a
plate of spaghetti.
Spaghetti code are confusing,
prone to error, difficult to
reuse, and hard to use as
building blocks for larger
applications.
For example, suppose that you
start a job as a dog washer
and that you receive the Figure : Spaghetti code logic for
instructions shown in Figure. washing a dog
Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 47 / 74
Structured Programming
A structure, also called a control structure, is a logical design that
controls the order in which a set of statements execute.
Structured Programming is a set of rules that prescribe good style
habits for the programmer.
Key Idea : Any numerical algorithm can be composed using the
three fundamental control structures:
sequence,
selection, and
loop Or repetition.

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 48 / 74


Sequential Flow
A set of statements that execute in the order that they appear is
called a sequence structure.
With a sequence structure, you perform an action or task, and
then you perform the next action, in order. The computer code
implemented one instruction at a time.
A sequence can contain any number of tasks, but there is no
option to branch off and
Once you start a series of actions in a sequence, you must
continue step by step until the sequence ends.

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 49 / 74


Selection/Branching/Switching/Decision
With this structure, you ask a question and, depending on the
answer, you take one course of action.
Provides a means to split the program’s flow into branches based
on the outcome of a logical condition.
Fundamental ways of implementation are:
Single-alternative selection(IF/THEN) and
Double-alternative selection (IF/THEN/ELSE).
Multiple-alternative selection (IF/THEN/ELSE).

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 50 / 74


Flowchart and Pseudocode
Double-Alternative Selection Constructs

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 51 / 74


Flowchart and Pseudocode
Multiple-Alternative Selection Constructs

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 52 / 74


Multiple-Alternative Selection Constructs
Illustration

Consider a school at which tuition fee varies per semester, depending


on whether a student is a first, second, third, or final year.

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 53 / 74


Flowchart and Pseudocode
Select Case Constructs

You can use the case structure when there are several distinct
possible values for a single variable, and each value requires a
different subsequent action.
When using the case structure, you compare a variable to a series
of test values, taking appropriate action when a match is found.
You use the case structure only when a series of decisions is
based on different values stored in a single variable

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 54 / 74


Select Case Constructs
Illustration

Consider a school at which tuition fee varies per semester, depending


on whether a student is a first, second, third, or final year.

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 55 / 74


Relational Operators

There are six standard relational operators, as shown in Table


below, together with the symbols we use to represent them.

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 56 / 74


Logical Operators

Logical operators are used to create compound conditions from


given simple conditions.
The most important logical operators are – OR, AND, and NOT.
The logical AND operator and the logical OR operator allow you to
connect multiple Boolean expressions to create a compound
expression.
The logical NOT operator reverses the truth of a Boolean
expression.

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 57 / 74


Hierarchy of Operations

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 58 / 74


Repetition/Looping
Provides a means to implement instructions repeatedly.
The resulting constructs, called loops, come in two "flavors"
distinguished by how they are terminated.
The Two Major Forms are Decision loop and Count-Controlled
loop.
A Decision/Break/while Loop : Structure repeats until a logical
condition is true.
Termination based on the result of a logical condition.
A Count-Controlled or DOFOR loop: Performs a specified number
of repetitions, or iterations.

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 59 / 74


Flowchart: while (– do) loop Or do – while loop

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 60 / 74


Flowchart and Pseudocode for Repetition:
Count-Controlled or for loop

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 61 / 74


Stacked Structures
All logic problems can be solved using only these three
structures–sequence, selection, and loop.
The structures can be combined in an infinite number of ways.
Attaching structures end to end is called stacking structures.

Figure : Structured flowchart and pseudocode with three stacked structures


Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 62 / 74
Nested Structures
There is no limit to the number of levels you can create when you
nest and stack structures.
Figure below shows logic that has been made more complicated
by replacing stepN with a selection.

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 63 / 74


Familiarisation with MATLAB IDE

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 64 / 74


Using MATLAB as Calculator

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 65 / 74


Arithmetic and Assign Operators In MATLAB

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 66 / 74


Using Mathematical Functions In MATLAB

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 67 / 74


Vectors and Matrices In MATLAB

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 68 / 74


Selection/Branching In MATLAB

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 69 / 74


Relational Operators In MATLAB

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 70 / 74


Logical Operators In MATLAB

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 71 / 74


Looping/Repetition In MATLAB

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 72 / 74


MATLAB m–files: Scripts and Functions

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 73 / 74


Plotting in MATLAB

Onassis (Maths Dept, KNUST – Ghana) Intro To Computer Programming 74 / 74

You might also like