Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
For is an overkill. And about Your question, please take a look at Throw. – Kuba Jun 20 '13 at 20:41
It is good to avoid For, more here: – Kuba Jun 20 '13 at 20:44
Throw doesn't seem to be helping me as it produces additional "error" output. I just want Mathematica to stop computing / skip the remaining commands. – Tom Jun 20 '13 at 20:55
Tom, you have to Catch the Throw, otherwise you get that error. – rcollyer Jun 20 '13 at 20:56
Mod[n, i] == 0 is the same as Divisible[n, i]. I would prefer Scan here over Do. You can exit a Scan using Return. Related Q&A – Jacob Akkerboom Jun 21 '13 at 11:11
up vote 6 down vote accepted

After comment:

Abort will help You:

  If[Mod[n, i] == 0, Print["factor found: ", i]; Abort[];]
  , {i, 2, Round@Sqrt@n}];
 Print[n, " is prime."];)
factor found: 2

I understand this behavior of ( ) rather intuitively so I will wait for someone smarter. I assume this is forcing procedures to create CompoundExpression, which is also created if You avoid line break between Your loop and last Print:

Do[ If[Mod[n, i] == 0, Print["factor found: ", i]; Abort[];]
  , {i, 2, Round@Sqrt@n}]; Print[n, " is prime."];
factor found: 2

Before comment:

It might be good to show/try/learn different way to do this.

Goto: This is not elegant and my friend once have told me "do not use Goto in C++", but it works in simple cases:

f[n_] := Module[{},
   If[Mod[n, i] == 0, Print["factor found: ", i]; Goto["End"]]
   , {i, 2, Round@Sqrt@n}];
  Print[n, " is prime."];
factor found: 3

Catch/Throw mentioned in comments:

n = 12;
  If[Mod[n, i] == 0, Throw@Print["factor found: ", i];]
  , {i, 2, Round@Sqrt@n}];
 Print[n, " is prime."];
factor found: 2
share|improve this answer
I really do appreciate the effort, but it is just not what I was looking for. I am aware of Module/Catch/etc. constructions, but I wanted to keep it clean. Hence I was trying to find a simple command that would just stop Mathematica from continuing whatever it was doing. – Tom Jun 20 '13 at 22:10
@Tom So I have missed the point again :). Please take a look at this edit. – Kuba Jun 20 '13 at 22:37

I agree with using Goto and Label in this situation. We can replace the For with While by testing the greatest factors. Of course, any number mod 1 is 0, which gets us out of the While.

Block[{n = 115, i = Ceiling[Sqrt[n]]},
  While[0 != Mod[n, --i]];
  If[i != 1, Print["Factor found ", n]; Goto["end"]];
  Print[n, " is prime"];
  (*...other code...*)
share|improve this answer

Use Break to get out of the loop, saving the test result for later use:

For[ ....  If[ notprime = (Mod[n, i] == 0) , Break[]] ... ];
If[notprime , Primt["not prime a factor is",i] , Print["Prime" ] ]

( I don't even use goto in Fortran code )

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.