Basic C / C++

In this tutorial I will go over some of the basics of programming in c++ (which can be easily modified into C)

First we will start out with some basic computer programming. And first of all you will need a compiler ( which is what you type your code into ). I use Borland, but you can get Dev C++ for free.

 

But before we get to programming, you need to know some terminology. The first term is “int” this means integer. So if you want “number” to be a integer, you type “int number”.

But “int” does have boundaries. And number more than (I think) 30000 will circulate all the way to -30000. so if you want a number bigger than 30000 stored, you can use “unsigned int” which makes “int”s boundaries 0 through 60000 instead of -30000 through 30000, or “long int” which is “int”, but with bigger boundaries. The same goes for “short int”, which is int, but smaller. “float”, which can go into decimals, also applies to these rules.

 

 

Now we get to the programming. For now, always include “#include <iostream>” at the top of your code. This lets you use std::cout and std::cin . Also, every program must have a main() function in it. This is where your program will start. Later, I wll describe what functions are, but for now, we will only use 1 function, main(). Here is the code:

 

#include <iostream>

 

int main()

{

Std::cout << “hello world” ;

Int a;

Std::cin >> a;

Return 0;

}

 

Analsys:

Line 1: includes the functions std::cout and std::cin

Line 5: displays “hello world” on your screen

Line 6&7: makes sure that your program will stay open until you press a number and hit okay

Line 8: returns 0, really does nothing but it has to be there

 

see attachments for the .cpp (C++) file and open this in your compiler and run it

 

Check for updates as I go deeper into C++ with more specific tutorials

 

If people find flaws with any of my tutorials, please PM (message) me at dellagd in the robot forum.

 

 

if statement

a IF statement consists of the word "if" then an expression (ex. (a <2)) then an open bracket ({), code, then a closing bracket (}). Like this:

 

if (b < 5)

{ std:cout << "b is less than 5"}

 

Now if b is > 5,  "b is less than 5" is not displayed. it is skipped over and the code keeps going. But usually if b is > 5, you still want it to do something. In that case, you would do this:

 

 

if (b < 5)

{ std:cout << "b is less than 5"}

else

{ std:cout << "b is greater than 5"}

 

So now if b is greater than five, it will display "b is greater than 5". What this code says is that if b is less than 5, display "b is less than 5". Anything else, display "b is greater than 5".

 

once again, see attachments for the attached code.

 

Check back for more tutorials!

while statament

 in this branch of my C++ series, I will show how to use while functions. A while function will be a loop until its parameter ( like the (b < 5) part of a if statement) evaluates false, in which it will exit the while loop and continue with the code. Example:

 

int b = 1;

 

 

while (b <= 10)

{

std::cout <<  " b is less than 10 ";

b++;

}

std::cout <<  "b is now greater than 10";

 

Analysis:

line 1: declares and sets b to 1 in one shot

line 4: declares that there will be a while statement following and the parameter is "b is less than or equal to 10"

line 6: displays " b is less than 10 "

line 7: increments b by 1. So every time the program sees b++, it will add 1 to b (only in C++, not C. For c you will have to use "int temp; temp = b + 1; b = temp;"

line 9: once b is 11 or greater, it displays "b is now greater than 10"

 

This statement can also be very useful if you want to have a infinite loop:

 

while (1)

{}

 

or

 

while (1 == 1)

{}

 

check back for more C++ tutorials.