Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "KUKUM Sem1-05/06EKT120: Computer Programming1 Week 2."— Presentation transcript:

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

2 KUKUM Sem1-05/06EKT120: Computer Programming2 Outline Identifiers and reserve words Program comments Preprocessor directives Data types and type declarations Operators Formatted input and output Program debugging

3 KUKUM Sem1-05/06EKT120: Computer Programming3 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

4 KUKUM Sem1-05/06EKT120: Computer Programming4 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

5 KUKUM Sem1-05/06EKT120: Computer Programming5 Types of Operator Mathematical operators Addition (+) Subtraction (-) Multiplication (*) Division (/) Integer division (\) Modulo division (MOD) Powers (^) Functions

6 KUKUM Sem1-05/06EKT120: Computer Programming6 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.

7 KUKUM Sem1-05/06EKT120: Computer Programming7 Types of Operators Logical Operator OR AND Not Use to connect relational expression (decision-making expression) To perform operation on logical data

8 KUKUM Sem1-05/06EKT120: Computer Programming8 Example of Operator OperatorComputer SymbolExample MathematicalOperationResultant Addition+3.0 + 5.28.2 Subtraction-7.5 – 4.03.5 Multiplication*8.0 * 5.040.0 Division/9.0/4.02.25 Integer Division\9\42 Modulo divisionMOD9 MOD 41 Power^3^29 Relational Equal To=5=7FALSE Less Than<5<7TRUE Greater Than>5>7TRUE Less than or equal to<=5< = 7FALSE Greater than or equal to >=5>=7FALSE Not Equal to<>5<>7TRUE Logical NotNOTNOT TRUEFALSE AndANDTRUE AND TRUETRUE OrORTRUE OR FALSETRUE

9 KUKUM Sem1-05/06EKT120: Computer Programming9 Operator Hierarchy (Precedence) Order of OperationOperand Data TypeResultant Data Type ( ) Reorders the hierarchy; all operations are completed within the parentheses using the same hierarchy 1. Functions Mathematical Operators 2. PowerNumerical 3. \, MODNumerical 4. *, /Numerical 5. +, -Numerical Relational Operators 6. =,, =, <>Numerical, string, character Logical Logical Operator 7. NOTLogical 8. ANDLogical 9. ORLogical

10 KUKUM Sem1-05/06EKT120: Computer Programming10 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

11 KUKUM Sem1-05/06EKT120: Computer Programming11 Normal equation Appropriate Computer Equation X* (3*Y+4)-4*Y/(X+6) Setting up Numerical Expression X+6 X(3Y+4) - 4Y

12 KUKUM Sem1-05/06EKT120: Computer Programming12 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) 12 3 4 5 6 OperationResultant 1. X+Y5 2. Z+612 3. 5 * resultant of 125 4. 4 * Y12 5. Resultant of 4 / resultant of 21 6. Resultant of 3 – Resultant of 524

13 KUKUM Sem1-05/06EKT120: Computer Programming13 Evaluation Relational Expression Expression 12 3 4 OperationResultant 1. A < BFALSE 2. C OR DTRUE 3. NOT the resultant of 1TRUE 4. Resultant of 3 AND Resultant of 2TRUE NOT (A < B) AND (C OR D) A = 4, B=2 C = TRUE D = FALSE

14 KUKUM Sem1-05/06EKT120: Computer Programming14 Getting Started With C //Program name : program1.c //Programmer : Salina //This program Print Welcom to KUKUM //Calculate A = B * K; //Print AnswerA #include 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 declaration begin end Return 0(int) to OS body Comments Preprocessor directives

15 KUKUM Sem1-05/06EKT120: Computer Programming15 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.

16 KUKUM Sem1-05/06EKT120: Computer Programming16 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 /* /* */*/

17 KUKUM Sem1-05/06EKT120: Computer Programming17 Preprocessor directives An instruction to pre-processor Standard library header (p154,Deitel) E.g. #include for std input/output #include Conversion number-text vise-versa, memory allocation, random numbers #include string processing

18 KUKUM Sem1-05/06EKT120: Computer Programming18 Data Types & Mem. Alloc. Data TypeDescription Size (bytes) char A single character. Internally stored as a coded integer value (refer to ASCII table ). 1 int Integer quantity. Can be represented in signed or unsigned form (with the unsigned keyword). 4 float Floating-point number. Set of real numbers.4 double A more precise version of float. Has larger dynamic range and better representation of decimal points. 8 bool Boolean representation of logic states. Can only be assigned true (1) or false (0). 1

19 KUKUM Sem1-05/06EKT120: Computer Programming19 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

20 KUKUM Sem1-05/06EKT120: Computer Programming20 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

21 KUKUM Sem1-05/06EKT120: Computer Programming21 Arithmetic Operators Used to execute mathematical equations The result is usually assigned to a data storage (instance/variable) using assignment operator ( = )

22 KUKUM Sem1-05/06EKT120: Computer Programming22 C Operators OperatorC Language SymbolExample MathematicalOperationResultant Addition+3.0 + 5.28.2 Subtraction-7.5 – 4.03.5 Multiplication*8.0 * 5.040.0 Division/9.0/4.02.25 Integer Division (Quotion\9\42 Modulo division%9 % 41 Power^3^29 Relational Equal To==5==7FALSE Less Than<5<7TRUE Greater Than>5>7TRUE Less than or equal to<=5< = 7FALSE Greater than or equal to>=5>=7FALSE Not Equal to!=5!=7TRUE Logical Not!NOT TRUEFALSE And&&TRUE AND TRUETRUE Or||TRUE OR FALSETRUE

23 KUKUM Sem1-05/06EKT120: Computer Programming23 Exercise on arithmetic operators Given x = 20, y = 3 z = x % y = 20 % 3 = 2 (remainder)

24 KUKUM Sem1-05/06EKT120: Computer Programming24 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

25 KUKUM Sem1-05/06EKT120: Computer Programming25 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.)

26 KUKUM Sem1-05/06EKT120: Computer Programming26 More on logical operators Logical operators are manipulation of logic e.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))

27 KUKUM Sem1-05/06EKT120: Computer Programming27 Truth table for && (logical AND) operator exp1exp2exp1 && exp2 false truefalse truefalse true

28 KUKUM Sem1-05/06EKT120: Computer Programming28 Truth table for || (logical OR) operator exp1exp2exp1 || exp2 false true falsetrue

29 KUKUM Sem1-05/06EKT120: Computer Programming29 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;

30 KUKUM Sem1-05/06EKT120: Computer Programming30 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

31 KUKUM Sem1-05/06EKT120: Computer Programming31 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: 4 val=5; cout<<val++; Output: 5 val=5; cout<<val--; Output: 5

32 KUKUM Sem1-05/06EKT120: Computer Programming32 Operator Precedence & symbol in C language Order of OperationSymbol 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||

33 KUKUM Sem1-05/06EKT120: Computer Programming33 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 declaration int a, b;//Line 1 char grade;//Line 2 //examples of input and output statements a = 25; grade = ‘A’;//Line3 printf(“a = ”,a) ; //Line 4 printf(“Enter two integers: ”) ;//Line 5 scanf (“%d%d”,&a,&b) ; printf(“The numbers you entered are %d %d”,a,b);//Line 6 printf(“Your grade is %c “,grade); //Line 10

34 KUKUM Sem1-05/06EKT120: Computer Programming34 Formatted Output with printf-cont

35 KUKUM Sem1-05/06EKT120: Computer Programming35 End Week 1 – Session 2 Q & A!


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

Similar presentations


Ads by Google