2.42. 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 and gotos is possible.

Start felix section to tut/tutorial/tut-1.42-0.flx[1 /1 ]
     1: #line 4233 "./lpsrc/flx_tutorial.pak"
     2: //Check conditional:procedural
     3: #import <flx.flxh>
     4: 
     5: inline proc f(x:int) (y:int) {
     6:   print "NOT ONE"; endl;
     7:   if x == 1 do
     8:     print 1; print " ";
     9:     if y == 20 goto twenty;
    10:     if y == 10 do print "TEN"; else done;
    11:   elif x == 2 do
    12:     print 2; print " ";
    13:     if y == 20 do
    14: twenty:>
    15:       print "TWENTY";
    16:     done;
    17:   else print "Dunno .. ";
    18:   done;
    19:   endl;
    20: }
    21: 
    22: f 1 10;
    23: f 1 20;
    24: f 1 40;
    25: f 2 20;
    26: f 3 30;
End felix section to tut/tutorial/tut-1.42-0.flx[1]
Start data section to tut/tutorial/tut-1.42-0.expect[1 /1 ]
     1: NOT ONE
     2: 1 TEN
     3: NOT ONE
     4: 1 TWENTY
     5: NOT ONE
     6: 1
     7: NOT ONE
     8: 2 TWENTY
     9: NOT ONE
    10: Dunno ..
End data section to tut/tutorial/tut-1.42-0.expect[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 felix section to tut/tutorial/tut-1.42-1.flx[1 /1 ]
     1: #line 4279 "./lpsrc/flx_tutorial.pak"
     2: //Check conditional:functional
     3: #import <flx.flxh>
     4: 
     5: proc f1(x:int) {
     6:   if x == 1 then { print 1; endl; }
     7:   else {} endif;
     8: }
     9: 
    10: proc f2(x:int) {
    11:   if x == 1 do print 1; endl; done;
    12: }
    13: 
    14: f1 1;
    15: f2 1;
End felix section to tut/tutorial/tut-1.42-1.flx[1]
Start data section to tut/tutorial/tut-1.42-1.expect[1 /1 ]
     1: 1
     2: 1
End data section to tut/tutorial/tut-1.42-1.expect[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 felix section to tut/tutorial/tut-1.42-2.flx[1 /1 ]
     1: #line 4311 "./lpsrc/flx_tutorial.pak"
     2: //Check conditional:folding
     3: #import <flx.flxh>
     4: macro val x = 1;
     5: if x == 1 do val y = 1; else val y = "ONE"; done;
     6: print y; endl;
End felix section to tut/tutorial/tut-1.42-2.flx[1]
Start data section to tut/tutorial/tut-1.42-2.expect[1 /1 ]
     1: 1
End data section to tut/tutorial/tut-1.42-2.expect[1]