This page include:

C++ Basic Structure

Well, some things you must never forgot are:
1. Every command in c++ needs ";" in the end.
2. Every function could be void which mean she doesnt return a value.

Lets see the structure of a c++ program.

//Area to define the headers files(*.h)

#include < file1.h >
#include < file2.h >
..
#include < filen.h >

//Area to define the headers of user functions

example: Myname();

//Area to define variables,constants and main code.

void main()
{
define of vars.consts..
instructions..
}

//Area to write the code of the user functions.

example: Myname()
{
code of this function
}


Variables of C++

name of variableAccepted Values
Char-128..+127
Int-32768..+32767
Long-2147483648..2147483648
Float3.4x10^-38..3.4x10^38
Double1.7x10^-308..1.7x10^308
Long Double3.4x10^-4932..3.4x10^4932
Unsigned Char0..255
Unsigned Int0..65535
Unsigned Long0..4294967295


Special characters

C++ has some special characters, they are:
Special characterUsage
\a Sound
\b Backspace
\f Change page
\n Change line
\t Tab
\\ print of \
\' print of '
\" print of "

You can use S.C. with this instruction:
cout<<"\n"; to change line or
cout<<"The prize is: \n"; in both of instructions the program will change line at the end.


Objects and functions

C++ uses objects the most famous objects are "cout" and "cin". Cout it is used to display messages or variables, cin it is used to take values from the keyboard. For everything you want to prompt or to take from the keyboard you must use the character "<<" for cout and ">>" for cin. Example:
cout<<"Hello c++";
cout<<"Hello"<<"my name"<<"is"< The first instruction prompt Hello c++ and the second instruction prompt Hellomynameis and the value of the variable name.Example for cin:
cin>>a>>b; or you can use
cin>>a;
cin>>b;
But dont forget the characters ">>" and "<<".

With c++ you can build your own functions but lets see the two most usefull functions of c++:
The clrscr() function which is used to clear the screen and
the getch() function which is waiting to press a key before the program continues.


Operators and main symbols

There also operators in c++. Two of them are:
the operator endl which is used to change line and the
operator setw() which is used to manage space of display. Example of endl is: cout<<"Hello"<<endl; or you can use cout<<"Hello \n"; An istruction with setw should be like that: cout<<setw(5)<<"Ok"; that will take 5 characters of display and it will use the two of 5 to prompt the ok. But the "Ok" will be written from the right to the left so the above instruction will have this output:
1234567
Ok

Where 123..7 are the collumns of the display. So we dont use setw() to put spaces but to enchain some of the positions of the display. If someone wants to put spaces he have to use this instruction:
cout<<" ";

Here are the most used symbols of C++:
SymbolUse of symbolHow it work 1How it work 2
=equivalencek=100;k=100;
+additionk=k+a;k+=a;
-subtractionk=k-a;k-=a;
*multiplicationk=k*a;k*=a;
/divisionk=k/a;k/=a;
%remainderk=k%a;k%=a;
++addition by onek=k+1;k++;
--subtraction by onek=k-1;k--;

Notes:
1)In the division both of the variables must be of the same type.Example if you want to make this action:
k=k/a;
you should define in the variable section:
int k,a; or
long k,a; or
type of variable k,a;
2)There no exists these symbols:
x//; or
x**; or
x+++; or
x----;



Control structures


Symbols

There are used two of them:
the if - else and
the switch.
But first lets see the symbols of controlling the programming flow:
SymbolMeaning
>Bigger than
>=Bigger or equal than
<Less than
<=Less or equal than
==equal than
!=Not equal than

Also:
SymbolMeaning
&&AND
||OR
!NOT

Notes:
1)What mean AND? AND is a logikal action. It mean that if you have two conditions then both of them must be true.
2)OR. If you have two conditions one of them must be true.
3)NOT. If you have two conditions then none of them should be true.
Dont worry if you dont understand about logikal actions we 'll examples with them.


If

The structure of If:
If (condition)
{
instructions..
}
Lets see an example:
If (k > 1)
{
cout<<"K is bigger than 1";
}
What this if do? First is checking the condition if it is true, this mean that k is bigger than 1, it prompt the message. If the condition is false the programm continue the execution.

If - else

The structure of If - else:
If (condition)
{
instructions..
}
else
{
instructions..
}
In this structure if the condition is false then the programm execute the instructions inside the else.

If - else if

The structure of If - else if:
If (condition 1)
{
instructions..
}
else if (condition 2)
{
instructions..
}
else
{
instructions..
}
Well this is the final version of If. Now at every else we have the opportunity to add a condition but we can also use the simple else.


Switch

The structure of switch:
switch(variable)
{
case value_1: instructions;break;
case value_2: instructions;break;
default: instructions;
}
The switch structure is used when we want to execute diferent instructions for diferent values of a variable.But we must know the diferent values that the variable can take otherwise we can use the -default-.This mean that if none of the selected cases match the value of the variable, will be executed the default section instructions.
lets see an example:
cout<<"give a number";
cin>>numb;
switch(numb)
{
case 0: cout<<"The number you give is zero.";break;
case 1: cout<<"The number you give is one.";break;
default: cout<<"The number you give is bigger of one";
}
In the example first we give the value of a variable.If we give 0 then will executed the instructions of case 0.If we give 1 the instructions of case 1 and so on.If we didn't give 1 or 0 will executed the instructions of default.The instruction break is used when we want to go out of the switch structure.If we not used break,it will executed and the three cout.


For

In many cases we want to repeat instructions.We can do that with For structure, While and Do while.
The for structure is:
for (initial value of counter;condition;change the value of counter)
{
instructions..
}
An example..
for (k=1;k<5;k++)  //This mean for the initial value of k(k=1)
//and while k is less than 5, repeat the instructions and in every
//repetition add 1 to k.
{
cout<<k;
}
The output to the display would be this:
1234


While

While is used as For to repeat instructions.But in while we dont know how many times we will repeat the instructions.
The structure of while:
while(condition)
{
instructions
}
Example:
k=10;
while(k<6)
{
cout<<k;
k--;
}
Well what this example does? The answer is nothing! Why? Because initial value of k is 10 and while is executed when k is less than 6.


Do while

Do while is used when we dont know the number of repeat the instructions but we want the instructions in the structure to executed at least one time.
The structure of  Do while:
Do
{
instructions
}
while(condition);
Example:
k=10;
do
{
k=3;
cout<<k;
k--;
}
while(k<6);
This example shows an no-end loop.What happen? The initial value of k is 10. When the program run the do-while k is 3.So it prints k(3).After k is being 2.The condition says:execute until k bigger than 6.But when the instructions will repeat k will be again 3! This mean that k will never be bigger than 6.The program will stop only when we will press the reset button of our computer.. So stay away from loops like that.


Other structures

Matrix


How we can define a matrix in c++? With this instruction:
type of variables name of matrix[number of elements];
Example: int mat1[10];
We define a matrix who have ten elements integer type.We must never forgot that the first element of the matrix is that with index zero.(i.e. mat1[0]).We cannot use negative numbers for index and we cannot use mat1[4.5] or mat1[8/6] the index of the matrix must be always integer.For more informations go to C++ examples page.

Tsamantero , [email protected] , C++ Basics
Time to go home?
Back to main page