Introduction to Computing Lab 8: Understand Datatypes and Variables in C++
LAB # 8
To understand the Data Types and Variables
Objective
Learn to write programs declaring different variables and data types
To learn statements for providing input and displaying output
Theory
Variable:
A variable is a named memory location that can hold various values. All variables must be
declared before they can be used. A variable‟s declaration serves one important purpose that it
tells the compiler what type of variable is being used. Variable declaration means that you are
giving an identity to the variable, which is to be used in the program.
Basic Data Types of C/C++ language:
C++ support four basic data type described below:
Table 1: Data type Description
Integer Signed whole number Int
Float Floating point number Float
Character Single character (symbol) Char
String More than one characters String
Data Type Description:
Character:
It is one bit long and it is most commonly used to hold a single character. A variable of type char
can also be used as a “little integer” if desired. A character constant is either a single alphabet, a
single digit, or a single special symbol enclosed within single inverted commas. For example
„A„, „0„, or „&„. It requires 1 byte in the memory for storage.
Integer:
Integer may hold signed whole number (Number with no fractional part). It may hold values in
the range –32768 to 32767.
1
Introduction to Computing Lab 8: Understand Datatypes and Variables in C++
Float:
Data type float hold signed floating-point values, which may have fractional components. Float
can hold very large values.
Variable Declaration:
Variable are generally declared as:
Type var-name;
Here „Type‟ is C data type (e.g char, int, float etc )and „var-name‟ is the name of the variable.
You can also declare more than one variable of same type by using a comma-separated list. For
example
int a,b,c ;
Value Assignment to the Variable
A programmer can assign a unique value to the variable. The general form of an assignment
statement is
Variable name = value;
For example;
X = 1000;
Example Programs
Program 1:
This program declares a variable and assigns it a value.
#include<iostream>
Using namespace std ;
main (void)
{
int number;
number = 1000;
cout<<"The value is "<< number<<endl;
}
2
Introduction to Computing Lab 8: Understand Datatypes and Variables in C++
Output
The result of the above program will be:
The value is 1000
Program 2:
This program declares different type of variables and prints their assigned values.
#include<iostream.h>
Using namespace std ;
main (void)
{
int event = 5;
char heat = 'c';
float time = 27.25;
cout<<"The winning time in heat "<< heat <<" of event was "
<< event << " is "<< time<<endl;
}
Output
The result of the program will be:
The winning time in heat c of event 5 is 27.25.
Variable Declaration and Definition:
Variables are used to hold values in memory. All variable must be defined before their usage.
C++ provides list of primitive data types including int, float, char, double and bool. Variable
definition has the following syntax.
Syntax
Type var1; / var1, var2, var3, …, var4;
Example
int age;
float gpa, per, pi;
3
Introduction to Computing Lab 8: Understand Datatypes and Variables in C++
Input Statement:
The cin statement allows the user to enter data from the keyboard. The cin statement allows the
user to enter a number or a single character. The cin statement consists of the keyword cin
followed by the extraction operator (>>) followed by a variableName where the input is to be
stored then ended by a semicolon (;). It has the following syntax.
Syntax
cin >> VariableName;
Consider the following program which prompts a user for the age and then returns his/her age in
days.
#include <iostream> //default compiler selected
Using namespace std ;
main (void)
{
int age;
cout << “Enter your age: “;
cin >> age; //age variable created earlier
cout << “Age in days: “ << age*365 << endl;
}
Compile and run the program and note down the output.
In C++, decisions to execute or not execute a statement or group of statements can be made by
using the statement if (expression). The expression must be an expression that can be evaluated
as either true or false. If it evaluates to true, a succeeding statement or group of statements
enclosed in { . . . }, called a block statement will be executed.
Syntax
if ( expression )
statement;/ block
NOTE: block will be of statements, instructions or may have other if statements.
4
Introduction to Computing Lab 8: Understand Datatypes and Variables in C++
Compile and run the following program and note down the output.
#include <iostream>
Using namespace std ;
main (void)
{
int age;
cout << “Enter your age: “;
cin >> age;
if (age <= 0)
cout << “Invalid age” << endl;
}
Control Structures
The “if-else” statement:
Indentation and format:
The general form of the „if-else‟ construct is as follows:
if (expression )
statement;/block
else
statement;/block
For clarity and making a program easy to understand, each „else‟ should be indented the same
amount as its matching „if‟.
Compile and run the following program and note down the output.
#include <iostream>
Using namespace std ;
main (void)
{
int age;
cout << “Enter your age: “;
cin >> age;
if (age <= 0)
cout << “Invalid age” << endl;
else
cout << “Welcome to Hamdard University” << endl;
}
5
Introduction to Computing Lab 8: Understand Datatypes and Variables in C++
Lab Task
Lab Task. 8.1) Write a program that inputs, three numbers and find the greatest numbers among
these numbers.
Sample Output
Enter First Number: 2
Enter Second Number: 3
Enter Third Number: 5
The greatest number is 5
Lab Task. 8.2) Write a program that swaps the value of two variables using a third variable
Sample Output
Variable1: 5
Variable2: 9
After swapping
Variable1: 9
Variable2: 5
Lab Task. 8.3) Write a program that checks whether the entered year is a leap year or not
Lab Task. 8.4) Write a program that checks whether the entered number is positive or negative
Note: Attach with manual every above mentioned task.