1.40. More on Conditionals

Of course conditional statements can be nested.

Also, like C, you can jump into conditionals, although this practice isn't recommended. This is because conditionals are just shorthand for a web of labels and conditional gotos, so adding extra labels can gotos is possible.

Start C++ section to tut/examples/tut149e.flx[1 /1 ]
     1: include "std";
     2: 
     3: inline proc f(x:int) (y:int) {
     4:   print "NOT ONE"; endl;
     5:   if x == 1 do
     6:     print 1; print " ";
     7:     if y == 20 goto twenty;
     8:     if y == 10 do print "TEN"; else done;
     9:   elif x == 2 do
    10:     print 2; print " ";
    11:     if y == 20 do
    12: twenty:>
    13:       print "TWENTY";
    14:     done;
    15:   else print "Dunno .. ";
    16:   done;
    17:   endl;
    18: }
    19: 
    20: f 1 10;
    21: f 1 20;
    22: f 1 40;
    23: f 2 20;
    24: f 3 30;
    25: 
End C++ section to tut/examples/tut149e.flx[1]
Conditionals are primarily a convenient shorthand for conditional expressions with procedural arguments. Conditional expresssions always have an else part, so that f1 and f2 below are equivalent:
Start C++ section to tut/examples/tut149f.flx[1 /1 ]
     1: include "std";
     2: 
     3: proc f1(x:int) {
     4:   if x == 1 then { print 1; endl; }
     5:   else {} endif;
     6: }
     7: 
     8: proc f2(x:int) {
     9:   if x == 1 do print 1; endl; done;
    10: }
    11: 
    12: f1 1;
    13: f2 1;
    14: 
End C++ section to tut/examples/tut149f.flx[1]
You can see we avoid the messy 'else {}'.

Conditionals may contain declarations. However the bodies are not blocks, and the declared symbols are not local to the conditional bodies.

The macro processor can fold conditional statements, in particular it can choose between two declarations.

Start C++ section to tut/examples/tut149g.flx[1 /1 ]
     1: include "std";
     2: macro val x = 1;
     3: if x == 1 do val y = 1; else val y = "ONE"; done;
     4: print y; endl;
     5: 
End C++ section to tut/examples/tut149g.flx[1]