A conditional statement is a statement that execute different statements depending on the value of a boolean expression.
If:then
The if:then statement used in previous sections is an example of a conditional statement. The code below shows a sketch of a definition of if:then as a control method.
if(cond: var boolean):then{thenPart:< Object}:
...
As mentioned, if:then is a built-in primitive control method in qBeta and cannot be defined in terms of other language mechanism.
Defining if:then:else
The if:then:else is an example of a control method that may be define as method in qBeta. You may find the following definition in the qBeta library:
if(cond: var boolean):then{thenPart:< Object}
:else{elsePart:< Object}:
if (cond) :then
thenPart
leave(if:then:else)
elsePart
The control method has the following structure:
- The name of the control method is
if:then:else. - It has three parameters,
cond, which is aboolean, thenPart, which is a virtualObject, andelsePart, which also is a virtualObject. - The parameters are specified using the fat-comma syntax as described in section .
- The first item in the main-part of the abstraction is an if-then statement.
- If the boolean
condis true then the virtualthenPartis executed followed byleave(if:then:else),which terminates the execution ofif:then:else. - If the boolean
condis false then the virtualelsePartis executed.
Consider an invocation of if:then:else:
if (amount <= balance) :then
balance := balance - amount
:else
console.print("The balance is less than the amount")
Execution of the if:then:else takes place as follows:
- The expression
(amount <= balance), the statementbalance := balance - amount, and the statementconsole.print("The balance is less than the amount")are the actual parameters (arguments) of the invocation ofif:then:else. - The boolean expression
amount <= balanceis evaluated and assigned to the parametercond. - The statement
balance := balance - amountis a further binding of the virtualthenPart. - The statement
console.print("...")is a further binding of theelsePart.

