Csc121 - Topic 3 Algorithm Design For Sequence Control Structure - PPTM
Csc121 - Topic 3 Algorithm Design For Sequence Control Structure - PPTM
7
Variables
🠶 Variables are memory locations, whose contents can vary or differ over time.
🠶 Names that refer to memory locations, that can holds 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.
▪ Most language allow both letters and digits within variable names.
▪ Different languages put different limits on the length of variable names,
although in general, newer languages allow longer names.
▪ Many modern languages, such as C++, C# and Java allow more than 200
characters in a variable name. Variable names in these languages usually
consist of
lowercase letters, don’t allow hyphens, but do allow underscores.
▪ Naming variables is case sensitive, so HOURLYWAGE, hourlywage, and
hourlyWage are considered three separate variable names.
▪ Most programmers who use the more modern language employ the format in
which multiple-word variable names are run together, and each new word within
the variable name begins with an uppercase letter (camel casing).
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.
10
Rules of naming identifier, variable
and constant
🠶 Even though every language has its own rules for naming variables, when designing the
logic of a computer program, you should not concern yourself with the specific syntax of
any particular computer language
🡪 The logic, after all, works with any language.
🠶 The names follow only two rules:
1. Names must be one word.
▪ The name can contain letters, digits, underscores, or other with the exception of
space.
2. 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.
11 Standard data types
Standard
Data Types
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
16 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
17 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
18 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 == !=
Logical AND && Lowest
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
🞂 Purpose: To read the data entered by the user and then store
the value(s) into the variable(s) mentioned.
🞂 A variable gets a value using input statement or using
assignment statement.
🞂 Output statement is usually used together with input statement
to guide the user as to what kind of data she/he has to enter.
🞂 The word read or input or get or prompt is used to implement
input statement.
Input Statement (cont.)