Csc121- Topic 3 Algorithm Design for Sequence Control Structure (1)
Csc121- Topic 3 Algorithm Design for Sequence Control Structure (1)
8
Variables
Variables are memory locations, whose contents can
vary or differ over time.
Names that refer to memory locations, that can hold
values
It is the ability of memory variable to change in value
that makes computers and programming worthwhile.
Because one memory location can be used over and
over again with different values, you can write
program instructions once and then use them for
thousands of separate calculations.
Every computer programming language has its own
set of rules for naming variables.
Constant
Constant are memory location, whose
content is not allowed to change during
program execution.
Holds data that remains the same as the
program runs.
Allow us to give a name to a value that is
used several times in a program.
Once the variable is declared constant, the
value cannot be changed and the variable
cannot be assigned to another value.
11
Rules of naming identifier, variable
and constant
The names follow only two rules:
Names must be one word.
The name can contain letters, digits, underscores, or other with the exception of
space.
Names should have some appropriate meaning.
This is not a rule of any programming language.
As long as the correct numeric result is placed in the variable, its actual name doesn’t
really matter.
However, it’s much easier to follow the logic of a program if you use appropriate
meaning of variable name.
You might think that you will remember how you intended to use a cryptic variable
name within a program, but six years later when a program requires changes, you and
other programmers working with you, will appreciate clear as well as descriptive
variable names.
12 Standard data types
Standard
Data Types
Character (char) ▪ A character is any value that can be represented in the computer’s
alphabet.
▪ Most computers use ASCII alphabet.
▪ Most of the personal, mini-, and mainframe computers use one
byte to store the char data types.
(Note: A byte is 8 bits. With 8 bits, there are 256 different values in
the char set)
14 … Standard data types
Data type Description
Operators
Operat Operation
or
+ Addition
- Subtraction Integral data type
* Multiplication
/ Division
Floating-point data type
% Modulus (Remainder)
-- Decrement by 1
Integral data type
++ Increment by 1
17 Relational operator
To compare the values of two operands.
The evaluation result is either TRUE (1) or FALSE (0).
The relational operators:
Operator Operation
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
== Equal to
!= Not equal to
18 Logical operator
When there is more than one relational expression at a
time, logical operator are used to perform the evaluation.
The evaluation result is either TRUE (1) or FALSE (0).
The logical operators:
Operator Operation
&& AND
|| OR
! NOT
19
OPERATOR PRECEDENCE
When more than one arithmetic operator is used in an
expression, C++ uses the operator precedence rules to
evaluate the expression.
Operator Category Operator
(evaluated from left to
right) Highest
Parentheses ()
Multiply, Division, Modulus * / %
Add, Subtract + -
Relational Operators < <= >
>=
Equality Operators == !=
Lowest
Logical AND &&
Logical OR ||
The multiplication, division, and modulus operators are evaluated
before addition and subtraction operators. Operators having the
same level of precedence are evaluated from left to right.
Grouping is allowed for clarity.
Simple Statements
Simple Statements