Parts: Declaring Variables Manipulating Variables Creating Arrays Summary (And Preview) Part 1 - Declaring Variables:
Note: You should add a semi-colon after every statement in C++. Spaces in C++ are usually used for clarification. In C++, you must declare a variable's type before you assign it a value (Note: cariable names my include both numbers and letters).
If you try to give a variable a value without declaring the variable's type, when you try to compile and run the program, you will recieve an error.
The types of variables are:
int,
unsigned int,
long,
unsigned long,
float,
double,
char,
string,
and bool.
Here are the definitions of each type:
int - An integer (not a fraction) that can be positive, negative, or 0.
unsigned int - A counting number that's only non-negative.
long - An integer that is very long (in some editors it is the same as int).
unsigned long - A nonnegative integer that is long.
float - A real number that takes up less memory than double but is less accurate in calculations.
double - A real number that is accurate during calculations and that may be very large; many times larger than an int variable.
char - A single character that is kind of like a string with one character.
string - A string of characters.
bool - A logical value: true of false
Here are a few examples of how you would declare variables and their values:
Example 1: int x; x = 5; or int x = 5;
Example 2: char a; a = "q"; or char a = "q";
Also, in strings, there are a few special things you may add in them:
\n - Start a new line.
\t - Tab
\0 - Null
\\ - Backslash
Part 2 - Manipulating Variables:
Here are some symbols you may or may not be familiar with
(2 means for 2 arguments, 1 means for 1 argument): + (1) Does nothing - (1) Retuens the negative of an argument ++ (1) +1 -- (1) -1 * (2) Mutiplication / (2) Division % (2) Modulo + (2) Addition - (2) Subtraction
Assignment Types:
= Means a=b
*= Means a=a*b
%= Means a=a%b
+= Means a=a+b
-= Means a=a-b
Logical Operators:
== Tests to see if the argument on the left is equal to the argument on the right
!= Tests to see if the argument on the left ISN'T equal to the argument on the right
> Greator than
< Less than
>= Greator than or equal to
>= Less than or equal to
&& Tests if the arguments on both sides are true
|| Tests if at least one of the arguments on one of its sides are true
! Tests if its argument is false
Example:
int myVariable1 = 10; int myVariable2 = 11; bool x; b = (myVariable2 != myVariable2);
The example above would make b be true. Part 3 - Creating arrays Arrays must have their type be declared too. Here is an example array: char myArray[] = {'H','I'}; To access a specific element in the array (let's use "H" for the example), you would do:
myArray[0] If you want the same element to be repeated in the whole array, you could do something like: float myArray[10] = {2.0};
That would make all 10 sections of myArray have the number 2.0. Part 4 - Summary: That should cover the BASICS of variables. I'll probably make another tutorial that has more advnaced info on variables. Preview to Stuff in the Next Tutorial:
To display strings, you would do:
cout << "string"; So to display a variable and a string, you could do: string yourName = "Bob"; cout << "Your name is" << yourName; To let the user of the program input sometthing, you do: cin >> myVariable;