RosettaCodeData/Task/Conditional-structures/Verbexx/conditional-structures.verbexx

71 lines
2.3 KiB
Plaintext

@VAR a b = 1 2;
// -------------------------------------------------------------------------------------
// @IF verb (returns 0u0 = UNIT, if no then: or else: block is executed)
// ======== (note: both then: and else: keywords are optional)
@SAY "@IF 1 " ( @IF (a > b) then:{"then:"} else:{"else:"} );
@SAY "@IF 2 " ( @IF (b > a) else:{"else:"} then:{"then:"} );
@SAY "@IF 3 " ( @IF (a > b) then:{"then:"} );
@SAY "@IF 4 " ( @IF (b > a) then:{"then:"} );
@SAY "@IF 5 " ( @IF (a > b) else:{"else:"} );
@SAY "@IF 6 " ( @IF (b > a) else:{"else:"} );
@SAY "@IF 7 " ( @IF (b > a) );
// ---------------------------------------------------------------------------------
// ? verb (conditional operator)
// ====== ( 1st block (TRUE) is required, 2nd block (FALSE) is optional)
@SAY "? 1 " ( (a < b) ? {"1st"} {"2nd"} );
@SAY "? 2 " ( (a > b) ? {"1st"} {"2nd"} );
@SAY "? 3 " ( (a < b) ? {"1st"} );
@SAY "? 4 " ( (a > b) ? {"1st"} );
// -----------------------------------------------------------------------------------
// @CASE verb
// ==========
//
// - executes code block for first when: condition that evaluates to TRUE
//
// - normally, ends after running that code block
//
// - if no when: conditions are true, executes else: code block (if present)
//
// - can exit a when: block with @CONTINUE case: verb -- causes @CASE to continue
// looking for more true when: blocks or the else: block
@VAR n = 0;
@LOOP times:3
{
@SAY ( "n =" n " @CASE results:"
( @CASE
when:(n == 0) { "n == 0(1)" }
when:(n == 0) { "n == 0(2)" }
when:(n == 1) { "n == 1(1)"; @CONTINUE case: }
when:(n == 1) { "n == 1(2c)" }
else: { "else" }
)
)
;
n++;
};
/] -----------------------------------------------------------------------
Output:
@IF 1 else:
@IF 2 then:
@IF 3 0_u0
@IF 4 then:
@IF 5 else:
@IF 6 0_u0
@IF 7 0_u0
? 1 1st
? 2 2nd
? 3 1st
? 4 0_u0
n = 0 @CASE results: n == 0(1)
n = 1 @CASE results: n == 1(2c)
n = 2 @CASE results: else