Professional Documents
Culture Documents
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
+ 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
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.