BASIC ELEMENTS OF A COMPUTER PROGRAM CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING LOGO
Contents 1 Identifier 2 3 Rules for naming and declaring data variables Basic data types 4 Arithmetic operators 5 Arithmetic expression
IDENTIFIERS
IDENTIFIERS Identifier : the name of something that appears in a program. Example of identifiers: variables, function and constants. Identifiers Rules: The 1 st character must be an alphabet letter (a,b,c, z) or an underscore _. Can only consists of letters, digits and underscore _. No symbol allowed (!@#$%^&*.) Cannot use C++ reserved words/keywords There must be no blank space character (Room 3, Level 1) Identifiers are case sensitive Examples : NO1, no1, No1,nO1 (Different)
Reserved Word It has predefined meaning for the language. int char long float double default case for void break const else if private do else goto switch while public class return continue break
Legal and Illegal Identifiers The following are legal identifiers in C++: _first conversion2 payrate student_name
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 Represent storage location in the computer s memory to store value. A symbolic name in the form of a letter, word or phrase that linked to a value. Every variable stored in the computer s memory has a name, a value and a type. Variable Rules Must begin with a letter or underscore (_), and may only contain only letters, underscore, or digits. Variable name cannot consists of more than 31 characters. Example: _names; number_of_cars; No1;
Variable Declaration Syntax: data_type variable; Example: int x; int number1, number2, number3; char codemi7 ; float after_sale_price; 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. float length = 6.0;
Identifiers
Whitespaces Every C++ program contains whitespaces Include blanks, tabs, and newline characters Used to separate special symbols, reserved words, and identifiers Proper utilization of whitespaces is important Can be used to make the program more readable
STANDARD DATA TYPES
DATA TYPES Data type: set of values together with a set of operations C++ data types fall into three categories: Simple data type Structured data type Pointers
Simple Data Type Three (3) categories of simple data Integral: integers (numbers without a decimal) Can be further categorized: char, short, int, long, bool, unsigned char, unsigned short, unsigned int, unsigned long Floating-point: decimal numbers Enumeration type: user-defined data type
DATA TYPES (Cont.) Every variable used in program hold data, and every data must have their own type. It is the way how we can measure the variable s data value as exist in the real world. Example of data types int, float, double, char
Data Types Categories of data type Numeric data type integer floating points Character data type Single Character String of characters
Numeric Data Type Integer (without a decimal) Whole number positive, zero or negative -543,-414,-30,-28,-1,0,1,232,83,94,725.. C++ keyword int, short, long Floating point Numbers with decimal points -87.345, 90.5443, 1.25,-88.60 C++ keyword float, double int area; float area;
int Data Type May be a zero (0), positive (1,2,3..) or negative (-1,-2,-3..) whole number. int will process integers within the range of -32768 to 32767 long integers will process integers within the range of -4294967296 to 4294967295 Examples: -6728 0 78
float, double Data Type Is a decimal number that contains the decimal point (.)or the exponent (e) or both. float data type range approximately 6 to 7 digits of precision Examples 32.88-89.5 double data type takes approximately 13 decimal digits of precision Examples 12.786675434-25.78301465
Bool Data Type bool type Two values: true and false Manipulate logical (Boolean) expressions Logical values bool, true, and false Reserved words
Character Data Type Single character ( keyword = char ) Store one character value ( a digit, letter or special symbol ) Single character value is enclosed in a single quotation mark ( ) Some of the values belonging to char data type are: 'A', 'a', '0', '*', '+', '$', '&' A blank space is a character Written ' ', with a space left between the single quotes Example: char grade = A ;
char[] Data Type String of characters ( keyword = char []) A string = a sequence of characters that terminated by a null character, \0 which has the value zero. The null character denotes the end of a string and is added to the string by compiler automatically. Example, to declare a string text that could hold 5 characters, the following statement is written. char text[6] = Hello ; Enclosed in double quotation marks ( ). Position of first character is 0, the position of the second is 1, and so on 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 32 6 digit precision 32.88, -89.5 double floating point double 64 12 digit precision 12.7866754 Character Character char 8 c, v Sequence of character char[ ] hello, sarah
Exercises 1) Give the most appropriate data type of the following values. i. 305 ii. A iii. 6.85 iv. RM45.60 v. 20123.12345 vi. + vii. 305 viii.80617
Exercises 1) Which of the following identifiers are VALID? i) Count_number ii) Net salary iii) countryname iv) tax6% 2) If the variable has_membership are declared to store an input from user which is expected to store Y, y, N or n. What is the suitable data type to be used for variable declaration?
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 keyword type const float PI = 3.14; Constant_name value
Exercise Problem: to calculate area of a circle Data Data type Variable name Input radius float radius Output area float areacircle Process areacircle = 3.142*radius*radius
Exercises Problem: to compute the area of triangle. Input Data Data type Variable name Output Process
Exercise Problem: to display user s name and retirement age Input Data Data type Variable name Output Process
Initialization 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 1) Write a C++ statement that declare a variable name numberofpeople. Assign the data type int to the variable and initialize it appropriately. 2) Write a C++ statement that declare a variable name interestrate. Assign the data type double to the variable and initialize it appropriately. 3) 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 Highest Lowest Operators Associativity () Left to right * / % Left to right + - Left to right 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.
Precedence & Associativity Example 1: 4 + 2 * 3 Example 2: 3 * 4 / 2
ARITHMETIC EXPRESSION
operands One of the input of an operator Example: Operator 3 + 6 Operand
Several rules that you need to follow to evaluate arithmetic expression: 1. If one or more operand(s) in an arithmetic expression has/have data type double, the resulting expression is of data type double. 2. 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 0-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 : 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 : 1. (c 5.0) = 10.0 2. (d + 7.0) = 15.0 3. (e - 37.0) = 3.0 4. (a + b / 10.0) / (15.0 / 3.0 / 3.0) 5. (a + 2) / (5.0 / 3.0) 6. (12) / (1.667) 7. 7.2
LOGO