Presentation is loading. Please wait.

Presentation is loading. Please wait.

BASIC ELEMENTS OF A COMPUTER PROGRAM

Similar presentations


Presentation on theme: "BASIC ELEMENTS OF A COMPUTER PROGRAM"— Presentation transcript:

1 BASIC ELEMENTS OF A COMPUTER PROGRAM
CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING

2 Arithmetic expression
Contents Identifier 1 Rules for naming and declaring data variables 2 Basic data types 3 Arithmetic operators 4 Arithmetic expression 5 Assignment statement 6

3 IDENTIFIERS

4 Identifiers An identifier is a name assigned by the user.
It is to represent programming entities such as variable, function and constants. Rules in naming an identifier: The 1st character must be an alphabet letter or an underscore ‘_’. Can only consists of letters, digits and underscore ‘_’. Cannot use C++ reserved words/keywords There must be no blank space character Identifiers are case sensitive

5 Reserved Word It has predefined meaning for the language. int char
long float double default case for void break const else if private do goto switch while public class return continue

6 Variable A location in computer memory to store data.
Can only store 1 value at a time Value of a variable may change during program execution. Must be declared before used. Must always assign/ initialize a value to a variable before using the variable.

7 Variable Declaration Syntax: data_type variable; Example: int x; int number1, number2, number3; Please take note of the coma to Separate the variable list & Semicolon to end the variable declaration

8 Variable: what happen when a variable is declared??
The compiler allocates a memory space for the variable. In the memory space, name, data type and value of the variable are stored. ex. Allocates a memory of 4 bytes with address the information stored in the memory space are variable name (area), data type (float). float area;

9 Variable Rules Must begin with a letter or underscore (_), and may only contain only letters, underscore, or digits. Variable name cannot be a keyword. Variable name cannot consists of more than 31 characters.

10 Constant Values that are fixed and do not change throughout program execution Declaring a constant: Using const keyword

11 Constant Declaration Syntax: const data_type constant_name = value; const float PI = 3.14; type const keyword value Constant_name

12 Legal and Illegal Identifiers
The following are legal identifiers in C++: first conversion payRate

13 STANDARD DATA TYPES

14 Data Types Categories of data type Numeric data type
Character data type

15 Numeric Data Type Integer Floating point
Whole number – positive, zero or negative C++ keyword int, long Floating point Numbers with decimal points float, double int area; float area;

16 int Data Type Examples: -6728 78
78 Positive integers do not have to have a + sign in front of them No commas are used within an integer Commas are used for separating items in a list

17 float, double Data Type Is a decimal number that contains the decimal point (.)or the exponent (e) or both. float data type can hold values up to six or seven significant digit accuracy. double data type can hold values up to 14 or 15 significant digit accuracy.

18 char Character Data Type char name; Single character Example:
Store one character value C++ keyword char Example: char name;

19 char Data Type The smallest integral data type
Used for characters: letters, digits, and special symbols Each character is enclosed in single quotes Some of the values belonging to char data type are: 'A', 'a', '0', '*', '+', '$', '&' A blank space is a character and is written ' ', with a space left between the single quotes

20 char[] Data Type Sequence of characters
It stores sequence of zero or more characters Enclosed in double quotation marks Null: no characters is stored. Each character has relative position Position of first character is 0, the position of the second is 1, and so on Length: number of characters in a sequence C++ keyword char[] char name[5]; Note: the double quotes and Single quotes

21 SUMMARY OF DATA TYPE Category Data types Keywords Bits Range Examples
Numeric integer int 16 to 32767 45, 0, -10 long integer long 32 to -37876 floating point Float 6 digit precision 32.88, -89.5 double floating point double 64 12 digit precision Character char 8 ‘c’, ‘v’ Sequence of character char[ ] “hello”, “sarah”

22 Exercise Problem: to calculate area of a circle Data Data type
Variable name Input radius float Output area areaCircle Process areaCircle = 3.142*radius*radius

23 Exercises Problem: to compute the area of triangle. Data Data type
Variable name Input Output Process

24 Exercise Problem: to display user’s name and retirement age Data
Data type Variable name Input Output Process

25 Initialization float rate = 0.0; double sale = 0.0; char grade = ‘ ’;
A variable can be initialized to any value as long as the value’s data type matches the variable’s data type. Syntax: datatype variable [ = initialValue ]; Examples: int age = 0; float rate = 0.0; double sale = 0.0; char grade = ‘ ’; char company[20] =“ ”;

26 Exercises Write a C++ statement that declare a variable name numberOfPeople. Assign the data type int to the variable and initialize it appropriately. Write a C++ statement that declare a variable name interestRate. Assign the data type double to the variable and initialize it appropriately. Write a C++ statement that declare a float variable named rate. Initialize the variable to the number 5.6

27 operators

28 OPERATORS A Symbol that tells the compiler to perform specific mathematical or logical manipulations Types of operator: Arithmetic operators Assignment operators Relational operators Logical operators

29 Arithmetic operator Addition Subtraction Modulus Multiplication
Division

30 Arithmetic operator Operation Operator Subtraction - Addition +
Multiplication * Division / Modulus %

31 Precedence & Associativity
Precedence of operator Precedence rules: specify which operator is evaluated first when two operators with different precedence are adjacent in an expression. Associativity rules: specify which operator is evaluated first when two operators with the same precedence are adjacent in an expression. Highest Operators Associativity () Left to right * / % Lowest

32 Precedence & Associativity
Example 1: Example 2: 4 + 2 * 3 3 * 4 / 2

33 Arithmetic expression

34 operands One of the input of an operator Example: 3 + 6 Operator

35 Several rules that you need to follow to evaluate arithmetic expression:
If one or more operand(s) in an arithmetic expression has/have data type double, the resulting expression is of data type double. If all operands in arithmetic expression are of type int, the resulting expression is of type int.

36 Arithmetic operators Require two operands Ex. +, -, *, /, %
For the modulus (remainder) operator, all of its operands must be integer only. Be careful of the operands’ data types when using division operator. For example, Expressions Output 7 / 4 1 7.0 / 4 1.75 -1 / 4 -1 / 4.0 -0.25

37 Example 3 * 7 – * 5 / (3 * 7) – 6 + ( (2 * 5) / 4 ) + 6 = 21 – 6 + (10 / 4) + 6 (Evaluate *) = 21 – (Evaluate /) = (Evaluate -) = (Evaluate first +) = 23 (Evaluate +)

38 Test yourself!!!! Examples : double a, b, c, d, e;
Declaration double a, b, c, d, e; Initialization a = 10.0, b = 20.0, c = 15.0, d = 8.0, e = 40.0; Expression to be evaluated (a + b / ( c – 5.0 )) / ((d + 7.0) / (e – 37.0) / 3.0)

39 Answer: (a + b / ( c – 5.0 )) / ((d + 7.0) / (e – 37.0) / 3.0) The sequence : (c – 5.0) = 10.0 (d + 7.0) = 15.0 (e ) = 3.0 (a + b / 10.0) / (15.0 / 3.0 / 3.0) (a + 2) / (5.0 / 3.0) (12) / (1.667) 7.2

40 Thank You !


Download ppt "BASIC ELEMENTS OF A COMPUTER PROGRAM"

Similar presentations


Ads by Google