Wednesday, February 25, 2009

Declaring Variables

To use those variables, we need to declare them first.
Syntax is :

Data_type Variable_name;

Examples:
  • Declaring a variable, which stores the number of students in a classroom.

int NumStudents;


  • Declaring a variable, which stores the price of an item in a convenience store.

double priceofItem1;


Some of you might be asking, example 1 : why int? why can't we use double too? Isn't 1 and 1.0 the same?
Of course both are the same, but we need to specify the most suitable data type for different variables. In example 1, the variable NumStudents stores the number of students, and there is no such thing as "Oh, my class has around 35.6 students."

  • Declaring a variable which stores the gender of the a student.


char studentGender;


I hope by now, everything should be clear about declaring variables.

No comments: