Welcome again to some more fun coding experiences.
Well, last week we learned how to print stuff to the console using an action such as:
cout << "Hello" << endl;
We also learned how to do some math using our cout action, such as:
cout << 5 + 3 - 1 << endl;
So, what now?
Variables...or in this lesson int.
int is short for integer.
When we use it in C++, it MUST ALWAYS be lower case. If you do not put it in lower case, your compiler will give you an error. int commands also go inside the curly brackets.
So, what can we do with int
, you ask?
We store variables/numbers.
We use a very simple coding structure to declare integers in c++:
{
int number;
}
So, we declared the int, number. You can use other words and numbers, but you cannot declare an int with a name starting with a number. It can have letters and numbers, but letters must come first.
So, we declared a variable. What now? Well, let's use our nifty cout action to display it on the console.
We would do something like this program:
#include <iostream>
#include <stdlib.h>
int main( int argc. char *argv[])
{
int number; // we are declaring the int
cout << number << endl; // we are using cout to display the value of number
system("Pause");
return 0;
}
DO NOT RUN THAT PROGRAM JUST YET!
So, we declared our variable, number, with the int command. We then used our cout action to print the value of number to the console. But, I did not use double quotes. That is because, since you declared number as an int(integer/variable), the system knows that and will display what number equals much like our math we did last lesson.
Now, you are also wondering what the // are all about. That is just a way to comment your code. You can comment your code in C++ by using //. The compiler sees it is a comment and does not use it in the program. This can be helpful to keep your programs neat and clean and understandable for others. No one on a team of coders wants to handle someone else's messing code. You can help keep it clean by simply adding comment to your code.
Well, moving on.
You see I told you not to run that program. I hope you did not. You see, I declared the variable,but I never set it equal to something. If you run that, you never know what happens. Sometimes you would get a weird random number. That's because int is storing a variable at a point in your memory/ram.
If you do not have a value for it, C++ simply uses what was there earlier before int took over. I ran a program like that once, I forgot to set my variable equal to a value and it crashed my computer. Remember to always set your variables equal to a value.
Now, int variables can only be equal to numbers. So, let's set our int to a value of 24(lol, my highschool jersey number for basketball). We would use the following commands:
{
int number;
number = 24; // we declare that the int number is equal to 24
}
So we did a simple math equation so to speak and set number equal to 24.
CAUTION:Whenever you set something equal to something else, whatever is on the left side is being set equal whatever is to the right!
What now?
Let's use our handy cout action once again. Insert the following code in the curly brackets of your program*I am using curly brackets here, do not add them since you already have them*:
{
int number; // we declare the int, number
number = 24; // we are declaring that number = 24
cout << "Theforeshadower's jersey number in highschool was " << number << endl;
system("PAUSE");
return 0;
}
I know you already have questions about what I did with cout.
Using cout, I printed onto the console "Theforeshadower's jersey...", but I continued with another set of <<. That's because I do not want to have to make another cout action. You can use the shortcut and after the first print in cout, add a space then a set of << and you can print some more things to the console.
If you put in the code properly and compiled and ran it, you should have this:
Theforeshadower's jersey number in highschool was 24
Press any key to continue...
So, now you know how to declare an int and how to give it a value. You also got a bonus of how to comment your code.
Practice using the int command. Have multiple int commands in a simple program using cout.
Later, we will begin using some more wonderful math with these int.
Until then.
EDIT:
I forgot something pretty neat you can do with declaring an int.
Instead of making a new line of code to set number = 24, you can do it like this:
{
int number = 24;
}
C++ allows you to set your variable to a value in this line because number is already set as an int before you set it equal to 24. Makes coding a little shorter if you are dealing with multiple variables.
Also, you can declare more than one int in a line of code. Instead of this:
{
int number;
int number2;
int number3;
}
We can do this:
{
int number, number2, number3;
}
That would be the same as what I typed before, just shorter and in one line..It is not necessary, but keeps your code cleaner I guess and shorter. You can also set them equal in the same line:
{
int number = 24, number2 = 1, number3 = 15;
cout << number << number2 << number3 << endl;
You can declare the value of the integers in the same line. And I just used cout to shoe you that you can also print multiple variables in one line of code. Now, if you ran a program with the above, there would be no spaces. You would have to have a << " " << between each or the numbers would be altogether.
END EDIT.
Have fun coding!
~Theforeshadower
PS Fell free to send me your programs via email
theforeshadower@gmail.com