Using Variables
So far you should be able to write a simple program to display information typed in by you, the programmer and to describe your program with comments. That's great, but what about interacting with your user? Fortunately, it is also possible for your program to accept input.
But first, before you try to receive input, you must have a place to store that input. In programming, i
nput
and data are stored in variables. There are several different types of
variables; when you tell the compiler you are declaring a variable, you
must include the data type along with the name of the variable. Several
basic types include char, int, and float. Each type can store different
types of data.
A variable of type char stores a
single character, variables of type int store integers (numbers without
decimal places), and variables of type float store numbers with decimal
places. Each of these variable types - char, int, and float - is each a
keyword that you use when you declare a variable. Some variables also
use more of the computer's memory to store their values.
It may seem strange to have multiple variable types when it seems like
some variable types are redundant. But using the right variable size can
be important for making your program efficient because some variables
require more memory than others. For now, suffice it to say that the
different variable types will almost all be used!
Before you can use a variable, you must tell the compiler about it by
declaring it and telling the compiler about what its "type" is. To
declare a variable you use the syntax ;. (The brackets here indicate
that your replace the expression with text described within the
brackets.)
Here are some variable declaration examples:
int x;
int a, b, c, d;
char letter;
float the_float;
Comments
Post a Comment