Prepared by: Saltihie Bin Zeni Jabatan Kej.
Elektrik, PMU
Start
OVERVIEW
2.1
Understand constant and variables Understand data types Understand operator and expression
2.2
2.3
2.1 Understand constant and variable
Constant
Values that do not change during program execution. Example : conts double PI = 3.14159 // conts is a reserved word
Variables
Variables are like small blackboards We can write a number on them We can change the number We can erase the number Example: int answer;
Identifiers
Identifiers
Variables names are called identifiers Choosing variable names Use meaningful names that represent data to be stored First character must be a letter the underscore character Remaining characters must be letters numbers underscore character
Example of identifiers
Identifier Valid Reason if Invalid
TotalSales Total_Sales Total.sales 4Qthrsales Total_sales$
Yes Yes No No No Cannot contains . Cannot begin with number Cannot contain $
Keyword
Keywords (also called reserved words)
Are used by the C++ language Must be used as they are defined in the programming language Cannot be used as identifiers Examples of keyword: asm class do auto conts break huge while else case float if far
char
Variables declaration (1)
Before use, variables must be declared Tells the compiler the type of data to store int number_of_bars; double one_weight, total_weight; int is an abbreviation for integer. could store 3, 102, 3211, -456, etc. number_of_bars is of type integer double represents numbers with a fractional component could store 1.34, 4.0, -345.6, etc. one_weight and total_weight are both of type double Examples:
Variables declaration (2)
Immediately prior to use
Two locations for variable declarations
int main() { int sum; sum = score1 + score 2; return 0;
}
At the beginning int main() { int sum; sum = score1 + score2; return 0; }
Variables declaration (3)
Declaration syntax:
Data_type Variable_1 , Variable_2, . . . ;
Declaration Examples:
double average, m_score, total_score; double moon_distance; int age, num_students; int cars_waiting;
Assignment statement
An assignment statement changes the value of a variable total_weight = one_weight + number_of_bars;
total_weight is set to the sum one_weight + number_of_bars
Assignment statements end with a semi-colon The single variable to be changed is always on the left of the assignment operator = On the right of the assignment operator can be
Constants -- age = 21; Variables -- my_cost = your_cost; Expressions -- circumference = diameter * 3.14159;
Assignment statement and Algebra
The = operator in C++ is not an equal sign
The following statement cannot be true in algebra number_of_bars = number_of_bars + 3; In C++ it means the new value of number_of_bars is the previous value of number_of_bars plus 3
Exercise 1
Give good variable names for identifiers to store.
the speed of an automobile? an hourly pay rate? the highest score on an exam?
Identify the following identifiers is valid or not.
Net_income 4years_sale Hour_p#y Year_1income Y2kilo
2.2 Understand data types
2 and 2.0 are not the same number A whole number such as 2 is of type int A real number such as 2.0 is of type double Numbers of type int are stored as exact values Numbers of type double may be stored as approximate values due to limitations on number of significant digits that can be represented
Data type (1)
Integer (int)
Integer data types hold whole numbers Type int does not contain decimal points Examples: 34 45 1 89 Table below shows an integer data types, size and range
Data type (2)
Float/double
The floating-point data types are:
They can hold real numbers such as:
12.45 -3.8
Stored in a form similar to scientific notation All floating-point numbers are signed
Data type (3)
Type float/double can be written in two ways Simple form must include a decimal point
Examples: 34.1 23.0034 1.0 89.9
Floating Point Notation (Scientific Notation)
Examples: 3.41e1 3.67e17 5.89e-6 means means means 34.1 367000000000000000.0 0.00000589
Number left of e does not require a decimal point Exponent cannot contain a decimal point
Data type (4)
Example:
Data type (5)
Character (char) Used to hold characters or very small integer values Usually 1 byte of memory Numeric value of character from the character set is stored in memory:
Data type (6)
Example
2.3 : Know operators & expression
PRIMARY EXPRESSION
a
Identifier
7
constant
(2+a-3)
expression
ASSIGNMENT EXPRESSION
variable
Assignment operators
expression
BINARY EXPRESSION
7
Second operand
First operand
operator
POSTFIX OPERATOR
operand
a++
Postfix operator (increment by 1)
PREFIX EXPRESSION
operand
++a
unary operator
Operator
Can be classified according to:
The type of their operands and their output.
Arithmetic Relational Logical Boolean
The number of their operand.
Unary(one operand) Binary(two operands)
1. Arithmetic Operators (2)
Arithmetic Operators (1)
Arithmetic Operators (3)
Arithmetic Operators (4)
Arithmetic Operators (5)
Arithmetic Operators (6)
Arithmetic Operators (7)
Example of Arithmetic operator
Arithmetic Operators (8)
Arithmetic Operators (9)
Arithmetic Operators (10)
Exercise 2
2. Relational Operator (1)
Relational Operator (2)
Relational Operator (3)
3. Logical Operator (1)
Logical Operator (2)
Logical Operator (3)
Expression
Formed by combining operators, variables and constant. Examples:
Gross_pay deductions (basic_pay + hours * rate) (socso + premium + loan) (b * b 4 * a * c) > 0 (sex == male || sex == female) && age >=21
Mathematical formula and C expression
Mathematical operator C expression
+ x
OR AND NOT
+ * / || && !
Example:
Given the following formula
1 F= 6.2832 LC
Convert to C expression F = 1 / (6.2832 * sqrt (L * C))
Exercise 3
Convert the following equation to C expression:
i. x = m2(m-q) + (p-n) x2 ii. y =
x5
iii.
w = y (x
5.5 ) 10
Given integer variables, a = 5, b=7 and c=8, find the following:
a/b+c a + b x (c / a) b%a+c a b + c x (b / a x c)