Chapter Three
Chapter Three
:
Identifiers Names given to different entries such as variables, structures, and functions
Keywords: Reserved words which have fixed meaning and its meaning cannot be changed
Constants: Are like a variable, except that their value never changes during execution once
defined
Identifiers
Programmer given names
Identify classes, variables, functions, etc.
Assignment operator =
Assigns value on left to variable on right
Binary operator (two operands)
Example:
sum = variable1 + variable2;
Add the values of variable1 and variable2
Store result in sum
1 // Fig. 2.5: fig02_05.cpp
Outline
2
3
4
// Addition program that displays the sum of two numbers.
#include <iostream.h> // allows program to perform input and output
RESULT
The const Keyword
Following is the form
const type variable = value;
Eg,
RESULT
Pointers
Simple Data Types
Three categories of simple data Type
Type int
represent integers or whole numbers
Some rules to follow:
Plus signs do not need to be written before the number
Minus signs must be written when using negative
Decimal points cannot be used
Commas cannot be used
Leading zeros should be avoided
Signed - negative or positive
Unsigned - positive
Short
Long
Simple Data Types
Type double, float
used to represent real numbers
many programmers use type float
avoid leading zeros, trailing zeros are ignored
long double
Simple Data Types
Type char
used to represent character data
a single character which includes a space
1 byte, enough to hold 256 values
must be enclosed in single quotes eg. ‘d’
Escape sequences treated as single char
‘\n’ newline
‘\’’ apostrophe
‘\”’ double quote
‘\t’ tab
(0-255) or as a member of the ASCII set
E.g. the lowercase letter "a" is assigned the value 97
Simple Data Types
Strings
used to represent textual information
string constants must be enclosed in double quotation marks
eg. “Hello world!”
empty string “”
new line char or string “\n”
“the word \”hello\”” (puts quotes around “hello” )
Area.cpp
#include <iostream.h>
int main() {
float Length;
float Width;
// Extract length and width
cout << enter width and length: ";
cin >> Length >> Width;
// Compute and insert the area
float Area = Length * Width;
cout << "Area = " << Area << " =
Length "
<< Length << " * Width " << Width
<< endl;
return 0;}
Write a program that accepts a character from the user
and echo it to the screen.
Operator
A symbol that tells the compiler to perform specific
mathematical or logical manipulations
5 Operators
Arithmetic operators
Assignment operator
Increment/Decrement operators
Relational operators
Logical operators
Arithmetic Operators
Common
Addition +
Subtraction -
Multiplication *
Division /
Mod %
Integer Division
Integer division produces an integer result
Truncates the result
Examples
3 / 2 evaluates to 1
4 / 6 evaluates to 0
10 / 3 evaluates to 3
Mod
Produces the remainder of the division
Examples
5 % 2 evaluates to 1
12 % 4 evaluates to 0
4 % 5 evaluates to 4
Arithmetic Operators and Precedence
Consider m*x + b which of the following is it equivalent to
(m * x) + b
m * (x + b)
Operator precedence tells how to evaluate expressions
Examples
20 - 4 / 5 * 2 + 3 * 5 % 4
(4 / 5)
((4 / 5) * 2)
((4 / 5) * 2) (3 * 5)
((4 / 5) * 2) ((3 * 5) % 4)
(20 -((4 / 5) * 2)) ((3 * 5) % 4)
(20 -((4 / 5) * 2)) + ((3 * 5) % 4)
Increment & Decrement Operators
Pre-increment:++variable
Post-increment: variable++
Pre-decrement: --variable
Post-decrement: variable--
Increment & Decrement Operators (continued)
++count; or count++; increments the value of count by
1
--count; or count--; decrements the value of count by 1
If x = 5; and y = ++x;
After the second statement both x and y are 6
If x = 5; and y = x++;
After the second statement y is 5 and x is 6
Assignment Operator
Assignment operator =
Assigns value on left to variable on right
Example:
int a= 5;
float b= 9.66;
char ch=‘d’;
int m, n, p;
m = n = p = 100;
Assignment Operator
Compound assignment Operator
== Equality 5 == 5 // gives 1
!= Inequality 5 != 5 // gives 0
Relational operators
Logical Operators
Like the relational operators, logical
operators evaluate to 1 or 0.
Operator Name Example
Logical operators
Type conversion
A value in any of the built-in types can be converted
Called type cast
Syntax
(<data – type> )value; or <data – type> (value);
E.g
(int) 3.14 // converts 3.14 to an int to give 3
(long) 3.14 // converts 3.14 to a long to give 3L
(double) 2 // converts 2 to a double to give 2.0
(char) 122 // converts 122 to a char whose code is
122
(unsigned short) 3.14 // gives 3 as an unsigned short
Some times the compiler does the type casting – implicit
type cast
E.g
double d = 1; // d receives 1.0
int i = 10.5; // i receives 10
i = i + d;
End of 3rd chapter
43