Next: , Previous: , Up: Tips and Tricks   [Contents][Index]


7.2 Proper use of conditionals

coNCePTuaL supports two forms of conditional execution: conditional expressions (see Arithmetic expressions) and conditional statements (see Conditional execution). From the perspective of code readability and “thinking in coNCePTuaL” it is generally preferable to use restricted identifiers (see Restricted identifiers) to select groups of tasks rather than a loop with a conditional as would be typical in other programming languages. For example, consider the following code in which certain even-numbered tasks each send a message to the right:

FOR EACH evtask IN {0, ..., num_tasks-1}
  IF evtask IS EVEN /\ evtask MOD 3 <> 2 THEN
    TASK evtask SENDS A 64 BYTE MESSAGE TO TASK evtask+1

While the preceding control flow is representative of that in other programming languages, coNCePTuaL can express the same communication pattern without needing either a loop or an explicit conditional statement:

TASK evtask SUCH THAT evtask IS EVEN /\ evtask MOD 3 <> 2 SENDS A 64
BYTE MESSAGE TO TASK evtask+1

One situation in which conditional statements do not have a convenient analogue is when a program selects among multiple disparate subprograms based on a command-line parameter:

func IS "Operation to perform (1=op1; 2=op2; 3=op3)" AND COMES FROM
"--function" OR "-f" WITH DEFAULT 1.

ASSERT THAT "the function must be 1, 2, or 3" WITH func>=1 /\ func<=3.

IF func = 1 THEN {
  Perform operation op1.
}
OTHERWISE IF func = 2 THEN {
  Perform operation op2.
}
OTHERWISE IF func = 3 THEN {
  Perform operation op3.
}

Next: , Previous: , Up: Tips and Tricks   [Contents][Index]

Scott Pakin, pakin@lanl.gov