Difference between revisions of "Variable"
(→Variable Types) |
|||
Line 10: | Line 10: | ||
=== int === | === int === | ||
<code>int</code> variables store an integer (non-fractional number) ranging from -2147483648 to 2147483647. They occupy 4 bytes of memory. | <code>int</code> variables store an integer (non-fractional number) ranging from -2147483648 to 2147483647. They occupy 4 bytes of memory. | ||
+ | === bool === | ||
+ | <code>bool</code> variables store a Boolean logic value (true or false). They occupy 4 bytes of memory. The bool type is used to control the operation of flow control statements, such as the if statement. | ||
== The const Specifier == | == The const Specifier == | ||
You can specify that a variable's value cannot be changed by declaring it like this: <code>const int blazeItFgt = 420;</code>. Doing so can protect your code from unintentional bugs and is generally considered a fucking good practice called 'const correctness'. Note that when you declare a variable as <code>const</code>, you need to initialize it during creation because you cannot assign to it afterwards. | You can specify that a variable's value cannot be changed by declaring it like this: <code>const int blazeItFgt = 420;</code>. Doing so can protect your code from unintentional bugs and is generally considered a fucking good practice called 'const correctness'. Note that when you declare a variable as <code>const</code>, you need to initialize it during creation because you cannot assign to it afterwards. |
Revision as of 15:09, 12 August 2016
Variables are like little boxes to store data in and read data from, typically numbers. What's in the box?!
Contents
How to Declare a Variable
You declare/create a variable by stating its type and then stating its name: int myVar;
. You can also give an initial value for the variable during creation using the assignment operator: int myVar = 69;
.
Variable Names
Variables names (the same as any other type of user-defined symbol) can contain any number, letter, and the underscore character. They cannot start with a number. Case matters (_DirtayBoi69
is not the same symbol as _dirtayboi69
).
Variable Types
int
int
variables store an integer (non-fractional number) ranging from -2147483648 to 2147483647. They occupy 4 bytes of memory.
bool
bool
variables store a Boolean logic value (true or false). They occupy 4 bytes of memory. The bool type is used to control the operation of flow control statements, such as the if statement.
The const Specifier
You can specify that a variable's value cannot be changed by declaring it like this: const int blazeItFgt = 420;
. Doing so can protect your code from unintentional bugs and is generally considered a fucking good practice called 'const correctness'. Note that when you declare a variable as const
, you need to initialize it during creation because you cannot assign to it afterwards.