Unit 4

13
F1001 PROGRAMMING FUNDAMENTALS PROGRAMMING OPERATORS Assignment Operator Mathematical Operators Relational Operators Logical Operators 59 UNIT 4

Transcript of Unit 4

Page 1: Unit 4

F1001 PROGRAMMING FUNDAMENTALS

PROGRAMMING OPERATORS

Assignment Operator

Mathematical Operators

Relational Operators

Logical Operators

59

UNIT

4

Page 2: Unit 4

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 *= 5

a –= 2

a %= 3

a = a + b

a = a * 5

a = a – 2

a = a % 3

60

Page 3: Unit 4

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:

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.

61

If a = 2, b = 4

a += b (means a = a + b)

Result: a = 6

Before:

After:

a

100

2

200

b 4

200

b 4

100

a 6

If a = 2, b = 5

b –= a (means b + b – a)

Result: b = 3

Before:

After:

a

100

2

200

b 5

200

b 3

100

a 2

Page 4: Unit 4

F1001 PROGRAMMING FUNDAMENTALS

Example:

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;

b = a++;

a = 10;

b = a;

a = a + 1;

Variable b will be assigned with value a=10,

then the value of a will be added to 1

a = 10;

b = ++a;

a = 10;

a = a + 1;

b = a;

Variable b will be assigned with value a=11,

after the value of a had been added to 1

62

Page 5: Unit 4

F1001 PROGRAMMING FUNDAMENTALS

Variable representative in memory:

63

A

10

Variable b is assigned with value a = 10, then value of a will be increased by 1.

Assume:

a = 10;

b = a++;

Before Operation

B

A

11

B

10

After Operation

Page 6: Unit 4

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:

a = 10, b = 2;

c = a > b;

d = (b * 3) > a;

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:

a = 10;

b = 2;

c = a != b;

d = (b * 5) == a;

64

Variable c will have 1 as value

because relation (10 > 2) is true,

while variable d will have 0 because

relation (6 < 0) is false.

Variable c will have 1 as value

because relation (10! = 2) is true, and

variable d will have 1 as value

because relation (2 * 5 == 10) is also

true.

Page 7: Unit 4

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

a = (x > 0) && (y > 0)

a = (x < y) && (y == 0)

a = x || y

a = (x !0) || (y != 0)

a = (x == y) || (y == 0)

a = ! (x == y)

a = ! (x < y)

1

1

0

1

1

0

1

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

Page 8: Unit 4

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 ) )

66

+ 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.

Page 9: Unit 4

F1001 PROGRAMMING FUNDAMENTALS

Example 2:

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.

67

Page 10: Unit 4

F1001 PROGRAMMING FUNDAMENTALS

TRACING VARIABLE’S VALUE IN MEMORY

68

Example:

Assume that user stores data Aminah in variable Nama.

MEMORY ILLUSTRATION

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.

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.

Page 11: Unit 4

F1001 PROGRAMMING FUNDAMENTALS

Let’s assume that user wants to access a data in variable Town at location 37 in memory:

69

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.