Ubuntu, ‘Helvetica Neue’, Arial, Helvetica, Verdana, sans-serif;font-size:12px;height:100px;line-height:16.1875px;margin:0 0 15px;outline:0;overflow:hidden;padding:0;vertical-align:baseline;”>Wibben

C++ Basics



Ubuntu, ‘Helvetica Neue’, Arial, Helvetica, Verdana, sans-serif;font-size:12px;line-height:16.1875px;margin:0 0 36px;outline:0;padding:0;text-align:justify;vertical-align:baseline;”>C++ is great for large performance-sensitive p
rojects or for coding desktop applications. It’s much easier to screw up with this language, but the provided functionality and possibility becomes quite useful for said projects. Plus, the sheer number of libraries is neat, I have barely used a tiny fraction of them myself. Just as a side note I made this to specifically to help people pass coding 1 and maybe 2 (if I have the time).

First all normal syntax in c++ ends in “;” with exceptions of functions.


Data Types


There are many data types in c++, and the ones that you will most likely use are:

    int – used to declare an integer variable, you can have an array of characters by declaring int NAME[length]
    char – used to declare a character variable, you can have an array of characters by declaring char NAME[length]
    string – used to get a bunch of chars as one variable, you can have an array of characters by declaring char NAME[length]
    float and double – used as precision integers, they can go into decimal places, arrays of them also available


The int main() and Basic Headers


Whenever you write a c++ program, you should always have

Code:
#include <iostream>
using namespace std;

This is because they are the basic input, output, and other built in functions, without them, the program would have no idea what you are talking about, and therefore never be able to do anything. Also, all c++ programs have this:

Code:
int main {
return 0;

}

int main is the main function of the program, and it is where the program basically starts it’s actual functioning and starts on its work.

Basic Operations


First and foremost, you need the input and output functions, cin and cout respectively. Sample code:

Code:
cin >> variable;
cout
<< variable << "Use quotes for hard-coded things" << endl;

Note that you must always use “>>” for cin and “<<” for cout, and the endl at the end of the output statement creates a new line.

IF and SWITCH


Now you should be ready to learn if and switch statements.

Code:
if(condition) {
//what happens when condition is true
} else if(other condition) {
//what happens when the other condition is true
} else {
//what happens when no conditions are true
}

switch (expression)
{
case condition:
//what happens when no conditions are true
break;
case other condition:
//what happens when no conditions are true
break;
default:
//default group of statements
}

The if statement is basically saying “if this is true or this happens, this other thing in the curly brackets happen”
The switch statement is basically another type of if statement.

FOR


For loops allow you to execute a command a number of times, count, or course through an array.
Code:

Code:
for(variable=starting_point; variable<ending_point; variable++) {
//do something each time
}

the “<” before the ending_point could be changed to <=, >=, or > according to the function of the loop. Also, the ++ could be changed to += any_number, –, or -= any_number. Another thing you should keep in mind is that the “variable<ending_point” could be ignored entirely to create an infinite loop, something you might or might not want depending on what you feel like doing. An example of a for loop with int i:
Code:

Code:
for(i=0; i<100; i++) {
cout
<< i << endl;
}

This loop starts at 0, then goes all the way to 99 (<100), going up by 1.

WHILE


Now for the while and do-while statements:

Code:
while (condition) {
//stuff here
}

do {
//stuff here
} while(condition);

while first checks whether a condition is true, if it is, it executes the stuff and then checks if the condition is true again. The do-while statement first does the actions THEN checks if the condition is true. Now that I’ve covered pretty much the conditional statements, it time to get to the for loop, which is what you are going to use for coding 1.

Hello World example

Code:
#include <iostream>
using namespace std;

int main()
{
cout
<< "Hello World!";
return 0;
}

What Do You Think on This ? Say Here