How to Do C++ Programming

Let’s get into it!

To compile C++ code you will need a debugger, the 2 most well known ones are Borland and Visual Studio C++. You can find these using Google.

Content table:

1. Data types
2. Statements
2.1. If, else
2.2. For-loop
3. Functions
4. Code example

Like any other programming language, C++ uses data types to store values and information in. Here you can find a small list of the most used data types.

Int (integer value)
Float (decimal value)
Char (a character, contains text)
Bool (boolean, true or false)

I won’t discuss Vector and String here since there are endless possibilities with them and it would take quite some time to write all that down.

When you are coding and you want to store a value, you will have to know two things. First, what kind of data should the variable be able to handle? Second, what will the variable be called?

This is how you declare those four data types.

int ThisIsAIntegerValue;
float ThisIsADecimalValue;
char ThisIsACharacterVariable;
bool ThisIsABoolean;

As you can see you first write the data type, after that, it’s name, and then you have to finish the declaring with a “;”.

Now we have created a place we can put data in, but until now we still haven’t put any in.

int ThisIsAIntegerValue = 0;
float ThisIsADecimalValue = 0.0;
char ThisIsACharacterVariable = “test”;
bool ThisIsABoolean = false;

2. Statements

2.1 If, else

In the next topic I will show you how to write your first program based on these two statements.

Other then the else-statement, the if-statement can stand-alone and can only be used in one way. The else-statement can also be used in a combination of both statements. These are the two possibilities:

if (… ) { } else { }
if (… ) { } else if (… ) { } else { }

When you use the else-statement the two actions will never be executed at the same time. As you may have guessed, the code which should get executed when a certain action returns true has to be placed between the two brackets.

So now we know where to put the action but not yet what we can do where the points are situated. This is an example of how you would use it with a boolean.

bool test=false;
if ( test ) {
// test is true
/* test is true */
else {
// test is false
/* test is false

The “//” sign is used to place comments in your code, as you can see you can also use the “/ *” and “* /” sign. (Without the spaces) The above code is one way to check if a boolean is true or false. But to compare all data types there are built in signs you can use, here I give you a very small list:

== equal to
!= not equal to
> bigger then
< smaller then
>= bigger or equal
<= smaller or equal

2.2. For-loop

You can use this loop when you want a certain action to get executed several times, or use it to loop trough several variables. This is an example of how you use it.

The first word written down is “void” it states that the function does not return any value. And as you may already know, “main” is the name of the function. Now I’m going to show you an example which contains several new aspects.

int testFunction( int temp ) {
return temp; }

Now the first word written down is “int”, which refers to the integer value. It means that the function will retain an integer value. We do this by using the “return”, and in this example we return “temp”. “temp” is our input, which we get when the function is called. This is an example of a normal function call without a return value:

int test = 0;
testXFunction( test );

As you can see our input is now “test” which contains the value 0. Now when we would use a function with an integer return value and want to store the output of that function. Then our calling might look like this:

int test = 1;
int output = testFunction( test );

If testFunction is the function I posted above, then output will be set equal to test, which is 1.

4. Code example

float sum( float input1, float input2 ) {
float temp = input1 + input2;

return temp; }

void main ( ) {

float var1, var2;

float outputValue;

cout << float="" input="" p="" value="">
cin >> var1;

cout << float="" input="" p="" value="">
cin >> var2;

outputValue = sum( var1, var2 );

cout << outputvalue;

You should be able to read and understand both functions. The only new things I haven’t explained yet basically are “cout”, “cin” and “endl”. The cout stands for output, you can display text, variables and every other kind of data types with it. The second one (cin) is used to write input to variables. And the endl cmd just takes a new line after your text/variables have been displayed. You should mess around with some of those small codes to try understand the code better and learn from your own mistakes.

Author: Emanuelle Bublee