-
Notifications
You must be signed in to change notification settings - Fork 2
Control Structure
break
is used to exit from a for
or while
loop, bypassing the normal loop condition.
Example:
while(1){
if(input(1) == 1) {
output(1, idx);
idx = (idx + 1) % 64;
} else {
break; // exit the loop
}
sleep(2);
}
continue
skips the rest of the current iteration of a for
or while
loop. It continues by checking the conditional expression of the loop, and proceeding with any subsequent iterations.
Example:
The following code outputs the string "i > 2" only if the value of i > 2.
for(i = 0; i < 5; i++) {
if(i < 3) {
continue;
}
putstr("i > 2");
}
}
The if…else
allows greater control over the flow of code than the basic if
statement. An else
block will be executed if the condition in the if
statement results in false. The else
can proceed another if
test, so that multiple, mutually exclusive tests can be run at the same time.
Each test will proceed to the next one until a true test is encountered. When a true test is found, its associated block of code is run, and the program then skips to the line following the entire if/else construction. If no test proves to be true, the default else block is executed, if one is present, and sets the default behavior.
Note that an else if block may be used with or without a terminating else block and vice versa. An unlimited number of such else if branches are allowed.
Syntax:
if (condition1) {
// do Thing A
}
else if (condition2) {
// do Thing B
}
else {
// do Thing C
}
The for
statement is used to repeat a block of statements enclosed in curly braces. An increment counter is usually used to increment and terminate the loop. The for
statement is useful for any repetitive operation, and is often used in combination with arrays to operate on collections of data.
Syntax:
for(initialization; condition; increment) {
// statement(s);
}
Parameters:
- initialization: happens first and exactly once.
- condition: each time through the loop, condition is tested; if it’s true, the statement block, and the increment is executed, then the condition is tested again. When the condition becomes false, the loop ends.
- increment: executed each time through the loop when condition is true.
Example:
The following code outputs the numbers 0 to 4
for(i = 0; i < 5; i++) {
putnum(i);
}
Continue the program execution at the point specified by the label.
Syntax:
label:
goto label; // Continue the program at label
Example:
for(i = 0; i < 100; i++) {
for(j = 0; j < 100; j++) {
if (input(0) == 0) {
goto exit;
}
// more statements ...
}
}
exit:
// more statements ...
mC supports values as goto labels according to Labels as Values. This allows to implement a switch/case similar behaviour.
import "sys/stdio.asm"
import "sys/math.asm"
func init() {
setstdout(1); // use terminal windows for stdout
putchar('\b'); // clear entire screen
}
// Goto jump table
static var arr[] = {&&mark1, &&mark2, &&mark3, &&default};
func foo(idx) {
idx = min(idx, 3);
while(1) {
goto *arr[idx];
mark1:
putstr("Mark 1\n");
break;
mark2:
putstr("Mark 2\n");
break;
mark3:
putstr("Mark 3\n");
break;
default:
putstr("Default\n");
break;
}
}
func loop() {
foo(0);
foo(1);
foo(2);
foo(3);
foo(4);
sleep(5);
}
The if
statement checks for a condition and executes the following statement or series of statements if the condition is "true".
Syntax:
if (condition) {
//statement(s)
}
Example:
if(a == 0) {
putstr("ok");
}
if((a > 2) {
putstr("ok");
}
if((a == 0) || (b == 2)) {
putstr("ok");
}
Terminate a function and return a value from a function to the calling function, if desired.
Example:
func test(a) {
var d = 4;
if(d > a) {
return 0;
}
return 15;
}
A while
loop will loop continuously, and infinitely, until the expression inside the parenthesis, () becomes false. Something must change the tested variable, or the while loop will never exit. This could be in your code, such as an incremented variable, or an external condition, such as testing a sensor.
Syntax:
while (condition) {
// statement(s)
}
Example:
var i = 1;
while(i < 32) {
c = get_char(i);
output(0, c);
i = i + 1;
}