Welcome to the third tutorial of the series. Before reading this tutorial it is absolutely vital you read the previous tutorial, number 2.
In this tutorial we will cover:
- Creating variables and data types
- More syntax
- Commenting your code
Part I: Creating the Project
Since not all of you may remember from the last tutorial we are going to go back through creating a project. First, open up Code::Blocks;
Now, into the File menu, highlight the "New" area and select Project;
A window should popup in the foreground. Simply press the Next button. For obvious reasons I won't repeat this instruction anymore. Assume to continue through the dialog as per normal an installation.
In the next window simply select C++.
Now, make sure that your Project creation directories are correct. Call this project "Statements";
You will not need to change any of these Build options, as per last time. Simply press Finish.
Part II: Changing the defaults
After waiting a short while for your project to be created, expand the tree view on the side and open main.cpp as shown in the last tutorial. Your window should now look like this;
Because we don't need the default code, delete the single line that prints Hello World to the console. You will now have an empty main() { ... } loop with a simple return statement. If you are unsure of what to do, just make your code look like what is in the picture;
Our aim for this tutorial is to print some simple maths. Before we do this, I need to explain the data types used in C++. To save memory programmers can choose what type they want a variable to be when they create it. Because of this, all variables have to be pre-declared. I'll explain this later.
void empty data
int integer, from negative 32000 to positive 32000
float floating-point number, can be ignored until later
double a number with decimals, eg: 1.3456 char
a string (words), eg: "Fred"
bool
either a one or a zero, eg: true
The main value type that you will always use is the integer kind. The rest aren't really important to remember and you will pick them up along the way.
Variables, like algebra, can be declared and given names. Here is an example;
Code:
// Create the integer myvariable
int myvariable;
// Set it to 5
myvariable = 5;
Have you noticed the braces? When you see a double brace in C++ it means a comment. When entered into the code editor it will turn green. Only that after a set of braces will not be executed so a command like this is still valid;
Go back to Code::Blocks. We are going to use variables to add numbers together and print to the screen. This may sound complicated at first, but if you just play around with the values you will easily understand it.
Type the following code into the editor as so. Make sure it goes beween the main loop and before the return statement (follow the format of the picture);
Code:
// Create the variable number
int number;
// Set it to 2
number = 2;
// Show us what happens when we add it to itself
cout << number + number << endl;
Now, by using the Build and Run option from the Build menu (as explained in the last chapter). If you are unable to locate it just follow this screenshot;
Wait a few seconds for the build to be completed, and then your program should pop up. It might be hard to see, but up in the corner the program has worked wonders and added 2 + 2 = 4.
Part III: Coding your own maths applications
From here stems a whole world of possibilities. Before letting your mind roam free, I'm going to quickly outline some vital C++ operators. You will have to remember these for later as they are the vital organs of programming in all languages.
2 + 2 = 4, addition
2 * 2 = 4, multiplication
2 - 2 = 0, subtraction
2 / 2 = 1, division
These should be somewhat similar to what you enter into your graphics calculator.
Try the following samples to get a further idea;
Multiplication
Code:
int number;
number = 2;
cout << number * number << endl;
Division
Code:
int number;
number = 2;
cout << number / number << endl;
Addition of several variables
Code:
int number, number2;
number = 2;
number2 = 15;
cout << number + number2 << endl;
Finding the sqaure of a number
Code:
int number;
number = 2;
cout << number * number << endl;
You can now execute simple programs which find things like the areas of sqaures, circles and all kinds of mathematic procedures. Don't forget to use a double value when storing decimals, although this is not needed for hardcoded values;
Code:
int radius;
radius = 2;
cout << (radius * radius)* 3.142 << endl;
Mixing words and numbers
Code:
cout << "The circle can hold " << apples << " apples before it explodes" << endl;
Don't forget you will need to Build and Run your applications or your new code will not work.
I hope that you enjoyed this tutorial. As an extension activity you could try and find out how the other variables work. If you look back to chapter 2 there is an example usage of the char variable.
In the next tutorial we will cover:
- Revision
- Polish our syntax
- Cover some topics again, explaining in more detail
- Explaining the use of some of the symbols
Regards,
-Schyler-