1 / 32

Fundamentals of Computer Programming Concepts

This course covers identifiers, reserved words, data types, operators, and more. Learn programming basics with hands-on exercises and examples.

johnwheeler
Download Presentation

Fundamentals of Computer Programming Concepts

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. EET110 2013/2014 Universiti Malaysia Perlis (UniMAP) Introduction to Computer and Algorithm (Part 2)‏ EET110 Basic Computer Programming School of Electrical System Engineering

  2. EET110 2013/2014 Outline Pseudo code & flowchart Identifiers and reserved words Program comments Pre-processor directives Data types and type declarations Operators Formatted input and output Program debugging Sample programming question Sample C program

  3. EET110 2013/2014 Variables & Reserved Words Identifiers/Variables labels for program elements case sensitive can consist 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 Reserved words cannot be variables/identifiers Reserved words already assigned to a pre-defined meaning e.g.: delete, int, main, include, double, for, if, etc.

  4. EET110 2013/2014 Program Comments Starts with /* and terminates with */ OR Character // starts a line comment, if several lines, each line must begin with // Comments cannot be nested /* /* */*/

  5. EET110 2013/2014 Preprocessor Directives An instruction to pre-processor Standard library header: <stdio.h>,<math.h> 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

  6. EET110 2013/2014 Data Types & Memory Allocation Data Type Description 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

  7. EET110 2013/2014 Data Types Declaration float income; float net_income; double base, height, area; 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

  8. EET110 2013/2014 Types of Operators Types of operators are: Arithmetic operators (+ , - , * , / , %)‏ Relational operators (> , < , == , >= , <=, !=)‏ Logical operators (&& , ||)‏ Compound assignment operators (+=, -=, *=, /=, %=)‏ Binary operators: needs two operands Unary operators: single operand Bitwise operators: executes on bit level

  9. EET110 2013/2014 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;

  10. EET110 2013/2014 Arithmetic Operators 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

  11. EET110 2013/2014 Exercise on Arithmetic Operators Given x = 20, y = 3 z = x % y = 20 % 3 = 2 (remainder)‏

  12. EET110 2013/2014 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

  13. EET110 2013/2014 More on relational operators Relational operators use mathematical comparison (operation) on two data, but give logical output e.g.1 let say b = 8, if (b > 10)‏ e.g.2 while (b != 10)‏ e.g.3 if (mark == 60) print (“Pass”); Reminder: DO NOT confuse == (relational operator) with = (assignment operator)‏

  14. EET110 2013/2014 More on logical operators Logical operators are manipulation of logic. For example: i. b=8, c=10, if ((b > 10) && (c<10))‏ ii. while ((b==8) || (c > 10))‏ iii. if ((kod == 1) && (salary > 2213))‏

  15. EET110 2013/2014 Truth Table for &&(logical AND) Operator exp1 exp2 exp1 && exp2 false false false false true false true false false true true true

  16. EET110 2013/2014 Truth Table for ||(logical OR) Operator exp1 exp2 exp1 || exp2 false false false false true true true false true true true true

  17. EET110 2013/2014 Compound Assignment Operators To calculate value from expression and store it in variable, we use assignment operator (=)‏ Compound assignment operator combines 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;

  18. EET110 2013/2014 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

  19. EET110 2013/2014 Comparison of Prefix and Postfix Increments

  20. EET110 2013/2014 Unary Operators (Example)‏ 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

  21. EET110 2013/2014 Formatted Output with “printf” #include <stdio.h> void main (void) { int month; float expense, income; month = 12; expense = 111.1; income = 1000.0 printf (“Month=%2d, Expense=$%9.2f\n”,month,expense); } Declaring variable (month) to be integer Declaring variables (expense and income) to be real Assignment statements store numerical values in the memory cells for the declared variables ‘,’ separates string literal from variable names Correspondence between variable names and %...in string literal

  22. EET110 2013/2014 Formatted Output with printf-cont

  23. EET110 2013/2014 Formatted input with scanf

  24. EET110 2013/2014 Formatted input with scanf-cont

  25. EET110 2013/2014 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

  26. EET110 2013/2014 Program debugging-syntax error snapshot

  27. EET110 2013/2014 Program debugging-run time error snapshot

  28. EET110 2013/2014 Program debugging-logic error snapshot

  29. EET110 2013/2014 Sample Programming Question Write a program that calculates area of triangle. Your program should read the base length and the height length from user. Given the formula to calculate the area of triangle: 0.5 x (base) x (height). Steps: Analyze the problem Use algorithm Convert to actual codes

  30. EET110 2013/2014 Recall..Pseudo code and Flowchart Try develop the pseudo code and flowchart for the problem given in the previous slide.

  31. EET110 2013/2014 Sample C Program //Program name : program1.c //Programmer : Yasmin //This program calculates the area of triangle #include <stdio.h> int main(void)‏ { double base, height, area; printf(“Enter base length : “); scanf(“%f”, &base); printf(“Enter height length : “); scanf(“%f”, &height); area=0.5 * base * height; printf(“\nArea of the triangle is : %5.2f\n”, area); return 0; } Comments Preprocessor directives begin The term void indicates we receive nothing from OS and return an integer to OS Variables declaration body return 0 (int) to OS end

  32. EET110 2013/2014 Q & A! Thank YouEnd Session 2

More Related