Lesson 2
Lesson 2
Variable
Declaration
What is Variable
A variable is simply a name the programmer uses to refer to the
computer storage locations. The term Variable are used because the value stored
in the variable can change, or vary,. The computer keep track of the actual
memory address that corresponds to each name the programmer assigned.
Variable = Expression
the semantic (meaning) of the assignment operator (=) is “store”; the value of the
expression is stored into the variable. Any previous value in the variable is destroyed and replace by
the value of the expression.
Expression
An arrangement of identifier, literals, and operators that can be evaluated to compute a
value of a given type.
Examples
Given the declarations
string firstname;
int age;
string lastname;
the following assignment statements are valid:
Firstname =“Jacob Samuel”;
Age = 5;
Lastname = “Pomerada”;
Local Variables
Variables that are declared inside a function or block are local variables. They can be
used only by statements that are inside that function or block code.
Examples
#include <iostream>
using namespace std;
int main ()
{
//Local variable declaration
int a=0, b=0;
int product = 0;
//actual variable initialization by assigning values.
a = 5;
b = 10;
product = (a * b);
cout<<"The Product of: "<<a<<" and "<<b<<" is "<<product<<".";
return 0;
}
Global Variables
Global variables are defined outside of all the functions, usually on top of the program.
The global variables will hold their value throughout the lifetime of your program.
Examples
#include <iostream>
using namespace std;
//Global Variable declaration.
int sum;
int main ()
{
//Local variable declaration
int a, b;
//actual variable initialization by assigning values.
a = 5;
b = 10;
sum = (a * b);
cout<<"The sum of: "<<a<<" and "<<b<<" is "<<sum<<".";
return 0;
}
A program can have the same name for local and global variables but the value of a local
variable inside a function will take preference.
Examples
#include <iostream>
using namespace std;
//Global Variable declaration.
int a=100;
int main ()
{
//Local variable declaration
int a=50;
cout<<"The value is "<<a<<".";
return 0;
}
Data Type
This is a definition of a set of data that specifies the possible range of values of the set,
the operations that can be performed on the values, and the way in which the values are stored in
memory. Knowing the type of data enables the computer to manipulate it appropriately.
Data type in C++ are mainly divided
into two types:
1. Primitive data types : these data types are built-in or predefined data types and can be used
directly by the user to declare variables. Example: int, char, float, bool etc. Primitive data types
available in C++ are:
integer
Character
Boolean
Floating point
Double Floating Point
Valueless or Void
Wide Character
•
Data Type Keyword Description
== Is equal to
if (age>=18){
cout<<"\n\n";
cout<<"Your are already adult"<<endl;
}
else {
cout<<"\n\n";
cout<<"Sorry your are still a minor";
cout<<"\n\n";
}
return 0;
}
Arithmetic Operators
There are five arithmetical operations supported by C++ are:
Operators Description
+ Addition
- subtraction
* multiplication
/ division
% modulo
Arithmetic Operators
Operations of addition, subtraction, multiplication and division correspond
literally to their respective mathematical operations. The lest one is modulo
operator, represented by a percent sing (%), gives the remainder of a division of
two values. For example.
X = 11%3
int main ()
Example {
int divisor, dividend, qoutient, remainder;
cout<<"\n\n";
cout<<"\t Qoutient and Remainder Solver";
cout<<"\n\n";
qoutient = (dividend/divisor);
remainder = (dividend%divisor);
cout<<"\n\n";
cout<<"\t Qoutient is: "<<qoutient<<endl;
cout<<"\t Remainder is: "<<remainder<<endl<<endl;
//Logical OR Example
if (a>b||c==d){
cout<<"\t A is greater than B OR C is equal to D.\n";
}
else{
cout<<"\t Neither A is greater than B OR C is equal to D. \n";
}