Download presentation
Presentation is loading. Please wait.
Published byDulcie Hunt Modified over 9 years ago
1
UNIMAP Sem2-07/08EKT120: Computer Programming1 Week 2 – Introduction to Programming
2
UNIMAP Sem2-07/08EKT120: Computer Programming2 Outline Pseudocode & flowchart Sample programming question Sample C program Identifiers and reserve words Program comments Preprocessor directives Data types and type declarations Operators Formatted input and output Program debugging
3
UNIMAP Sem2-07/08EKT120: Computer Programming3 Sample Programming Question Write a program that calculate nett income.Your program should read income from user. Given tax rate is 10% and epf rate is 5% from income. Steps: Analyze the problem Use algorithm Convert to actual codes
4
UNIMAP Sem2-07/08EKT120: Computer Programming4 Sample C Program //Program name : program1.c //Programmer : Yasmin //This program reads income and calculate nett income //after epf and tax deduction #include int main(void) { float income, net_income; const float epf=0.1, tax=0.05; printf(“Enter income : “); scanf(“%f”, &income); net_income=income-(epf*income) – (tax*income); printf(“\nNett income : %5.2f”, net_income); return 0; } The terms void indicates we receive nothing from OS and return an integer to OS Variable & constant declaration begin end Return 0(int) to OS body Comments Preprocessor directives
5
UNIMAP Sem2-07/08EKT120: Computer Programming5 Variables & Reserve Words Identifiers labels for program elements 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 identifiers Reserve words already assigned to a pre-defined meaning eg: delete, int, main, include, double, for, if etc.
6
UNIMAP Sem2-07/08EKT120: Computer Programming6 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 /* /* */*/
7
UNIMAP Sem2-07/08EKT120: Computer Programming7 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
8
UNIMAP Sem2-07/08EKT120: Computer Programming8 Data Types & Mem. Alloc. 1 Boolean representation of logic states. Can only be assigned true (1) or false (0). bool 8 A more precise version of float. Has larger dynamic range and better representation of decimal points. double 4Floating-point number. Set of real numbers. float 4 Integer quantity. Can be represented in signed or unsigned form (with the unsigned keyword). int 1 A single character. Internally stored as a coded integer value (refer to ASCII table ). char Size (bytes) DescriptionData Type
9
UNIMAP Sem2-07/08EKT120: Computer Programming9 Type declarations 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
10
UNIMAP Sem2-07/08EKT120: Computer Programming10 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
11
UNIMAP Sem2-07/08EKT120: Computer Programming11 Arithmetic Operators Used to execute mathematical equations The result is usually assigned to a data storage (instance/variable) using assignment operator ( = ) E.g sum = marks1 + marks2;
12
UNIMAP Sem2-07/08EKT120: Computer Programming12 Arithmetic Operators r % s r mod s % Remainder (Modulus) x / y / Division b * m bm * Multipication p - c p – c - Subtraction f + 7 + Addition C expressionAlgebraic expression Arithmetic Operator C Operation
13
UNIMAP Sem2-07/08EKT120: Computer Programming13 Exercise on arithmetic operators Given x = 20, y = 3 z = x % y = 20 % 3 = 2 (remainder)
14
UNIMAP Sem2-07/08EKT120: Computer Programming14 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
15
UNIMAP Sem2-07/08EKT120: Computer Programming15 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.)
16
UNIMAP Sem2-07/08EKT120: Computer Programming16 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))
17
UNIMAP Sem2-07/08EKT120: Computer Programming17 Truth table for && (logical AND) operator true false true falsetruefalse exp1 && exp2exp2exp1
18
UNIMAP Sem2-07/08EKT120: Computer Programming18 Truth table for || (logical OR) operator true falsetrue false exp1 || exp2exp2exp1
19
UNIMAP Sem2-07/08EKT120: Computer Programming19 Compund 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;
20
UNIMAP Sem2-07/08EKT120: Computer Programming20 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
21
UNIMAP Sem2-07/08EKT120: Computer Programming21 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; printf(“%d”, ++val); Output: 6 val=5; printf(“%d”, --val); Output: 4 val=5; printf(“%d”, val++); Output: 5 val=5; printf(“%d”, val--); Output: 5
22
UNIMAP Sem2-07/08EKT120: Computer Programming22 Operator Precedence last= seventh|| sixth&& fifth== != fourth = > third+ - second* / % first! + - PrecedenceOperators
23
UNIMAP Sem2-07/08EKT120: Computer Programming23 Formatted Output with printf
24
UNIMAP Sem2-07/08EKT120: Computer Programming24 Formatted Output with printf- cont
25
UNIMAP Sem2-07/08EKT120: Computer Programming25 Formatted input with scanf
26
UNIMAP Sem2-07/08EKT120: Computer Programming26 Formatted input with scanf- cont
27
UNIMAP Sem2-07/08EKT120: Computer Programming27 Program debugging Syntax error Mistakes caused by violating “grammar” of C C compiler can easily diagnose during compilation Run-time error Called semantic error or smart error Violation of rules during program execution C compiler cannot recognize during compilation Logic error Most difficult error to recognize and correct Program compiled and executed successfully but answer wrong
28
UNIMAP Sem2-07/08EKT120: Computer Programming28 Program debugging-syntax error snapshot
29
UNIMAP Sem2-07/08EKT120: Computer Programming29 Program debugging-run time error snapshot
30
UNIMAP Sem2-07/08EKT120: Computer Programming30 Program debugging-logic error snapshot
31
UNIMAP Sem2-07/08EKT120: Computer Programming31 End Week 1 – Session 2 Q & A!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.