1 / 21

Week 2 – Introduction to Computer Programming

Week 2 – Introduction to Computer Programming. CONTENTS. Identifiers Variable Constant Data Types and Types Declaration Operators Pre-Processor Directives scanf/printf Program debugging/compiling. IDENTIFIERS (1). There are two types of identifiers Variable Constance

donnel
Télécharger la présentation

Week 2 – Introduction to Computer Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Week 2 – Introduction to Computer Programming Created By: Dr. Shahrul Nizam Yaakob

  2. CONTENTS • Identifiers • Variable • Constant • Data Types and Types Declaration • Operators • Pre-Processor Directives • scanf/printf • Program debugging/compiling Created By: Dr. Shahrul Nizam Yaakob

  3. IDENTIFIERS (1) • There are two types of identifiers • Variable • Constance • The naming rules: • Can’t used the reserved words • (e.g. void, include, main, return, printf, scanf etc) • No blanks spacing • (e.g. data 1, out 1) Created By: Dr. Shahrul Nizam Yaakob

  4. IDENTIFIERS (2) • Variable • Hold data and can change during program execution • e.g. • int x, double y • Constant • Hold data and can’t change during program execution • e.g. • const int x=1, const double y=0.1 Created By: Dr. Shahrul Nizam Yaakob

  5. DATA TYPES Created By: Dr. Shahrul Nizam Yaakob

  6. TYPES OF DECLARATION • Single declaration: (e.g.) • float fInput; • float fOutput; • Combine declaration: (e.g.) • float fInput, fOutput; • Declare and initialize: (e.g.) • float fInput=0.1,fOutput=0.0076; const int iK=2; Created By: Dr. Shahrul Nizam Yaakob

  7. OPERATORS • Types of operators are: • Arithmetic operators • Unary operators • Compound assignment operators • Relational operators • Logical operators • Bitwise operators Created By: Dr. Shahrul Nizam Yaakob

  8. C Operation Arithmetic Operator Algebraic Expression C Expression Addition + f + 7 f + 7 Subtraction - p – c p - c Multipication * bm b * m Division / x / y x / y Remainder (Modulus)‏ % r mod s r % s ARITHMATIC OPERATORS 1 Created By: Dr. Shahrul Nizam Yaakob

  9. ARITHMATIC OPERATORS 1 • How Remainder operator works: • E.g. given x=10 y=3 z = 10 % 3 = 1 void main () { z = x % y; } Created By: Dr. Shahrul Nizam Yaakob

  10. UNARY OPERATORS • Operating on ONE operand • E.g. ++j, j++, --i, i-- int j, i=2; j = i++; // j=2 and i=3 j = ++i; // j=3 and i=3 j = --i; // j=1 and i=1 j = i--; // j=2 and i=1 Created By: Dr. Shahrul Nizam Yaakob

  11. COMPOUND ASSIGNMENT OPERATORS • Combination between ‘+’ and ‘=‘ • Combination between ‘-’ and ‘=‘ int j=4, i=2; j+=i; //j=j+i j=6 i=2 j+=3; //j=j+3 j=7 j-=i; //j=j-2 j=2 j-=4; //j=j-4 j=0 Created By: Dr. Shahrul Nizam Yaakob

  12. RELATIONAL OPERATORS • X == Y;X is equal to Y • X != Y; X is not equal to Y • X > Y;X is greater then Y • X < Y; X is less then Y • X <= Y; X is less then or equal to Y • X >= Y;X is greater then or equal to Y Reminder: DO NOT confuse == (relational operator) with = (assignment operator)‏ Created By: Dr. Shahrul Nizam Yaakob

  13. LOGICAL OPERATORS • Logical operators are manipulation of logic and there are: • && (AND) • || (OR) e.g. if ((i==1)&&(k!=2)) //must both if ((i==1)||(k!=2)) //either one Created By: Dr. Shahrul Nizam Yaakob

  14. BITWISE OPERATORS • Will not be covered in this course! Created By: Dr. Shahrul Nizam Yaakob

  15. Pre-Processors Directives • The C Pre-processor (cpp) executes before a program is compiled • Any Pre-processor directives begin with # • It can performs (e.g): • Inclusion of other files (e.g: #include < >) • Definition of symbolic definition • Conditional of compilation • Conditional execution of pre-processor directives Created By: Dr. Shahrul Nizam Yaakob

  16. printf (1) • Require stdio.h header file printf(“…%[width][.precision]specifier…”, identifier,…); Created By: Dr. Shahrul Nizam Yaakob

  17. printf (2) int x=1; double y=0.3; printf (“x=%d y=%lf”, x, y); x=1 y=0.3 printf(“x=%3d y=%.3lf”,x,y); x= 1 y=0.300 Printf(“x=%3.1d y=%3.3lf”, x,y); x= 1.0 y=0.3 Created By: Dr. Shahrul Nizam Yaakob

  18. printf (2) Created By: Dr. Shahrul Nizam Yaakob

  19. scanf(1) • Require stdio.h header file • e.g int x; float y; char w; scanf(“%d%f%c”, &x,&y,&w); Created By: Dr. Shahrul Nizam Yaakob

  20. scanf(2) Created By: Dr. Shahrul Nizam Yaakob

  21. Program Debugging/Compiling • 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 Created By: Dr. Shahrul Nizam Yaakob

More Related