The following example either finds a factor of the number $n$ or returns a message saying that the number in question is prime (I specifially wrote this to demonstrate my question below).
n = 125;
For[i = 2, i <= Sqrt[n], i++,
If[Mod[n, i] == 0, Print["Factor found: ", i]; Break[];]
]
If[i <= Sqrt[n], Exit[];]
Print[n, " is prime."]
...
I know I could simply include the last Print statement in the if-condition, but I am specifically looking for a command that makes Mathematica skip the last Print statement (and all other statements that might come thereafter...). I tried Break, Abort and Interrupt, but none have the desired effect.
Now the commands Exit and Quit work, but they terminate the entire Mathematica kernel, which is a bit of an overkill. How could I bypass this issue? I need something like SkipRemainingCommands.
For
is an overkill. And about Your question, please take a look atThrow
. $\endgroup$For
, more here: mathematica.stackexchange.com/q/7924/5478 $\endgroup$Catch
theThrow
, otherwise you get that error. $\endgroup$Mod[n, i] == 0
is the same asDivisible[n, i]
. I would preferScan
here overDo
. You can exit aScan
usingReturn
. Related Q&A $\endgroup$