Why should we declare a variable in Programming

There are two types of quantities in variable declaration:

  • One is Constant whose value remain unchanged.
  • One is a variable quantity whose value can be change.

We know that if we want to perform a task or any calculation in computer system the must be some memory area where the task is going to stored.

If we think logically if we want to build a home we must have some area or plot. Without plot we can not build home.

Similarly, if we are going to perform any calculation in computer we must have some area from memory.

So variable is a quantity whose value can be change during the program execution.

Variable declaration can be defined as:

The process of specifying variable name and its type is known as variable declaration.

Variable name is known as identifier which is used to identify the nature of variable.

Variable type means the type of data which is going to store in variable i.e. integer, character ect.

There are many type of data we are going to discuss some of them for example

variable declaration
variable declaration

Integer Datatype

For whole numbers or integers we have a datatype “int”

How to declare an integer variable?

int num;

The above statement declares a variable num of type integer which stores integer variable.

Float Variable

This datatype stores fractional numbers.

float num;

The above statement declares a decimal number of name “num”.

Variable initialization

The process of specifying variable name, its type and value of variable as well is known as variable initialization.

int x = 10;

Int means variable type is integer.

“x” is variable name.

“=” is the assignment operator.

10 is the value of x.

Variable declaration is very important in programming.

Let’s see an example.

Write a program that declares a variable in c++ programming of type integer then displays its value.

#include”iostream.h”  

#include”conio.h”

void main()

{

int n;

n=10;

cout<”Value of N = “<<n;

getch();

}

Write a program that initialize a variable in c++ programming of type float then displays its value.

#include”iostream.h”

#include”conio.h”

void main()

{

int n = 10;

cout<<”Value of N = <<n;

getch();

}

You can learn more about programming from blow programming exercises.

If Statement Programming Exercise

Looping Structure Exercise

 

 

Leave a Reply