unit-8
unit-8
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.
A variable in a computer program is a named data store than contains a value that may change during the execution
of a program.
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.
It is considered good practice to declare the constants and variables to be used in that program. Some languages
require explicit declarations, which specifically state what type of data the variable or constant will hold. Other
languages require implicit declarations, where the data type is based on the value assigned.
Basic data types:
The basic data types you will need know are as given follows
» 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);
» Boolean – a variable or constant that can have only two values TRUE or FALSE
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.
Selection is a very useful technique, allowing different routes through the steps of a program. Selection was
demonstrated in pseudocode with the use of IF and CASE
IF statements:
Case statements:
Case statements are used when there are multiple choices to be made. Different programming languages provide
different types of statement to do this.
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.
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.
Look at some of the different pre- and post-condition loops used in your programming language.
String Handling
Strings are used to store text. Every string contains a number of characters. The first character in a string can be in
position zero or position one, depending on the language.
» 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".
Arithmetic, logical and Boolean operators
Arithmetic operators :all programming languages make use of arithmetic operators to perform calculations.
Logical operators
Boolean operators:
Many programming languages make use of subroutines, also known as named procedures or functions. These are
defined once and can be called many times within a program.
A procedure is a set of programming statements grouped together under a single name 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 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.
Instead of calling them procedures, different terminology is used by each programming language. Procedures are
known as:
Functions
A function is just like a procedure except it always returns a value. Just like a procedure it is defined once and can be
called many times within a program.
» void functions in Python
» subroutines in VB
» methods in Java.
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.
Library routines
Many programming language development systems include library routines that are ready to incorporate into a
program.
» MOD – returns remainder of a division
» DIV – returns the quotient (i.e. the whole number part) of a division
» ROUND – returns a value rounded to a given number of decimal places
» RANDOM – returns a random number.
Arrays:
An array is a data structure containing several elements 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.
One- and Two-dimensional arrays
The first element of an array can have an index of zero or one. However, most programming languages automatically
set the first index of an array to zero. Arrays can be one-dimensional or multi-dimensional.
One-dimensional arrays A one-dimensional array can be referred to as a list.
File handling
Purpose of storing:
Data stored in a file can thus be accessed by the same program at a later date or accessed by another program. Data
stored in a file can also be sent to be used on other computer(s).