Saturday 4 April 2020

Declaration of Variables in C++


How to Declare the Variables in C++? or What is the Declaration of Variables?
C++ allows the declaration of a variable anywhere in the scope. This means that a variable can be declared right at the place of its first use.  This makes the program much easier to write and reduce the errors. 

Example: 
int main()
{
float x;
float sum = 0;                                 //declaration
 for(int i=1; i<5; i++)                       //declaration
{
                       cin>>x;
                       sum = sum + x;
}
float average;
              average= sum/i;                                 //declaration
              cout<<average;
              return 0;
}

No comments:

Post a Comment