How to resolve the algorithm Conditional structures step by step in the Verbexx programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Conditional structures step by step in the Verbexx programming language
Table of Contents
Problem Statement
List the conditional structures offered by a programming language. See Wikipedia: conditionals for descriptions. Common conditional structures include if-then-else and switch. Less common are arithmetic if, ternary operator and Hash-based conditionals. Arithmetic if allows tight control over computed gotos, which optimizers have a hard time to figure out.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Conditional structures step by step in the Verbexx programming language
Source code in the verbexx programming language
@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
You may also check:How to resolve the algorithm SEDOLs step by step in the C++ programming language
You may also check:How to resolve the algorithm Dijkstra's algorithm step by step in the Haskell programming language
You may also check:How to resolve the algorithm Boolean values step by step in the ALGOL W programming language
You may also check:How to resolve the algorithm Catalan numbers step by step in the Standard ML programming language
You may also check:How to resolve the algorithm Continued fraction/Arithmetic/Construct from rational number step by step in the Racket programming language