How do I achive the function of Continue using Catch and Throw?

Procedural programming is considered a bad practice in writing Mathematica code. But for education's sake, is there any way I can do Continue using Catch and Throw?

According to WRI's documentation, the function of Break and Continue both can be achieved by using Catch and Throw. For the former one, an example is given: Catch[For[i = 1, i <= 10, i++, If[i > 2, Throw[Null]]]]; i is equivalent to For[i = 1, i <= 10, i++, If[i > 2, Break[]]]; i. For the latter, however, no example could be found. I googled for that, it seems no one asked the question. And I suspect that theoretically it can't even be done. Throw lets you quit a loop, how could it be used to jump to somewhere else in the loop as Continue does?


If you wrap the body of the loop with Catch, then any Throw will act as Continue:

r = 0;
   If[EvenQ[i], Throw[Null]];
   r += i
   ], {i, 10}];
(* Out: 25 *)

(Example taken from the documentation for Continue.)

Category: procedural programming Time: 2016-07-30 Views: 0

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development


Front-end development


development tools

Open Platform

Javascript development

.NET development

cloud computing


Copyright (C), All Rights Reserved.

processed in 6.387 (s). 13 q(s)