0% found this document useful (0 votes)
11 views

1chp8 Programming

Uploaded by

unknownfighter9
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

1chp8 Programming

Uploaded by

unknownfighter9
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 60

PROGRAMMING

For IGCSE Computer Science the high-level programming


languages recommended are Python, Visual Basic or Java.

 Many programming languages need an interactive


development environment (IDE) to write and run code.
Programming concepts
There are five basic constructs to use and understand
when developing a program:
» data use – 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.
variable: A variable in a computer program is a
named data store than contains a value that may
change during the execution of a program.

 Constant: A constant in a computer program is a


named data store than contains a value that does
not change during the execution of a program.

 There are several ways of highlighting a constant,


for example:
Basic data types
The basic data types you will need to use for IGCSE Computer Science
are:
» integer – a positive or negative whole number that can be used with
mathematical operators
» real – a positive or negative number with a fractional part. Real
numbers can be used with mathematical operators

» char – a variable or constant that is a single character

» string – a variable or constant that is several characters in length.


Strings vary in length and may even have no characters (known as an
empty string); the characters can be letters and/or digits and/or any
other printable symbol (If a number is stored as a string then it cannot
be used in calculations.)
» Boolean – a variable or constant that can have only two values TRUE
or FALSE.
Input and output
 Each input needs to be accompanied by a prompt
stating the input required

 In a programming language the data type of the


input must match the required data type of the
variable where the input data is to be stored.

 All inputs default as strings, so if the input should


be an integer or real number, commands are also
used to change the data type of the input (for
instance, in Python these are int() or float()).
Examples of input statements with prompts

For a program to be useful, the user needs to know


what results are being output, so each output needs
to be accompanied by a message explaining the
result. If there are several parts to an output
statement, then each part is separated by a
separator character.
Examples of output statements with messages

Basic concepts
When writing the steps required to solve a problem,
the following concepts need to be used and
understood:
» sequence
» selection
» iteration
» counting and totalling
» string handling
» use of operators.
Sequence
 The ordering of the steps in an algorithm is very
important. An incorrect order can lead to incorrect
results and/or extra steps that are not required by
the task.
 Example:

 Calculate square of a number

BEGIN
DECLARE num, result : INTEGER
OUTPUT "Enter a number for calculate the power of the number"
INPUT num
result  num*num
OUTPUT "Power of the Number:" + result
END
Selection
Selection is a very useful technique, allowing
different routes through the steps of a program.

For example, data items can be picked out according


to given criteria, such as: selecting the largest value
or smallest value, selecting items over a certain
price, selecting everyone who is male.
IF statements single choice with alternative
Case statements
 Case statements are used when there are multiple
choices to be made.
Example:
Iteration
There are three types of loop structures available to
perform iterations so that a section of programming
code can be repeated under certain conditions.

These are:
» Count-controlled loops(for a set number of
iterations)

» Pre-condition loops – may have no iterations

» Post-condition loops – always has at least one


iteration.
 Count-controlled loops FOR loops are used when a set number of iterations are
required. Look at some of the different types of FOR statements with a counter
starting at one, finishing at ten and increments by two for every iteration.
Condition-controlled loops When the number of
iterations is not known, there are two options:

» pre-condition loops which may have no iterations

» post-condition loops which always have at least one


iteration.
Some of the different pre- and post-condition loops
Totalling and counting
String Handling
 Strings are used to store text.

 Every string contains a number of characters, from


an empty string, which has no characters stored, to
a maximum number specified by the programming
language.

 The characters in a string can be labelled by


position number.

 The first character in a string can be in position


zero or position one, depending on the language.
String handling is an important part of programming.
» 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.
» substring – extracting part of a string.
For example, the substring "Science" could be extracted
from "Computer Science".
» 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".
 These string manipulation methods are usually provided in
programming languages by library routines
Arithmetic, logical and Boolean operators
 Arithmetic operators:
All programming languages make use of arithmetic operators
to perform calculations.
Logical operators:
All programming languages make use of logical
operators to decide which path to take through a
program.
Boolean operators
All programming languages make use of Boolean
operators to decide whether an expression is true or
false.
Use of nested statements
 Selection and iteration statements can be nested
one inside the other.

 This powerful method reduces the amount of code


that needs to be written and makes it simpler for a
programmer to test their programs.
THANK YOU

You might also like