Hi all, a lot of people asked me to write a tut on C and C++ so i thought i would to get you guys started... also I'ld like to point out i've a bit of a time constraint
while writing this so ill keep it brief.
Firstly what is C and C++:
C and C++ are both programming languages used to write software(.exe's).
Why should i learn C or C++:
a lot of people I've met have asked me whats the point in learning it if all i can do is print a few letters to a console...
the point of learning it is that once you've learned the basic stuff you can modify existing code to improve it, build your version, etc... once you do that for a while you can start to understand how it works. Soon you will be able to start from the ground up...
Whats the difference between C and C++:
C is basically what most of us use, C++ is OOP(Object Orientated Programming) based, it uses classes amongst other things, so far i have yet to find a advantage to classes when i could just use the standard C method of doing it...
overall i prefer C to C++ Wink
What do i need to code C or C++ programs:
You need an IDE(Integrated Development Environment), you can get a free one here:
http://sourceforge.net/project/downl..._mirror=belnet yeah i already hooked you up with a direct download so you could start straight away Wink...
Ok lets get down to explaining a few things....
Firstly the entry point for all C or C++ console based programs is 'main()', main usually has to return something when it ends, this is usually success or failure,
this is represented by a number, usually 1 or 0, so we now have 'int main()' meaning it returns type int, a number...
Secondly we have variable's... all hold a different kind of data, the point of variables is that say when we want to print a random message box to the screen, we would have a variable to hold a few random strings, when your program runs it it chooses a string from variable and displays it, hopefully from that you can see the concept behind them...
Ok so what kind of variable types are there, well, there are quite a lot but I'm gonna show you the fundamental ones that your use the most...
these are the mostly used variables:
char letter = "y" // i hold characters
int number = 2; // i hold numbers
float decimal = 56.40; // i hold decimal numbers
firstly we have 'char', this is used to hold characters or strings made out of letters or numbers...
then we have 'int' which is used to hold integers(numbers), note that 'int' variables DO have a limit as to how high they can go, but its pretty high so i don't think your need to worry about it atm...
finally we have 'float' this variable is basically the same as int except it can go hold decimals...
Ok ill show you some code to demonstrate this stuff..
using namespcae std; // so we can use 'cout' instead of 'std::cout' which takes longer
int main() // our programs entry point
{ // opening brace 'main'
cout<<"hello world; // C++ function for printing text to the console
// remember we had 'int' main()... so we make our program return an int, in this case 0
return 0;
} // closing brace for main()
Ok that was a simple example of printing output to the screen(console), you may of noticed the ';'(semicolon) at the end of some lines, this denotes the end of a function or similar, most compile errors for beginners are caused by a missing semicolon or other. You may of also noted the '//' this is a C++ style comment, which means your compiler skips it, your compiler is what turns your code into .exe format, it also aids in finding bugs in your code...
there's a difference between a C++ style comment and a C style comment, a C++ style comment looks like this '//' and is only one line long, where is a C style comment looks like this "/* C comment */" and can comment more than 1 line, '/*' denotes the comments opening tag, and '*/' denotes its closing tag(or closing brace).
Ok i think its time to introduce loops, there are a few different kind of loops,
we have the for loop and the while loop, there are also a few more like do while etc but there basically the same the above loops, i want to keep this simple so ill only explain the basic ones Wink
A for loop is used to do something a certain amount of times, for example, instead of telling you how leet i am ill just do this:
for(int i = 0; i < 10; i++) {
cout<<"Im leet";
}
Ok, ill explain the above code... you already know what an int is so i wont explain that... basically the above code prints "Im leet" into the console 10 times, you may of noticed the '<' ill explain these now...
'<' = less than
'>' = more than
'!' = not
'==' = equal to
'||' = or
'%' = remainder
that may be a bit confusing but your get used to it, trust me, now the above can be combined, we could us '!=' which would mean not equal to... so what about while loops? these are conditional loops, while condition is true loop...
they look like this:
while(true) // this will loop forever because the condition is always true
{
Sleep(5); // pauses the loop for 5 milee seconds to stop CPU usage from reaching 100%
cout<<"this is a while loop";
}
thats what a basic loop looks like, that isnt a very good example so ill show you a complete example:
using namespace std;
int main()
{
int number = 0; // create number variable of type int
for(number = 0; number < 5; number++) // number++ means 'number' will be incremented by 1 each time through the loop, the loop will terminate when number reaches 4(less than 5)
{
cout<<"all your base are belong to me";
}
while(number != 25) // while number not equal to 25 loop
{
cout<<"looping";
number++; // increment number by 1 through each cycle of the loop
// loop will exit after 25 cycles because number will equal 25 which means
// the while loops condition is false, so exit loop
}
return 0; // return success
}
Hopefully you understood that Wink, compile and run it, experiment with it, learn from it...
What if you wanted something to happen if number was equal to 30 ?
To do this we need if statements, they look like this:
if(condition == true)
{
// do something
}
else // do something else
{
// do something else
}
That was a simple if statement, however it gets harder, you can have nested if statements aswell, ill show you an example, this may be a little but hard to understand but ill explain it in the code:
using namespace std;
int main()
{
int i = 0;
char done;
while(true) // infinite loop
{
cout<<"\nEnter a number: ";
cin>> i; // prompts the user to enter the value of 'i', so if they put in 5 then 'i' will = 5
cout<<"\nValue of i is: "<<i<<"\n"; // prints the value of i to the console
if(i == 5)
{
cout<<"\nyay, correct answer\n"; // '\n' denotes a newline
cout<<"\ndo you want to continue? y for yes, n for no\n";
cin>>done;
if(done == 'y')
{
cout<<"\nok were continue you\n";
// the loop would continue even if i didnt include this line, but its good to make understandable code
continue; // restart loop
}
else
{
cout<<"\nok bye\n";
break; // exit from loop
}
}
else
{
cout<<"\nWRONG!!\n";
continue; // restart loop
}
} // closing brace for while loop
return 0;
} // closing brace for main
Remember, run, experiment with, improve, learn

...
Ok i'm getting tired now, but ill write a little bit more...
switch statements, switch can be useful say if your writing a quiz program, it looks nicer than loads of if statements... lets see an example:
using namespace std;
int main()
{
char answer; // a charecter varible to hold our anwser
for(int i = 0; i < 2; i++) // for loop, i more than or equal to 2
{
cout<<"what does 'cout' do: \n";
cout<<"a: print output to the console\n";
cout<<"b: accept input\n";
cout<<"c: loop\n";
cout<<"Answer: ";
cin>>answer; // prompt for input
switch(answer) // our switch statment begins here
{
case 'a': // if anwser = 'a'
cout<<"\n\nCorrect\n\n";
Sleep(1000);
system("cls"); // use batch command to clear screen
break;
case 'b': // if anwser = 'b'
cout<<"\n\nWrong\n\n";
Sleep(1000);
system("cls"); // use batch command to clear screen
break;
case 'c': // if anwser = 'c'
cout<<"\n\nWrong\n\n";
Sleep(1000);
system("cls"); // use batch command to clear screen
break;
default: // if anwser = anything other than the above
cout<<"\n\nInvalid anwser\n";
Sleep(1000);
system("cls"); // use batch command to clear screen
break;
}
}
// they tried more than twice, game over
cout<<"\n\nYou lost, sorry\n\n";
Sleep(2000); // give them time to read the message before exit
return 0;
} // closing brace for main
K, iv'e gone through loops, ifs, switches, variables + a little more... i intentionally left room for improvements in the above code, just a little project for you to work on... possible improvements, 1. add more questions. 2. make it ask random questions each time it runs. 3. give it a high score mechanism...