PF Ue Lec 2
PF Ue Lec 2
Lecture 2
Data types and Variables – Input/Output
Output:
Hello
Variables
• Variables are containers for storing data values.
Declaring (Creating) Variables
• To create a variable, you must specify the type and name of the
variable.
• Syntax
type variable = value;
Where type is one of C++ data types and variable is the name of the
variable such as “x”, “myVar”
The equal sign is used to assign values to the variable.
Some examples
• int myNum = 15; //declare variable and assign value
cout << myNum;
//assigning new value to existing variable with overwrite the previous value
• int myNum = 15; // myNum is 15
myNum = 10; // Now myNum is 10
cout << myNum; // Outputs 10
Const keyword
• You can add the const keyword if you don't want others
(or yourself) to override existing values (this will
declare the variable as "constant", which
means unchangeable and read-only).
• To declare more than one variable of the same type, you can use a
comma-separated list:
• int x = 5, y = 6, z = 50;
cout << x + y + z;
Identifiers
• All C++ variables must be identified with the unique names. These unique
names are called identifiers.
• Identifiers can be short names (like x and y) or more descriptive names
(age, sum, totalVolume)
• It is recommended to use descriptive names in order to create
understandable and maintainable code.
• The general rules for constructing names for variables (unique identifiers)
are:
• Names can contain letters, digits and underscores
• Names must begin with a letter or an underscore (_)
• Names are case sensitive.
• Names cannot contain whitespaces or special characters like !, #, %, etc
• Reserve words (keywords like int) cannot be used as names.
Example program
• A program that takes two integer inputs from user and display its sum.
#include<iostream>
using namespace std;
main()
{
int x=0;
int y=0;
int sum=0;
cout<< “enter the value for x”; //insertion operators <<
cin>>x; //extraction operators >>
cout<< “enter the value for y”;
cin>>y;
sum= x+y;
cout<<“sum of x and y is”<<sum;
}
Variable scoping
• Variable is visible within the scope in which it is declared.
• Variable with same name cannot be declared within same scope;
however, this can be done if scope is different.
• Formal parameters have functional scope.
main()
{
int a=0;
int a=2; //error
}
Variable scoping –example 1
#include<iostream>
using namespace std;
//global variable
int x=4;
main()
{
int x=2; //local variable
cout<<x;
}
Output? How to access global variable?
Variable scoping –example 1
#include<iostream>
using namespace std;
//global variable
int x=4;
main()
{
int x=2; //local variable
cout<<x;
cout<<::x;
}
Output:
24
Class activity ( variables, data types, and input/output)
• Write a C++ program that takes following information of a citizen from
user:
• Citizen name
• CNIC number
• Address
• The input of address should separately be taken for house no., street no., block no., area,
city, province, and country.
• Also, take the information regarding whether the citizen is living in the specified address
or not.
• The program should display the details of citizen on screen where the
complete address should be shown in a single line.
References
• C++ How to Program
By Deitel & Deitel
• W3schools.com