Structured Programming
Currently the language supports the following constructs of the structured programming
- Functions
- Loops
- Conditionals
Functions have been described in the Functions section of the Language reference.
The loops constructs have been implemented through for
loops, and conditionals
construct have been implemented with the if-else
.
Semantics of the for
construct are as follows
- for loops can be defined only inside functions or compound statements.
- defining a counter variable inside the for is optional, any variable in its scope can be used as a counter variable.
- condition expressison of the for must always evaluate to a boolean value.
- step condition in the for is also optional.
Semantics of the foreach
construct are as follows
- foreach loops can be defined only inside functions or compound statements.
- defining a variable inside the foreach is mandatory, it's type will be inferred on it's own, just the name of the variable is enough.
- as keyword is also mandatory.
- array name is mandaotry after the as keywords.
- foreach only works on arrays.
Semantics of the if-else
construct are as follows
- if-else can be defined only inside functions or compound statements.
- condition expressison of if-else must always evaluate to a boolean value.
- writing an else on the tail of if is optional.
For Loops
consume fn printint(int i) void;
#Declaring and initializing the array
int[10] a = [10,20,30,40,50,60,70,80,90,100];
fn start() void {
#Iterating over and printing an array using for-loop
int i = 0;
for(; i < 10; i = i + 1){
printint(a[i]);
}
return;
}
note
The printint
function in the exapmle above, is a Native C function call,
for more on Native C calls look at the Consume section
For Each Loops
consume fn printint(int val) void;
consume fn printdouble(double val) void;
fn start() void {
int[5] a = [1,2,42,45,2];
foreach(myvar as a){
printint(myvar);
}
printint(myvar);
double[6] b;
foreach(dp as b){
printdouble(dp);
}
return;
}
If-else
consume fn printint(int i) void;
#Declaring and initializing the array
int[10] a = [10,20,30,40,50,60,70,80,90,100];
fn start() void {
#Iterating over and printing an array using for-loop
int i = 0;
for(; i < 10; i = i + 1){
# print only five actual values of the array elements
# otherwise print 0
if(i < 5){ printint(a[i]); }
else { printint(0); }
}
return;
}