Flow-control structures: Difference between revisions

Content added Content deleted
No edit summary
(Shortened the contents table by changing third level of hierarchy into just bold text)
Line 398: Line 398:
== Icon and Unicon ==
== Icon and Unicon ==
==={{header|Icon}}===
==={{header|Icon}}===

====Prelude about Goal-Directed Evaluation and Generators====
'''Prelude about Goal-Directed Evaluation and Generators'''<br>




Two of the key features of Icon and Unicon that affect program flow are [[Icon%2BUnicon/Intro#Goal-Directed_Evaluation_and_Generators|Goal Directed Evaluation and Generators]] and [[Icon%2BUnicon/Intro#Failure_is_an_Option|Expression Failure]]. Goal Direction uses Generators to produce multiple results as needed and Expression Success and Failure forces the selection of logic pathways within programs.
Two of the key features of Icon and Unicon that affect program flow are [[Icon%2BUnicon/Intro#Goal-Directed_Evaluation_and_Generators|Goal Directed Evaluation and Generators]] and [[Icon%2BUnicon/Intro#Failure_is_an_Option|Expression Failure]]. Goal Direction uses Generators to produce multiple results as needed and Expression Success and Failure forces the selection of logic pathways within programs.



====goto====
'''goto'''<br>
Does not exist in the Icon or Unicon language.
Does not exist in the Icon or Unicon language.

====next====
'''next'''<br>
Restarts the enclosing loop. The conditional on the loop is evaluated as normal.
Restarts the enclosing loop. The conditional on the loop is evaluated as normal.

====break expr====
'''break expr'''<br>
Default value of ''expr'' is the null value ''&amp;null''. This operator breaks out of the enclosing loop, yielding the expression as the result of the loop. Normally loops yield a failure ie no result, so you can write code like this:
Default value of ''expr'' is the null value ''&amp;null''. This operator breaks out of the enclosing loop, yielding the expression as the result of the loop. Normally loops yield a failure ie no result, so you can write code like this:
<lang icon>
<lang icon>
Line 425: Line 429:
</lang>
</lang>
breaks out of two levels of loop and re-enters the top of the third-level enclosing loop.
breaks out of two levels of loop and re-enters the top of the third-level enclosing loop.

====return expr====
'''return expr'''<br>
Default value of expr is ''&amp;null''. Apart from the usual meaning of ''return'', if the ''expr'' value fails, then the procedure actually fails too, ie does not yield a value. See description of ''fail'' keyword. If the ''expr'' is capable of yielding more than one result, only the first result is asked for and used.
Default value of expr is ''&amp;null''. Apart from the usual meaning of ''return'', if the ''expr'' value fails, then the procedure actually fails too, ie does not yield a value. See description of ''fail'' keyword. If the ''expr'' is capable of yielding more than one result, only the first result is asked for and used.

====fail====
'''fail'''<br>
Causes the the enclosing procedure to terminate without returning value. This is different from returning void or a null value that many other languages do when the code does not return an actual value. For example, in
Causes the the enclosing procedure to terminate without returning value. This is different from returning void or a null value that many other languages do when the code does not return an actual value. For example, in
<lang icon>
<lang icon>
Line 433: Line 439:
</lang>
</lang>
The value of x will not be replaced if ftn() issues the ''fail'' command. If ftn fails, then Goal-Directed Evaluation will also fail the assignment, therefore x is not assigned a new value. If the flow of control through a procedure falls off the end, the procedure implicitly fails.
The value of x will not be replaced if ftn() issues the ''fail'' command. If ftn fails, then Goal-Directed Evaluation will also fail the assignment, therefore x is not assigned a new value. If the flow of control through a procedure falls off the end, the procedure implicitly fails.

====suspend expr====
'''suspend expr'''<br>
Default value of expr is ''&amp;null''. Any procedure containing the ''suspend'' command will yield a value to the calling code. However the procedure remains in a state of suspended animation ready to be reactivated if the calling code demands another result due to Goal Directed Evaluation. Note that this capability is built directly into the runtime rather than being an artifically constructed behaviour provided by Python or C#'s use of the 'yield' keyword. Every and all expressions may suspend or be involved in a suspending expression without any effort. Behaviourally much closer to Prolog which also supports backtracking as a core part of the language. If the ''expr'' is capable of yielding more than one result, then supend (if driven) will progressively yield all of those values.
Default value of expr is ''&amp;null''. Any procedure containing the ''suspend'' command will yield a value to the calling code. However the procedure remains in a state of suspended animation ready to be reactivated if the calling code demands another result due to Goal Directed Evaluation. Note that this capability is built directly into the runtime rather than being an artifically constructed behaviour provided by Python or C#'s use of the 'yield' keyword. Every and all expressions may suspend or be involved in a suspending expression without any effort. Behaviourally much closer to Prolog which also supports backtracking as a core part of the language. If the ''expr'' is capable of yielding more than one result, then supend (if driven) will progressively yield all of those values.


A procedure can contain several uses of ''suspend'' and it's quite reasonable for the procedure to execute many of them in any chosen order.
A procedure can contain several uses of ''suspend'' and it's quite reasonable for the procedure to execute many of them in any chosen order.

====stop(expr)====
'''stop(expr)'''<br>
Terminate program with prejudice.
Terminate program with prejudice.

====error trapping====
'''error trapping'''<br>
The keyword &amp;error is normally zero, but if set to a positive value, this sets the number of fatal errors that are tolerated and converted to expression failure; the value of &amp;error is decremented if this happens. Therefore the now-common TRY-CATCH behaviour can be written as:
The keyword &amp;error is normally zero, but if set to a positive value, this sets the number of fatal errors that are tolerated and converted to expression failure; the value of &amp;error is decremented if this happens. Therefore the now-common TRY-CATCH behaviour can be written as:
<lang icon>
<lang icon>
Line 452: Line 461:
</lang>
</lang>
Various idiomatic simplifications can be applied depending on your needs.
Various idiomatic simplifications can be applied depending on your needs.

====error throwing====
'''error throwing'''<br>
Errors can be thrown using the function
Errors can be thrown using the function
<lang icon>
<lang icon>
Line 819: Line 829:
It then becomes necessary for any descendants of this class to over-ride the ''__init__()'' method. Any attempt to instantiate a "MyVirtual" object directly will raise an exception.
It then becomes necessary for any descendants of this class to over-ride the ''__init__()'' method. Any attempt to instantiate a "MyVirtual" object directly will raise an exception.



====Case 1 - Try, Except====
'''Case 1 - Try, Except'''
<lang python>try:
<lang python>try:
temp = 0/0
temp = 0/0
Line 826: Line 837:
print "An error occurred."
print "An error occurred."
# Output : "An error occurred"</lang>
# Output : "An error occurred"</lang>

====Case 2 - Try, Except====
'''Case 2 - Try, Except'''
<lang python>try:
<lang python>try:
temp = 0/0
temp = 0/0
Line 833: Line 845:
print "You've divided by zero!"
print "You've divided by zero!"
# Output : "You've divided by zero!"</lang>
# Output : "You've divided by zero!"</lang>

====Case 3 - Try, Except, Finally====
'''Case 3 - Try, Except, Finally'''
<lang python>try:
<lang python>try:
temp = 0/0
temp = 0/0
Line 856: Line 869:
finally:
finally:
do_some_cleanup() # run in any case, whether any exceptions were thrown or not</lang>
do_some_cleanup() # run in any case, whether any exceptions were thrown or not</lang>

====Case 4 - Try, Except, Else====
'''Case 4 - Try, Except, Else'''
<lang python>try:
<lang python>try:
temp = 1/1 # not a division by zero error
temp = 1/1 # not a division by zero error
Line 866: Line 880:
# Output :
# Output :
# No apparent error occurred.</lang>
# No apparent error occurred.</lang>

====Case 5 - Try, Except, break, continue====
'''Case 5 - Try, Except, break, continue'''
<lang python>i = 0
<lang python>i = 0
while 1: # infinite loop
while 1: # infinite loop
Line 887: Line 902:
# You've divided by zero. Decrementing i and continuing...
# You've divided by zero. Decrementing i and continuing...
# Imaginary Number! Breaking out of loop</lang>
# Imaginary Number! Breaking out of loop</lang>

====Case 6 - Creating your own custom exceptions, raise====
'''Case 6 - Creating your own custom exceptions, raise'''
<lang python># Let's call our custom error "StupidError"; it inherits from the Exception class
<lang python># Let's call our custom error "StupidError"; it inherits from the Exception class