Summary of Basic C++-Commands
Summary of Basic C++-Commands
Compiling
To compile a C++-program, you can use either g++ or c++.
For the following commands you can find at the end of this summary sample
programs.
Each command in C++ is followed by “;” . Carriage return has no meaning
in C++.
Comments
Essential for the writing of clear programs are comments, which are explana-
tions for your program, and which are ignored by the compiler.
/* . . . */ puts the text . . . into comment (more than one line possible)
// . . . puts text . . . for rest of same line into comment
Data Types
== = example: i1 == i2
!= 6= i1 != i2
> > i1 > i2
< < i1 < i2
>= ≥ i1 >= i2
<= ≤ i1 <= i2
&& and (i1 != i2)&& (i1 == i3)
|| and/or (i1 == i2) || (i1 == i3)
Statements:
if( condition ) {
statements
}
if( condition ) {
statements
}
else {
statements
}
if( condition ) {
statements
}
else if {
statements
}
else {
statements
}
switch ( casevariable ) {
case value1a:
case value1b:
{statements}
break;
case value2a:
case value2b:
{statements}
break;
default:
{statements}
}
Repetitions
while (conditions) {
statements
}
do {
statements these statements are done before the while statement check
} while ( condition) ;
functions
A function is a set of commands. It is useful to write a user-defined function,
i.e. your own function, whenever you need to do the same task many times in
the program. All programs start execution at the function main. For Functions
you need steps 1-3:
Step 1: At the beginning of your program you need to declare any functions:
function type function name (types of parameter list);
example 1: double feetinchtometer (int,double);
example 2: void metertofeetinch (double, int&, double&);
The function type declares which variable is passed back from the function
(void means none via the function type). The variables without “&” are all
input parameters, i.e. only passed to the function and are not changed within
the function. The variables with “&” may be passed both to and from the
function and may be changed in the function.
Arrays
Data Type Declaration (Example) Assignment (Example)
array int street[100]; street[0]=0; street[50]=7;
double matrix[7]; matrix[6]=3.2;
etc. etc.
multidim. array int lattice[10][10]; lattice[0][1]=1;
double wateruse[5][3][2]; wateruse[3][1][0]=1.2;
etc. etc.