Control Structures

Top  Previous  Next

Here are control structures supported by Tibbo BASIC and Tibbo C. Control structures use relational and logical operators.

 

If—then—else

Tibbo BASIC supports two distinctive versions of the if-then-else construct: single-line and multi-line.

The single-line version is the only construct in the entire Tibbo BASIC/C domain that must fit entirely on one line of text.

 

Tibbo BASIC:

if x>10 then s="Over the limit" x=10 else s="Within limit" 'notice how there are two statements for when x>10. This is totally OK!
 
if x>10 then s="Over the limit" : x=10 else s="Within limit" 'you may separate multiple statements with a colon; this is for your convenience -- TIDE doesn't care

 

C, of course also has its "single-line" if-then-else that doesn't even use the keywords if, then, and else. I used quotation marks around single-line because C doesn't really care about line breaks. It's just weird to have this construct spill onto more lines.

 

Tibbo C:

x=y<z ? y : z;

 

The multi-line if-then-else version is similar between Tibbo BASIC and C:

 

Tibbo BASIC:

Tibbo C:

if x=0 then
  s="At zero"
  sys.debugprint("x=0")
else if x=1 then
  s="At one"
else
  s="Other value"
end if

if(x==0){
  s="At zero";
  sys.debugprint("x=0");
}
else if(x==1)
  s="At one";
else
  s="Other value";

 

Select case / switch()

Differences between Tibbo BASIC and Tibbo C:

In BASIC, there is no need to end each case with a break. It's a must in C!

In BASIC, if you want to combine several values in one case use the syntax like this: case 2,3. If you write case 2: case 3: it will mean that case 2 has no code in it (an empty case). This is the opposite to how C has it.

The switch construct of C is much more efficient. It's realized as a "jump table", while select case of BASIC goes through each case: sequentially. On the other hand, BASIC allows you to define cases that are not constant in value, i.e. case 1+y:. Trying this in C would generate an error — only constant expressions are allowed! This is because the jump table must be built at compile time.

 

Tibbo BASIC:

Tibbo C:

select case x
case 0:
  s="At zero"
case 1+y: 'comparison with a non-constant expression
  s="At one"
case 2,3: 'this is how you combine several values in one case
  s="At two or three"
case 4:   'no code -- this is NOT combined with "case else", this is a separate case
case else:
  s="At something else"
end select

switch(x){
case 0:
  s="At zero"; break;
case 1:
  s="At one"; break;
case 2: //this is how you combine several values in one case
case 3:
  s="At two or three"; break;
case 4:
  break;
default:
  s="At something else"; break;
}

 

For—next / for()

Tibbo BASIC loops are "inclusive of the last value". If you write for f=0 to 4 this means that the loop will run 5 times (provided the step=1).

Let's see two examples: a simple loop that increments by one, and a loop with a custom step.

 

Tibbo BASIC:

Tibbo C:

'simple for-next with increment by 1
for f=0 to 4
  sys.debugprint(str(f))
  sys.debugprint("\x0A\x)D")
next f
 
'for-next with custom step; f must be a char (signed variable) for this to work properly
for f=10 to 0 step -2
  sys.debugprint(str(f)+" ")
next f

//increment by 1
for(f=0;f<=4;f++){
  sys.debugprint(str(f));
  sys.debugprint("\x0A\x0D");
}
 
//step=-2; f must be a char (signed variable) for this to work properly
for(f=10;f>=0;f-=2)
  sys.debugprint(str(f)+chr(10)+chr(13));

 

Do—loop / do—while()

There are four versions of this in BASIC: do until — loop, do — loop until, do while — loop, do — loop while.

Having the condition check on the front differs from having it on the back: do until — loop and do while — loop may not loop even once, while do — loop until and do — loop while will always go through the loop at least one time.

On the C side this construct has only one form: do—while(). With the check positioned on the back this loop will always run at least once.

 

Tibbo BASIC:

Tibbo C:

'do until--loop
f=0
do until f=10  
  f=f+1
loop
 
'do--loop until
f=0
do
  f=f+1
loop until f=10
 
'do while--loop
f=0
do while f<11  
  f=f+1
loop
 
do--loop while
f=0
do
  f=f+1
loop while f<11

f=0;
do{
  sys.debugprint(str(f)+" ");
  f=f+1;
}while(f<11);

 

While-wend / while()

BASIC and C implementations are identical. The check is on the front, so the loop may run zero times.

 

Tibbo BASIC:

Tibbo C:

f=0
while f<11
  f=f+1
wend

f=0;
while(f<11){
  f=f+1;
}

 

Exit for, exit do, exit while (Tibbo BASIC) and break (Tibbo C) statements

In BASIC, exit for, exit do, and exit while abort the current for—next, do—loop/while, or while-wend cycle.

In C, the break statement can abort the current for, do—loop, and while cycle.

Exit and break statements only terminate "their" loop. Outer loops are not affected:

 

Tibbo BASIC:

Tibbo C:

'Go through each row of a 2-dimensional array until the 'A' character is encountered
for f=0 to 1
  for f2=0 to 5
    if arr(f,f2)=`A` then
        exit for
    end if
  next f2
next f

//Go through each row of an array until the 'A' character is encountered
for(f=0;f<10;f++){
  for(f2=0;f2<10;f2++){
    if(arr[f][f2]=='A'){
        break; //will only break out of the inner "f2" loop
     }
  }
}

 

In C, the break statement is also used in the switch construct. It is actually an integral part of it.

I have already provided a code example above.

 

Exit sub and exit function statements (Tibbo BASIC)

Exit sub and Exit function allow you to finish with the execution of a sub/function at any place in its body.

The equivalent in C is the return statement, but it's use is only possible in non-void functions.

 

Tibbo BASIC:

Tibbo C:

sub process_command(cmd as string)
  if cmd="" then
    exit sub
  end if
...
...
end sub
 
function create_file(file_name as string) as boolean
  create_file=false
 
  if fd.format(1000,16)<>PL_FD_STATUS_OK then exit function
 
  if fd.mount<>PL_FD_STATUS_OK then exit function
 
  if fd.create("file_name")<>PL_FD_STATUS_OK then exit function
 
  create_file=true
end function

bool create_file(string file_name){
  if(fd.format(1000,16)!=PL_FD_STATUS_OK)
     return false;
   
  if(fd.mount()!=PL_FD_STATUS_OK)
     return false;
 
  if(fd.create("file_name")!=PL_FD_STATUS_OK)
     return false;
  return true;
}

 

The continue statement (Tibbo C)

For, do—loop, and while constructs of C can also use the continue statement. It skips whatever is left in the loop's body and jumps straight to the next loop pass.

No equivalent exists in Tibbo BASIC.

 

Tibbo C:

//Divide each element in an array by 2, but only if this division will have no remainder.
//This example is somewhat artificial -- the same result could be achieved without using the continue statement.
for(f=0;f<10;f++){
  if(arr[f]&0x01)
    continue;
     arr[f][f2]/=2;
}

 

And don't forget... the goto statement

Some argue that no sensible language (or developer) needs the goto statement, ever. Yet, the humble goto exists and persists.

In Tibbo BASIC and Tibbo C, labels must end with a colon (:), or they won't be recognized as such. This only applies to places where labels are placed, not to the goto statement itself.

 

Tibbo BASIC:

Tibbo C:

sub process_data
  if data_len=0 then goto error_handling
  if incoming_data(0)<>`S` then goto error_handling
  ...
  ...
  ...
  ...
  exit sub
 
error_handling: 'here when something is wrong
  ...
 
end sub

void process_data1(){
  if(data_len==0)
    goto error_handling;
  if(incoming_data[0]!='S')
    goto error_handling;
  ...
  ...
  goto exit;
 
error_handling: //here when something is wrong
  ...  
exit:  
}

 

The scope of labels is limited to the procedure (function) they are in. This is because you can't use the goto statement to jump from one procedure into another.

Consequently, you can use the same label name in multiple procedures.