1
Programming
Lecture 4
VARIABLES
Dr. Badria Nabil
Assignment 2
2
Write a C++ program that computes and display the
multiplication table of "5" as the following:
5*1=5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
Assignment 2 Solution
3
#include <iostream>
using namespace std;
int main()
{
cout << "the multiplication table of 5 \n";
cout << "5*1= " << 5 * 1 << endl;
cout << "5*2= " << 5 * 2 << endl;
cout << "5*3= " << 5 * 3 << endl;
cout << "5*4= " << 5 * 4 << endl;
cout << "5*5= " << 5 * 5 << endl;
cout << "5*6= " << 5 * 6 << endl;
cout << "5*7= " << 5 * 7 << endl;
cout << "5*8= " << 5 * 8 << endl;
cout << "5*9= " << 5 * 9 << endl;
cout << "5*10= " << 5 * 10 << endl;
return 0;
}
Chaining Stream Insertion Operations
stream insertion operations
Use multiple stream insertion operators in a single
statement
◼ Stream insertion operation knows how to output each type
of data
Alsocalled chaining or cascading
Example
◼ std::cout << "Sum is " << number1 + number2
<< std::endl;
◼ Outputs "Sum is “
◼ Then, outputs sum of number1 and number2
4
What Are Variables?
Locationin memory where value can be stored
Variables must be explicitly declared.
Each variable has a type, an identifier, and a scope.
Type
Identifier
Initial value
int myAge;
boolean isAMovie;
float maxItemCost = 17.98F;
Declaring Variables
The basic form of variable declaration:
◼ type identifier [ = initial value];
int itemsRented = 1;
float itemCost;
int i, j, k;
double interestRate;
Variables can be initialized when declared.
Data Types
Defines a set of values and operations that can
be performed on those values
integers
positive and negative whole numbers,
e.g. 5, -52, 343222
short, int, long
represented internally in binary
8
Data Types (con’t)
Floating point (real)
number has two parts, integral and fractional
e.g. 5, 3.66666666, -.000034, 5.0
float, double, long double
stored internally in binary as mantissa and exponent
10.0 and 10 are stored differently in memory
Data Types (con’t)
Boolean
named for George Boole
represent conditional values
values: true and false
11
Data Types (con’t)
Characters
represent individual character values
E.g. ’A’ ’a’ ’2’ ’*’ ’”’ ’ ’
stored in 1 byte of memory
special characters: escape sequences
E.g. ’\n’ ’\b’ ’\r’ ’\t’ ‘\’’
13
14
string Class
Strings not built-in, but come from library
Classes extend C++
string literal enclosed in double quotes
E.g.: “Enter speed: “ “ABC” “B” “true”
“1234”
#include <string>
for using string identifiers, but not needed for literals
16
Data type Size Example
Char 1 byte -128 to 127
unsigned char 1 byte 0 to 255
int 2 bytes -32,768 to 32,767
unsigned int 2 bytes 0 to 65,535
long or (long int) 4 bytes -2,147,483,648 to 2,147,483,647
unsigned long 4 bytes 0 to 4,294,967,295
float 4 bytes 3.4 x 10^-38 to 3.4 x 10^38
double 8 bytes 1.7 x 10^-308 to 1.7 x 10^308
long double 10 bytes 3.4 x 10^-4932 to 3.4 x 10^4932
17
10/30/2022
Identifiers
Names for data and objects to be manipulated
by the program
Must begin with a letter or underscore (not
recommended)
Consist only of letters, digits, and underscores
Cannot be reserved word
Upper and lower case significant
Defining Variable Names
Variable names must start with a letter of the
alphabet, or underscore.
Other characters may include digits.
a item_Cost item#Cost item-Cost
itemCost _itemCost item*Cost abstract
item1Cost itemCost2 2itemCost
Use meaningful names for variables, such as
customerFirstName, ageNextBirthday.
Variables Declarations (Cont.)
Can declare several variables of same type in one
declaration
Comma-separated list
int integer1, integer2, sum;
Variable names
Valid identifier
◼ Series
of characters (letters, digits, underscores)
◼ Cannot begin with digit
20
Good Programming Practice
Place a space after each comma (,) to make programs more
readable.
Use identifiers of 31 characters or fewer to ensure portability.
Choosing meaningful identifiers helps make a program self-
documenting—a person can understand the program simply by
reading it rather than having to refer to manuals or comments.
Avoid identifiers that begin with underscores and double
underscores, because C++ compilers may use names like that
for their own purposes internally.
Always place a blank line between a declaration and
executable statements.
21
Input stream object
Obtain data for program to use - different each time
program executes
std::cin
from <iostream>
Usually connected to keyboard
Stream extraction operator >>
◼ Waits for user to input value, press Enter (Return) key
◼ Stores value in variable to right of operator
◼ Converts value to variable data type
Example
◼ std::cin >> number1;
◼ Reads an integer typed at the keyboard
22
◼ Stores the integer in variable number1
Assignment 3
23
24
25