You are on page 1of 15

Elements of Programming

Languages
Introduction
• There are several elements which programming languages, and programs
written in them, typically contain.
• These elements are found in all programming languages, not just C++.
• If you understand these elements and what they’re for, not only will you
understand C++ better, but you will also find learning other programming
languages, and moving between different programming languages, much
easier.
• We will look at the following elements in turn
• Character set
• Constants
• Data types
• Variables and
• Operators
Character set
• A character set is a well-defined list of characters and symbols that a
programming language can understand and use.
• The common character set used in most programming languages includes;
A – Z, a – z, 0 – 9, and special symbols, “+, -, /, *, &, \, #, $, ?, (), [], {}, %,
^, !, ||, @, <, >” etc.
• It should be noted that the character set of one programming language
may differ from another.
Constant
• In mathematics, a constant is a specific number or a symbol that is
assigned a fixed value.
• For example, in the equation below, “x” and “y” are variables, while the
numbers 2 and 3 are constants. y = 2x – 3
• Constants are also used in computer programming to store fixed values.
• They are typically declared at the top of the source code file or at the
beginning of a function.
• Constants are useful for defining values that are used many times within a
function or program. Example; const int min = 10;
• By using constants, programmers can modify multiple instances of a value
at one time.
Data types
• All programs are composed of two items: Data and Operations on that
Data.
• During programming, we must know what is to be stored in the
computer’s memory, whether it is a simple number, a letter or a very large
number.
• A data type is a classification of the type of data that a variable or an
object can hold in computer programming.
• Data type is therefore an important factor in virtually all computer
programming languages.
• As its name indicates, it represents the type of the data which you can
process using your computer program.
Data types Cont’
• The most common data types include Boolean, integer, float, double, character,
and string.
• Boolean denoted by bool in the C++ language, is the data type that holds a boolean
value of true or false. An integer value 1 represents true and 0 represents false.
• Integer denoted by int in the C++ language, is the data type that holds an integer value
or a whole number.
• float is the data type that holds a single–precision floating point value or a real number.
• double is the data type that holds a double–precision floating point value or a real
number.
• Character is denoted by char, is the data type that holds an integral value corresponding
to the representation of an element of the ASCII character set. Char holds exactly one
character such as ‘a’ or ‘A’.
• string is a data type which holds a sequence of characters such as “abc”. It is typically
used to represent words and text. However, it is possible to have a numeric string, such
as “1234” or alphanumeric such as “abc123”
Data types Cont’
Table1: Data types and their sizes in memory
Type Size (Bytes) Range

bool 1 Logical value T/F

int 2 -32768 to 32767

char 1 -128 to 127 or 0 to 255

float 4 1.2E-38 to 3.4E+38 till 6


decimal places

double 8 +/- 1.7e +/-308 (~15 digits)


Variable
• Variable is a location in the computer memory which can store data and is
given a symbolic name for easy reference.
• Variables may be declared global or public (i.e. accessible anywhere in the
program) or local or private (i.e. private to certain parts of the program)
depending on the particular programming language under used.
• All variables in any programming language must first be declared before they
can be used in the program.
• Variables can also be initialized, meaning you can give a value to a variable
which can change later as the program executes.
Variable Cont’
• Rules for naming variables
1. The first character of a variable can never be a number but must be a letter, or an
underscore (_), which is also count as a letter.
2. The blank or white space character is not permitted in naming a variable.
3. Can be of any length.
4. Reserved words/keywords and characters such as ‘int’ and # also cannot be used.
• Variable declaration: A variable declaration means to tell the compiler where
and how much to create the storage for the variable.
• data_type variable_name; // syntax for declaring a variable in C++
• Example: int sum;
• Initializing variable
• data_type variable_name = value; // Syntax in C++
• Example: float num1 = 2.45;
Operators
• Operators are the symbols for the operations performed on data in the
computer’s memory. These are categorized as follows:
• Assignment operator (=): The assignment operator assigns a value to a
variable. E.g. x = 5; x = y;
• Mathematical operators: These are used to perform simple arithmetic
operations.
Operator Description

+ Addition

- Subtraction

* Multiplication

/ Division

% Modulo
Operators Cont’
• Relational operators: Relational are used to compare expressions, asking
questions such as, “is x greater than 200?” or “is y equal to 10”. An expression
containing a relational operator evaluates as either TRUE (1) or FALSE (0).
Operator Symbol Meaning Example

Equal == Is operand 1 equal to operand 2? x==y

Greater than >  Is operand 1 greater than operand 2? x>y

Less than <  Is operand 1 less than operand 2? x<y

Greater or equal to >= Is operand 1 greater than or equal to operand 2? x >= y

Less or equal to <= Is operand 1 less than or equal to operand 2? x <= y

Not equal to != Is operand 1 not equal to operand 2? x != y


Operators Cont’
• Logical operator: Logical operators enable the programmer to combine 2 or
more relational expressions into a single expression that evaluate as either
TRUE (1) or FALSE (0).
Operator Symbol Example

AND && Expression1 && expression2

OR || expression1 || expression2

NOT ! !expression1
Operators Cont’
• NOTE:
• Logical AND (&&) returns True (1) statement if and only if both expression1 and
expression2 are true; false (0) otherwise. Example; (5 == 5) && (6 != 2) returns
true because both operands are true.
• Logical OR (||) returns True (1) statement if either expression1 or expression2
is true; false (0) only if both are FALSE. Example; (5 > 1) || (6 < 1) returns true
because one operand is true
• Logical NOT (!) returns False (0) if expression1 is true; true (1) if expression1 is
false. Example; ! (5 == 4) returns true (1) because the operand is false. !(3 < 5)
returns false (0) because the expression is true.
Operators Cont’
• Increment/Decrement operators (++/--)
• The increase operator (++) and the decrease operator (--), increase or
reduce by one the value stored in a variable.
• They are equivalent to +=1 and to -=1, respectively.
• A peculiarity of this operator is that it can be used both as a prefix and as a
suffix.
• That means that it can be written either before the variable name (++x) or
after it (x++).
Expressions
• They are constructs which denote rules of computation whereby currently-
available values are combined to compute other values by the application of
mathematical operators.
• In simple terms, expressions are any legal combination of symbols that
represents a value.
• Expressions allow a programmer to derive a desired value from existing data
by applying suitable operators to the operands (i.e., the available data).
• For example, given a person’s year of birth [Y] and the current calendar year
[CY], a programmer can compute the person’s age by subtracting Y from CY
(i.e. CY - Y).
• CY – Y is an example of an expression. x = x + 5 is also an expression.

You might also like