As the basis of the programming language used in LabPP_Automat widely known, simple and flexible C++ language.
The advantages of this language are:
- high speed execution;
- irrigating the readability, conciseness and understandability of the code;
- easy to create and access the functions;
- large library of different procedures.
- its development is much easier and it is more convenient to work.
For convenience, the use of language constructs in LabPP_Automat added string data type.
If You set the MyStringFunc function that returns a string value, it is possible to write so:
string MyString = MyStringFunc();
Here we declare a variable of type "string" MyString immediately recorded in it the value from the function MyStringFunc().
There are limitations in which the creation of programs is greatly simplified:
1. you can only use built-in classes using descriptors.
2. there are no pointers.
3. missing data type "reference".
Software modules are created as separate text files.
Function main()
Each file should contain basic main () function.
The following is an example main() function with a call to another function.
int main()
{
// Comment
/* Big comment
on multiple rows */
string s; // - declare variable "string" type
int i; // - declare variable integer type
double d; // - declare variable with floating point
char c; // - declare variable type char
i = 12; // assign 12 to variable i
i++; // increment i;
i--; // decrement i;
string mystr;
mystr = "my "+"string";
myfunc(1, 0.2, mystr); // call to function
}
// Declare own function
int myfunc(int ii, double dd, string str)
{
return 0; // return value 0
}
Looping
Loop for
for(int i=0;i<10;i++) // to execute the code enclosed between
{ // the braces until i is less than 10 and, in each execution of the loop, i increment by 1
cout << i; // print the value of i in the message window LabPP_Automat
if(i==5) // if i is equal to 5 to execute in the fragment between the braces.
{
break; // to break the cycle
}
}
Loop do-while
int i;
i=0;
do {
cout << i; // Displayed in the message window LabPP_Automat the value of i
i++; // Increment i
} while (i<10) // Run a loop as long as i is less than 10
Loop while
int i;
i=0;
while(i<10) {
cout << i;
i++;
}
Logical operations
== Equality is True if the number or string on the left is equal to the number or the string right
!= Inequality is True if the number or string on the left is NOT equal to the number or the string right
< Less True if the left number is less than the number on the right
<= Less than or equal to True if the left number is less than or equal to the number on the right
> More is True if the number on the left is greater than the number on the right
>= Greater than or equal to True if the left number is greater than or equal to the number on the right
&& Logical And
If the expression on the left and right are true the result is true. If at least one of the expressions to the left or right is false, then the result is false
|| Logical OR If at least one expression on the left and right are true the result is true. If both expressions are false, then the result is false.
Conditional branches
Operator if
if(i<0)
{
cout << "is true";
}
Construction if-else
if(i<0) // If the expression in parentheses is true, execute first fragment
{
cout << "is true";
}
else // If the expression in parentheses is false, it executes the second fragment
{
cout << "is false";
}
Construction if-else if-else
if(i<0) // If the expression in parentheses is true, execute first fragment
{
cout << "i<0";
}
else if(i==0) // If the expression in parentheses is true, it executes the second fragment
{
cout << "i=0";
}
else // If none of the expressions in brackets is not true (else if points can be a lot), there is one final fragment
{
cout << "i=0";
}
Operator switch
Depending on the value of the number in the parentheses is executed the corresponding program fragment.
switch(i)
{
case 1:
cout << "i=1";
break;
case 2:
cout << "i=2";
break;
case 10:
cout << "i=10";
break;
default:
// statements executed if any other value of i
}
Additional files
The #include Directive
If you have functions or variables that are used in multiple programs, one can be placed in the so-called "header files". Usually the header file, to distinguish from the others, is given the extension .h (header for example.h).
Before you give the program to execute, it collects in a single unit by the preprocessor.
Here is an example of the file header.h
/***********/
/* header.h */
/***********/
int iGlobalValue=0; // so you can declare a global variable
// here is an example of a function that can be used in various programs
double iMyCommonFunction(double var1, double var2)
{
return var1*var2;
}
Here's a sample program using the header file header.h
/**************/
/* my_prg1.cpp */
/**************/
#include "header.h" // to include the header file header.h
int main()
{
cout << iMyCommonFunction(100, 50); // the function is described in the header file header.h
}
Variables
Types of variables
The interpreter supports the following variable types:
char
int
double
string
The visibility of variables
Variables can be local, global, and superglobal.
Local variables inside the function are seen and not seen outside the function.
Global variables are visible inside any function, regardless of depth of nesting.
Global variables are declared before the body of main () function.
Superglobal variables are those that exist during the entire session AddOns.
They can be used to transfer data from one program to another.
The following snippet initialisere variable my_var line:
/***********/
/* prgfile1.cpp */
/***********/
int main() {
int ires;
ires = var_extern_set("My Gloabal Variable Name", 100);
}
And this piece is another program (run by another button) and reads the value of the variable my_var:
/**************/
/* prgfile2.cpp */
/**************/
int main()
{
int ires;
string extern_var;
ires = var_extern_get("My Global Variable Name", extern_var);
cout << "ires=" << ires << " my_var = " << extern_var;
}