+ All Categories
Home > Documents > KUKUM Sem1-05/06EKT120: Computer Programming1 Week 2.

KUKUM Sem1-05/06EKT120: Computer Programming1 Week 2.

Date post: 17-Jan-2016
Category:
Upload: quentin-booth
View: 216 times
Download: 0 times
Share this document with a friend
Popular Tags:
35
KUKUM Sem1-05/06 EKT120: Computer Programm ing 1 Week 2
Transcript
Page 1: KUKUM Sem1-05/06EKT120: Computer Programming1 Week 2.

KUKUM Sem1-05/06

EKT120: Computer Programming

1

Week 2

Page 2: KUKUM Sem1-05/06EKT120: Computer Programming1 Week 2.

KUKUM Sem1-05/06

EKT120: Computer Programming

2

Outline Identifiers and reserve words Program comments Preprocessor directives Data types and type declarations Operators Formatted input and output Program debugging

Page 3: KUKUM Sem1-05/06EKT120: Computer Programming1 Week 2.

KUKUM Sem1-05/06

EKT120: Computer Programming

3

Operators Operators tell the computer how to

process the data. It connects data in the expression

and equation Types of operators used in

calculation and problem solving include Mathematical Relational Logical

Page 4: KUKUM Sem1-05/06EKT120: Computer Programming1 Week 2.

KUKUM Sem1-05/06

EKT120: Computer Programming

4

Operators continue…. Two concepts related to operator are

Operand Resultant

Operands are the data being process Resultant is the answer that result when the

operation is complete. Example 5 + 7 = 12 Operands ara 5 and 7 Operator is + Resultant is 12

Operands can be constant or variable Data types of resultant and operands will

depend on the operator

Page 5: KUKUM Sem1-05/06EKT120: Computer Programming1 Week 2.

KUKUM Sem1-05/06

EKT120: Computer Programming

5

Types of Operator Mathematical operators

Addition (+) Subtraction (-) Multiplication (*) Division (/) Integer division (\) Modulo division (MOD) Powers (^) Functions

Page 6: KUKUM Sem1-05/06EKT120: Computer Programming1 Week 2.

KUKUM Sem1-05/06

EKT120: Computer Programming

6

Types of Operator Relational operators

Equal to Less than Greater than Less than or Equal to

The resultant of relational operation is logical data type i.e. TRUE or FALSE

The next set of actions will depend on the result of the relational expression

E.g. Balance > 500, if the expression TRUE withdraw 100, if the expression FALSE withdraw 50.

Page 7: KUKUM Sem1-05/06EKT120: Computer Programming1 Week 2.

KUKUM Sem1-05/06

EKT120: Computer Programming

7

Types of Operators Logical Operator

OR AND Not

Use to connect relational expression (decision-making expression)

To perform operation on logical data

Page 8: KUKUM Sem1-05/06EKT120: Computer Programming1 Week 2.

KUKUM Sem1-05/06

EKT120: Computer Programming

8

Example of OperatorOperator Computer Symbol Example

Mathematical Operation Resultant

Addition + 3.0 + 5.2 8.2

Subtraction - 7.5 – 4.0 3.5

Multiplication * 8.0 * 5.0 40.0

Division / 9.0/4.0 2.25

Integer Division \ 9\4 2

Modulo division MOD 9 MOD 4 1

Power ^ 3^2 9

Relational

Equal To = 5=7 FALSE

Less Than < 5<7 TRUE

Greater Than > 5>7 TRUE

Less than or equal to

<= 5< = 7 FALSE

Greater than or equal to

>= 5>=7 FALSE

Not Equal to <> 5<>7 TRUE

Logical

Not NOT NOT TRUE FALSE

And AND TRUE AND TRUE TRUE

Or OR TRUE OR FALSE TRUE

Page 9: KUKUM Sem1-05/06EKT120: Computer Programming1 Week 2.

KUKUM Sem1-05/06

EKT120: Computer Programming

9

Operator Hierarchy (Precedence)Order of Operation Operand Data Type Resultant Data Type

( ) Reorders the hierarchy; all operations are completed within the parentheses using the same hierarchy

1. Functions

Mathematical Operators

2. Power Numerical Numerical

3. \, MOD Numerical Numerical

4. *, / Numerical Numerical

5. +, - Numerical Numerical

Relational Operators

6. =, <, >, <=, >=, <> Numerical, string, character

Logical

Logical Operator

7. NOT Logical Logical

8. AND Logical Logical

9. OR Logical Logical

Page 10: KUKUM Sem1-05/06EKT120: Computer Programming1 Week 2.

KUKUM Sem1-05/06

EKT120: Computer Programming

10

Expression & Equation Expression and Equation make up the

instructions in the solution to a computer problem

Expression processes data (the operand) through the operators. E.g. LENGTH * WIDTH

Equation stores the resultant of an expression in a memory location in the computer through the (=) sign (assignment symbol) E.g. AREA = LENGTH * WIDTH The resultant of expression LENGTH * WIDTH will be stored

in a location called AREA

Page 11: KUKUM Sem1-05/06EKT120: Computer Programming1 Week 2.

KUKUM Sem1-05/06

EKT120: Computer Programming

11

Normal equation

Appropriate Computer Equation X* (3*Y+4)-4*Y/(X+6)

Setting up Numerical Expression

X+6X(3Y+4) - 4Y

Page 12: KUKUM Sem1-05/06EKT120: Computer Programming1 Week 2.

KUKUM Sem1-05/06

EKT120: Computer Programming

12

Evaluating Mathematical Expression Consider

5*(X+Y)-4*Y/(Z+6) Value of X = 2, Y=3,

Z=6 Evaliation

5 * (X + Y) – 4 * Y / (Z+6)

1 23 4

56

Operation Resultant

1. X+Y 5

2. Z+6 12

3. 5 * resultant of 1 25

4. 4 * Y 12

5. Resultant of 4 / resultant of 2 1

6. Resultant of 3 – Resultant of 5 24

Page 13: KUKUM Sem1-05/06EKT120: Computer Programming1 Week 2.

KUKUM Sem1-05/06

EKT120: Computer Programming

13

Evaluation Relational Expression Expression

1 23

4

Operation Resultant

1. A < B FALSE

2. C OR D TRUE

3. NOT the resultant of 1 TRUE

4. Resultant of 3 AND Resultant of 2

TRUE

NOT (A < B) AND (C OR D) A = 4, B=2

C = TRUED = FALSE

Page 14: KUKUM Sem1-05/06EKT120: Computer Programming1 Week 2.

KUKUM Sem1-05/06

EKT120: Computer Programming

14

Getting Started With C//Program name : program1.c//Programmer : Salina//This program Print Welcom to KUKUM//Calculate A = B * K;//Print AnswerA

#include <stdio.h>int main(void){

float A, B;const float K=0.05;

printf(“Welcome To KUKUM”); B = 400;

A = B * K;printf(“\nValue of A : %5.2f”, A);return 0;

}

The terms void indicates we receive nothing from OS. int at the beginning return an integer

to OS

Variable &

constant declaratio

n

begin

end

Return 0(int) to OS

body

Comments

Preprocessor directives

Page 15: KUKUM Sem1-05/06EKT120: Computer Programming1 Week 2.

KUKUM Sem1-05/06

EKT120: Computer Programming

15

Identifiers & Reserve Words Identifiers

labels for program elements, such as variable and constant name

case sensitive can consists of capital letters[A..Z], small

letters[a..z], digit[0..9], and underscore character _ First character MUST be a letter or an underscore No blanks Reserve words cannot be used as identifiers

Reserve words already assigned to a pre-defined meaning eg: delete, int, main, include, double, for, if, float etc.

(Pleased refer text book) We cannot use the reserve word as our variable.

Page 16: KUKUM Sem1-05/06EKT120: Computer Programming1 Week 2.

KUKUM Sem1-05/06

EKT120: Computer Programming

16

Program comments Starts with /* and terminate with

*/ OR Character // start a line comment,

if several lines, each line must begin with //

Comments cannot be nested /* /* */*/

Page 17: KUKUM Sem1-05/06EKT120: Computer Programming1 Week 2.

KUKUM Sem1-05/06

EKT120: Computer Programming

17

Preprocessor directives An instruction to pre-processor Standard library header (p154,Deitel) E.g. #include <stdio.h>

for std input/output #include <stdlib.h>

Conversion number-text vise-versa, memory allocation, random numbers

#include <string.h> string processing

Page 18: KUKUM Sem1-05/06EKT120: Computer Programming1 Week 2.

KUKUM Sem1-05/06

EKT120: Computer Programming

18

Data Types & Mem. Alloc.

Data Type

DescriptionSize (byte

s)

charA single character. Internally stored as a coded integer value (refer to ASCII table).

1

intInteger quantity. Can be represented in signed or unsigned form (with the unsigned keyword).

4

float Floating-point number. Set of real numbers. 4

doubleA more precise version of float. Has larger dynamic range and better representation of decimal points.

8

boolBoolean representation of logic states. Can only be assigned true (1) or false (0).

1

Page 19: KUKUM Sem1-05/06EKT120: Computer Programming1 Week 2.

KUKUM Sem1-05/06

EKT120: Computer Programming

19

Type declarations Before we can use the variables in

the program, we must declare them first

float income;float net_income;

int index =0, count =0; char ch=‘a’, ch2; const float epf = 0.1, tax = 0.05;

float income, net_income;

Declare and initialize

Named constant declared and

initialized

Page 20: KUKUM Sem1-05/06EKT120: Computer Programming1 Week 2.

KUKUM Sem1-05/06

EKT120: Computer Programming

20

Types of operators Types of operators are:

Arithmetic operators (+ , - , * , / , %)

Relational operators (> , < , == , >= , <=, !=)

Logical operators (&& , ||) Compound assignment operator

(+=, -=, *=, /=, %=) Binary operators: needs two operands Unary operators: single operand Bitwise operators: executes on bit level

Page 21: KUKUM Sem1-05/06EKT120: Computer Programming1 Week 2.

KUKUM Sem1-05/06

EKT120: Computer Programming

21

Arithmetic Operators Used to execute mathematical

equations The result is usually assigned to a

data storage (instance/variable) using assignment operator ( = )

Page 22: KUKUM Sem1-05/06EKT120: Computer Programming1 Week 2.

KUKUM Sem1-05/06

EKT120: Computer Programming

22

C OperatorsOperator C Language Symbol Example

Mathematical Operation Resultant

Addition + 3.0 + 5.2 8.2

Subtraction - 7.5 – 4.0 3.5

Multiplication * 8.0 * 5.0 40.0

Division / 9.0/4.0 2.25

Integer Division (Quotion \ 9\4 2

Modulo division % 9 % 4 1

Power ^ 3^2 9

Relational

Equal To == 5==7 FALSE

Less Than < 5<7 TRUE

Greater Than > 5>7 TRUE

Less than or equal to <= 5< = 7 FALSE

Greater than or equal to >= 5>=7 FALSE

Not Equal to != 5!=7 TRUE

Logical

Not ! NOT TRUE FALSE

And && TRUE AND TRUE TRUE

Or || TRUE OR FALSE TRUE

Page 23: KUKUM Sem1-05/06EKT120: Computer Programming1 Week 2.

KUKUM Sem1-05/06

EKT120: Computer Programming

23

Exercise on arithmetic operators Given x = 20, y = 3 z = x % y = 20 % 3

= 2 (remainder)

Page 24: KUKUM Sem1-05/06EKT120: Computer Programming1 Week 2.

KUKUM Sem1-05/06

EKT120: Computer Programming

24

Relational and Logical Operators Previously, relational operator: >, < >=, <=, == , != Previously, logical operator:

&&, || Used to control the flow of a

program Usually used as conditions in

loops and branches

Page 25: KUKUM Sem1-05/06EKT120: Computer Programming1 Week 2.

KUKUM Sem1-05/06

EKT120: Computer Programming

25

More on relational operators Relational operators use

mathematical comparison (operation) on two data, but gives logical output

e.g1 let say b = 8, if (b > 10) e.g2 while (b != 10) e.g3 if(kod == 1)

print(“Pegawai”); Reminder: Don’t confuse == (relational op.) with = (assignment op.)

Page 26: KUKUM Sem1-05/06EKT120: Computer Programming1 Week 2.

KUKUM Sem1-05/06

EKT120: Computer Programming

26

More on logical operators Logical operators are manipulation

of logice.g1 let say b=8, c=10,

if ((b > 10) && (c<10))e.g2 while ((b==8) ||(c > 10))e.g3 if ((kod == 1) && (salary > 2213))

Page 27: KUKUM Sem1-05/06EKT120: Computer Programming1 Week 2.

KUKUM Sem1-05/06

EKT120: Computer Programming

27

Truth table for &&(logical AND) operator

exp1 exp2 exp1 && exp2

false false false

false true false

true false false

true true true

Page 28: KUKUM Sem1-05/06EKT120: Computer Programming1 Week 2.

KUKUM Sem1-05/06

EKT120: Computer Programming

28

Truth table for ||(logical OR) operator

exp1 exp2 exp1 || exp2

false false false

false true true

true false true

true true true

Page 29: KUKUM Sem1-05/06EKT120: Computer Programming1 Week 2.

KUKUM Sem1-05/06

EKT120: Computer Programming

29

Compound assignment operator To calculate value from expression and

store it in variable, we use assignment operator (=)

Compound assignment operator combine binary operator with assignment operator

E.g. val +=one; is equivalent to val = val + one; E.g. count = count -1; is equivalent to

count -=1;count--;--count;

Page 30: KUKUM Sem1-05/06EKT120: Computer Programming1 Week 2.

KUKUM Sem1-05/06

EKT120: Computer Programming

30

Unary Operators Obviously operating on ONE

operand Commonly used unary operators

Increment/decrement { ++ , -- } Arithmetic Negation { - } Logical Negation { ! }

Usually using prefix notation Increment/decrement can be both

a prefix and postfix

Page 31: KUKUM Sem1-05/06EKT120: Computer Programming1 Week 2.

KUKUM Sem1-05/06

EKT120: Computer Programming

31

Unary Operators (Eg.) Increment/decrement { ++ , -- }

prefix:value incr/decr before used in expression

postfix:value incr/decr after used in expression

Logical Negation { ! } bool isDinnerTime = true; bool isLunchTime = !isDinnerTime;

val=5;

cout<<++val;

Output:

6

val=5;

cout<<--val;

Output:

4val=5;

cout<<val++;

Output:

5

val=5;

cout<<val--;

Output:

5

Page 32: KUKUM Sem1-05/06EKT120: Computer Programming1 Week 2.

KUKUM Sem1-05/06

EKT120: Computer Programming

32

Operator Precedence & symbol in C languageOrder of Operation Symbol in C

( ) Reorders the hierarchy; all operations are completed within the parentheses using the same hierarchy

1. Functions

Mathematical Operators

2. Power ^

3.Integer division, modulo \, %

4. Multiplication, division *, /

5. Addition, substraction +, -

6. equal, less than, greater than, less than equal, greater than equal, not equal

==, <, >, <=, >=, !=

Logical Operator

7. NOT !

8. AND &&

9. OR ||

Page 33: KUKUM Sem1-05/06EKT120: Computer Programming1 Week 2.

KUKUM Sem1-05/06

EKT120: Computer Programming

33

Formatted Output with printf To display result of the program

can be done by using keyword printf and operator as shown below:

printf(“formating”,variable) ;//this is variable declarationint a, b; //Line 1char grade; //Line 2//examples of input and output statements a = 25; grade = ‘A’;//Line3printf(“a = ”,a) ; //Line 4printf(“Enter two integers: ”) ;//Line 5scanf (“%d%d”,&a,&b) ;printf(“The numbers you entered are %d %d”,a,b); //Line 6printf(“Your grade is %c “,grade); //Line 10

Page 34: KUKUM Sem1-05/06EKT120: Computer Programming1 Week 2.

KUKUM Sem1-05/06

EKT120: Computer Programming

34

Formatted Output with printf-cont

Page 35: KUKUM Sem1-05/06EKT120: Computer Programming1 Week 2.

KUKUM Sem1-05/06

EKT120: Computer Programming

35

End Week 1 – Session 2

Q & A!


Recommended