পৃষ্ঠাসমূহ

.

Search Your Article

Total Pageviews

Monday, February 6, 2017

Rexx - Signals

In Rexx, the signal instruction is used generally for two purposes, which are −
  • One is to transfer control to another part of the program. This is normally like the go-to label which is used in other programming languages.
  • The other is to go to a specific trap label.
If the signal command is used in any of the following instruction commands, the pending control structures will automatically be deactivated.
  • if ... then ... else ...
  • do ... end
  • do i = 1 to n ... end [and similar do loops]
  • select when ... then ... ...etc. otherwise ... end
The general syntax of the signal statement is shown as follows −

Syntax

signal labelName  
   
signal [ VALUE ] labelExpression 
Let’s look at an example of how to use the signal statement.

Example

/* Main program */ 
n = 100.45 

if \ datatype( n, wholenumber ) then 
   signal msg 
   say 'This is a whole number' 
   return 0 
msg : 
   say 'This is an incorrect number'
The output of the above program will be as shown below.

Output

This is an incorrect number.
If you change the value of the variable n to a whole number as shown in the following program −
/* Main program */ 
n = 100 

if \ datatype( n, wholenumber ) then 
   signal msg 
   say ' This is a whole number ' 
   return 0 
msg : 
   say ' This is an incorrect number ' 
You will get the following output −
This is a whole number
One can also transfer to the value of the label as shown in the following program −
/* Main program */ 
n = 1 

if \ datatype( n, wholenumber ) then 
   signal msg 

if n < 1 | n > 3 then 
   signal msg  
   signal value n 
   3 : say 'This is the number 3' 
   2 : say ' This is the number 2' 
   1 : say ' This is the number 1' 
   return n 
msg : 
   say ' This is an incorrect number ' 
   exit 99 
The output of the above program will be shown as follows −
This is the number 1

Trap Label Transfer Activation / Deactivation

As we have mentioned earlier, the signal instruction can also be used to transfer control to a trap label.
The general syntax of the Trap label transfer is given as follows −

Syntax

signal ON conditionName [ NAME Label ] 
  
signal OFF conditionName
Where,
  • conditionName − This is the condition for which the signal should be either be turned on or off.
  • Label − The optional label to which the program should be diverted to.
Let’s see an example of using a trap label transfer.

Example

/* Main program */ 
signal on error 
signal on failure 
signal on syntax 
signal on novalue 
beep(1) 
signal off error 
signal off failure 
signal off syntax 
signal off novalue 
exit 0 
error: failure: syntax: novalue: 
say 'An error has occured' 
In the above example, we first turn the error signals on. We then add a statement which will result in an error. We then have the error trap label to display a custom error message.
The output of the above program will be as shown follows −
An error has occurred.

No comments:

Post a Comment