2. F1001 PROGRAMMING FUNDAMENTALS
CONDITIONAL STATEMENT
• After a program has received data from the users and the data is been held by a variable, the next step
is to operate the variable.
• The operation done to the variable can be one or more.
• Specific instructions used in the variable operation are called operators.
• Operators are symbols used to represent computer operations.
• These symbols have one or more special characters defined by programming language.
• The operations that can be operated on variables are:
1. Arithmetic operation.
Example: Addition, multiplication, division, subtraction
2. Assignment operation.
Example: assign or change value to variable.
3. Relational and Logical operation.
Example: Testing truth for expression (example: a > b).
ASSIGNMENT OPERATOR
In certain languages, for example in C language, equal (=) symbol is used as symbol in assignment
operations.
This operator is used to change variable’s value.
Example:
x=5
So 5 are assigned to variable x.
Example:
x=y=0
The result from the operations: variable x and variable y are assigned to the same value, which is 0.
One type of assignment operator is the compound assignment operator.
Compound assignment operator is the operator that is combined with –, +, %, *, / and other
operations.
Examples:
Ungkapan / Expression Maksud / Meaning
a += b a=a+b
a *= 5 a=a*5
a –= 2 a=a–2
a %= 3 a=a%3
60
3. F1001 PROGRAMMING FUNDAMENTALS
Example:
If a = 2, b = 4
a += b (means a = a + b)
Result: a = 6
If b = 5
b -= 2 (means b = b – 2)
Result: b = 3
Variable representative in memory:
If a = 2, b = 4 If a = 2, b = 5
a += b (means a = a + b) b –= a (means b + b – a)
Result: a = 6 Result: b = 3
Before: Before:
a 2 b 4 a 2 b 5
100 200 100 200
After: After:
a 6 b 4 a 2 b 3
100 200 100 200
ARITHMETIC OPERATOR
Arithmetic operation is the calculation operation that can be done on data in variables.
In C language, the 5 arithmetic operators are:
Symbol Operators
+ Add
– Subtract
* Multiply
/ Divide
% Modulus
Modulus operator is used to get the balance of division of 2 numbers.
Example:
5 % 3 is 2
10 % 6 is 4
Arithmetic expressions are normally made of variables with arithmetic operator.
Example:
61
4. F1001 PROGRAMMING FUNDAMENTALS
a+b/2
This expression will be evaluated based on priority defined by the programming language.
Increment and Decrement Operation
Variables usually can be increased or decreased by 1.
For example, C language and Java has provided operator to add or subtract 1 from variable values.
The symbols are:
Symbol Operation
++ Add 1
–– Subtract 1
Example:
x++ can also be written as x = x + 1
y– – can also be written as y = y – 1
However, this operation cannot be used for constants.
For example: 5++ is invalid.
Symbols can be written before or after the variable.
Example:
Statement Flow of Execution Explanation
a = 10; a = 10; Variable b will be assigned with value a=10,
b = a++; b = a; then the value of a will be added to 1
a = a + 1;
a = 10; a = 10; Variable b will be assigned with value a=11,
b = ++a; a = a + 1; after the value of a had been added to 1
b = a;
62
5. F1001 PROGRAMMING FUNDAMENTALS
Variable representative in memory:
Assume:
a = 10;
b = a++;
10
A B
Before Operation
11 10
A B
After Operation
Variable b is assigned with value a = 10, then value of a will be increased by 1.
63
6. F1001 PROGRAMMING FUNDAMENTALS
Relational Operator
Relational operator can be divided into 2 groups:
1. Not Equivalent Group
- Not equal is an expression that consists relational operator that will return the value of 1, if
the relation is true and 0 if it is false.
- Symbols:
Symbol Operators
> Greater than
>= Greater than or equals to
< Less than
<= Less than or equals to
- Example:
Variable c will have 1 as value
a = 10, b = 2;
because relation (10 > 2) is true,
c = a > b;
while variable d will have 0 because
d = (b * 3) > a;
relation (6 < 0) is false.
2. Equivalent Group
- Equivalent is an expression that consists of relational operator that will return 1 if the relation
is true and 0 if false.
- Symbols:
Symbol Operators
== Equal to
!= Not equal to
- Example:
Variable c will have 1 as value
a = 10;
because relation (10! = 2) is true, and
b = 2;
variable d will have 1 as value
c = a != b;
because relation (2 * 5 == 10) is also
d = (b * 5) == a;
true.
64
7. F1001 PROGRAMMING FUNDAMENTALS
Logical Operator
In certain languages, for example C language, it has 3 types of logical operators:
Symbol Operators
&& AND
|| OR
! NOT
Table below shows the definition of C languages for logical operator:
P Q P&&Q P||Q !P !Q
0 0 0 0 1 1
0 NOT 0 0 1 1 0
NOT 0 0 0 1 0 1
NOT 0 NOT 0 1 1 0 0
Example 1:
If x = 2 and y = 3
Ungkapan / Expression Nilai a / Value a
a = x && y 1
a = (x > 0) && (y > 0) 1
a = (x < y) && (y == 0) 0
a = x || y 1
a = (x !0) || (y != 0) 1
a = (x == y) || (y == 0) 0
a = ! (x == y) 1
a = ! (x < y) 0
Example 2:
If a = 4, b = 6, c = 8, d = 4
Ungkapan / Expression Nilai x / Value x
x = a == d 1
x = a == b 0
x=b<c 1
x=b>c 0
x=c>a 1
x=d<a 0
x = b >= a 1
x=d>a 0
65
8. F1001 PROGRAMMING FUNDAMENTALS
x = d <= b 1
x = a != d 0
x = b != c 1
x = c <= b 0
OPERATOR COMPOUND
All operators (for example in C language) can be combined into one expression.
Notes below shows the priorities set for all operators, that has been discussed before:
Operations
() Highest priority
! ++ ––
* / %
+ –
< <= > >=
== !=
&&
||
= Lowest priority
Example 1:
Expression x + y == z && m – n will be defined as below:
( x + y ) == z && m – n
( x + y ) == z && ( m – n )
( ( x + y ) == z ) && ( m – n )
( ( ( x + y ) == z ) && ( m – n ) )
+ operation gets highest priority in the expression. Then, followed by – operation
and then == operation. The last operation done is && because it has the lowest
priority.
Example 2:
66
9. F1001 PROGRAMMING FUNDAMENTALS
4 + 2 * 3 – (5 / 2)
4 + 2 * 3 – (2.5)
4 + 6 – (2.5)
10 – 2.5
7.5
Example 3:
5*2+5%2
10 + 5 % 2
10 + 1
11
Example 4:
10 / 5 + (9 / 3)
10 / 5 + 3
2+3
5
CAST OPERATOR
Cast operator is an operator used to change the types of expression result.
For example, to change integer type to float type.
Example in C Language:
int i = 9;
float f;
f = i / 2;
From example, variable f will get 4.0. This happened because variable i is integer type, so dividing the
integer number will cause the decimal part to be cut out of the answer.
To get an accurate answer, the syntax must be changed to:
int i = 9;
float f;
f = (float) i / 2;
Therefore, the result 4.5 will be assigned to variable f.
TRACING VARIABLE’S VALUE IN MEMORY
67
10. F1001 PROGRAMMING FUNDAMENTALS
• Each data entered by the users for a variable is stored in memory.
• Memory is the processing area for Central Processing Unit (CPU).
• It is a small sequential location storing cells where each of them has a single byte of information.
• The information may consist program instructions, arithmetic numbers, code that represents text
character, variable, picture or other data.
• Memory location is like a letterbox. Each letterbox represents an address and holds 1 byte of information.
• Each byte has its own address to allow it to be recognised by the CPU.
MEMORY ILLUSTRATION
• Example:
Assume that user stores data Aminah in variable Nama.
Computer will allocate a special
location for variable Name when
it is declared by the user. Data
Aminah will be stored to the
variable Name at location 3 in
memory.
• Let’s assume that user wants to access a data in variable Town at location 37 in memory:
68
11. F1001 PROGRAMMING FUNDAMENTALS
When the user written an
instruction to access the value in
variable Town, CPU will detect
the memory location according
to the address of the variable.
When the variable is found, the
value will be accessed and
displayed on the user’s screen.
69