Controls:

It is imposible to create program without loops and conditions. Here we explain what you can use.


if/else

Below you can see simple if condition. Logic assign "OK: One is ok" to msg variable if variable a is 1. Remember that this is only example, you can put any expresion in if condition.

if(a = 1) {
  msg = "OK: One is ok";
}

If control wouldn't be complete without else condition, below is exmaple which assign "ERR: Only one is ok" to msg variable if a value isn't 1

if(a = 1) {
  msg = "OK: One is ok";
} else {
  msg = "ERR: Only one is ok";
}

while

While loop will repeat everything which is in body till condition if true. Below we have example of loop which run 10 times

i = 0;
while(i<10) {
 //do something 
 i++;
}

for

For loop is another way to repeat some action, first argument is executed before we enter loop, second argument is condition of loop, till it will be true, then loop will run, last argument is executed every each loop run. Below we have example of for loop which run 10 times

for(i=0; i<10; i++) {
  //do something
}

break

To be able to more control loops behaovior we can use break to break loop and leave body. In example below "//do something" will run only 2 times, as we break in 3th loop run

for(i=0; i<10; i++) {
  if(i=2) break;
  //do something
}

continue

Another loop controle is continue, which stops loop run and start next interaction. Below we have example when "//do something" will run only 9 times, as in 3th loop run will skip

for(i=0; i<10; i++) {
  if(i=2) continue;
  //do something
}

labels

Labels are used in more advanced loop logic, when we are in more then one loop and we want to break or continue parent loop. Below example for breaking for loop from inside of while loop. We can also use continue like below.

mainloop:
for(i=0; i<10; i++) {
  j = 0;
  while(j<10) {
    //do something
    if(j=5) break mainloop;
    j++;
  }
  //do something
}

return

To break function run, we can use return anytime we want. It will stop run function and return value. If function is void then we can use it without 1st argument.

int divide(int a,int b) {
  if(b=0) {
    console("Can't divide by 0");
    return 0;
  }
  r = a / b;
  return r;
}