BASIC ELEMENTS OF A COMPUTER PROGRAM CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING
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
IDENTIFIERS
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
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
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.
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
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 00000004. the information stored in the memory space are variable name (area), data type (float). float area;
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.
Constant Values that are fixed and do not change throughout program execution Declaring a constant: Using const keyword
Constant Declaration Syntax: const data_type constant_name = value; const float PI = 3.14; type const keyword value Constant_name
Legal and Illegal Identifiers The following are legal identifiers in C++: first conversion payRate
STANDARD DATA TYPES
Data Types Categories of data type Numeric data type Character data type
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;
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
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.
char Character Data Type char name; Single character Example: Store one character value C++ keyword char Example: char name;
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
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
SUMMARY OF DATA TYPE Category Data types Keywords Bits Range Examples Numeric integer int 16 -32768 to 32767 45, 0, -10 long integer long 32 -4294967296 to 4294967295 -37876 floating point Float 6 digit precision 32.88, -89.5 double floating point double 64 12 digit precision 12.7866754 Character char 8 ‘c’, ‘v’ Sequence of character char[ ] “hello”, “sarah”
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
Exercises Problem: to compute the area of triangle. Data Data type Variable name Input Output Process
Exercise Problem: to display user’s name and retirement age Data Data type Variable name Input Output Process
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] =“ ”;
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
operators
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
Arithmetic operator Addition Subtraction Modulus Multiplication Division
Arithmetic operator Operation Operator Subtraction - Addition + Multiplication * Division / Modulus %
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
Precedence & Associativity Example 1: Example 2: 4 + 2 * 3 3 * 4 / 2
Arithmetic expression
operands One of the input of an operator Example: 3 + 6 Operator
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.
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
Example 3 * 7 – 6 + 2 * 5 / 4 + 6 (3 * 7) – 6 + ( (2 * 5) / 4 ) + 6 = 21 – 6 + (10 / 4) + 6 (Evaluate *) = 21 – 6 + 2 + 6 (Evaluate /) = 15 + 2 + 6 (Evaluate -) = 17 + 6 (Evaluate first +) = 23 (Evaluate +)
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)
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 - 37.0) = 3.0 (a + b / 10.0) / (15.0 / 3.0 / 3.0) (a + 2) / (5.0 / 3.0) (12) / (1.667) 7.2
Thank You !