EA programming tutorial - operators

1 compound statement.
A compound operation (section) consists of one or more statements, which are included by braces and parentheses ({}). Note that semicolon (;) is not required after the parenthesis. For example:
if (x==0)  {
Print("invalid position x=",x);

2 expression
An expression ends with a semicolon (;), and a semicolon (;) is also an operator representing the end of a statement. For example:
i = 4;
I = a = 7; / / error, two or more equal signs (=) cannot appear in an expression at the same time.

Function call statements, such as functionname ("a");
//A function called functionname was called with the parameter value of string "a".

3 break statement
The reach statement causes the program to jump out of the nearest, while, for, switch statement. If you have completed the required work in a loop statement, you can use the break statement to jump out of the loop. For example:
for ( a=0; aif ( array[a]==0 ) break;

The meaning of this code is to traverse the array array. If one of the values in the array is found to be 0, it will jump out of the loop.

4 continue

Continue is used to end the execution of the current loop statement body and go directly to the next loop. It can be used in for and while loop body sentences. For example:
int a = 0;
for(int j = 0; j < 10; j++) {
if ( array[j] <= 0) {
/*If continue is encountered, the program will enter the next loop and will not execute the a + = array [J] statement again. Note that it does not jump out of the loop body at this time*/
a += array[j];

Note the difference between continue and break. Continue directly enters the next cycle of the loop body, while break directly jumps out of the loop body.

5 return
The return statement ends the execution of the current function and returns a value to the calling program, or no value is returned. For example:
int CalcSum(int x, int y) {
//When the function is executed here, the return value is x + y; the following statements will not be executed.
Int i = x * y; / / the statement cannot be executed.

If you want to control the function to end directly, just use the return statement.

6 conditional statement if else

For example:
if (a == 0) {
else if (a < 0){
Print ("less than 0");
else  {
Print ("greater than 0");

The explanation of the above code is: if the value of a is 0, the string "a" will be printed; if a < 0, it will be printed "less than 0"; otherwise, "greater than 0" will be printed.

7 switch statement

Examples are as follows:
int x = 1
Switch(x) {
case 1:
Print ("case 1"); when the value of / / X is 1
case 2:
case 3:
Print ("case 2 or 3"); the value of / / X is 2 or 3
Print ("not 1,2,3"); / / the value of X is not 1,2,3
In switch, if the program does not encounter the break statement, or continues to execute until it encounters a break; or the program block ends.

8 loop statement while, for
The following statement
int k = 1;
int n = 10;
while ( k < n )  {
}//Will print from 1 to 10.

for(int n = 0; n < 10; n++)
Print (n); / / will print from 0 to 9

    Was this article helpful?

    2 out of 0 found this helpful