0
$\begingroup$

I have the following code,

j=0;
Table[{Label[1], j = j + 1, i, If[j == 2, Goto[1], Continue]}, {i, 1,10}] 

which does not work; I get does not find Label[1] and for other initial values of j, I get the following result:

{Label[1], 3, 1, Continue}, {Label[1], 4, 2, Continue}, {Label[1], 5, 3, Continue}, {Label[1], 6, 4, Continue}, {Label[1], 7, 5, Continue}, {Label[1], 8, 6, Continue}, {Label[1], 9, 7, Continue}, {Label[1], 10, 8, Continue}, {Label[1], 11, 9, Continue}, {Label[1], 12, 10, Continue}}

What I intended to do was: when j is equal to 2, the i in loop does not change until j gets added by one and then it continues. Could you please help me where I made mistake? Thanks

$\endgroup$

closed as unclear what you're asking by Szabolcs, m_goldberg, MarcoB, José Antonio Díaz Navas, Öskå Sep 8 '18 at 17:17

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 8
    $\begingroup$ Can you please explain what output you expect to see? There is probably a much simpler way to accomplish the task. $\endgroup$ – bill s Aug 30 '18 at 3:09
4
$\begingroup$

Maybe this does what you want?

j = 0;
Table[Label[1]; j = j + 1; i; If[j == 2, Goto[1]]; j, {i, 1, 10}]

{1, 3, 4, 5, 6, 7, 8, 9, 10, 11}

The main problems where the commas; they have to be semicoli. Notice also that the braces { and } in Mathematica are used as delimiters for lists and not for code blocks -- contrary to C. For code blocks, just use parentheses ( and ).

Continue is not really necessary here. If you insist on using it, please use Continue[] instead (notice the brackets).

$\endgroup$
  • $\begingroup$ thank you, but what I wanted, was when j=2 goes back without changing i, in this case when j=2, it goes back without changing i could you please advise me in this respect> $\endgroup$ – kmsin Aug 30 '18 at 11:44
  • $\begingroup$ @kmsin I don't understand your question. When j equals 2, the proposed algorithm does jump back to Label[1] without changing i. $\endgroup$ – Henrik Schumacher Aug 30 '18 at 11:47
  • $\begingroup$ Ah, yes. Thank you so much Herik, I really appreciate it. $\endgroup$ – kmsin Aug 30 '18 at 11:52
  • $\begingroup$ You're welcome. $\endgroup$ – Henrik Schumacher Aug 30 '18 at 11:53
  • $\begingroup$ could you please tell me what the problem is with this one? pos = Table[ Label[2]; {x[i] = 20*RandomReal[], y[i] = 20*RandomReal[]}, Table [If[Sqrt [(x[i] - x[j])^2 + (y[i] - y[j])^2] < 4, Goto[2] ], {j, 1, i - 1}], {i, 1, 10}] $\endgroup$ – kmsin Aug 30 '18 at 19:04
3
$\begingroup$

There are easier ways to "skip a beat". For example:

Table[If[i == 2, Nothing, i], {i, 1, 11}]
$\endgroup$
1
$\begingroup$

If already does what you want:

j = 0;
Table[j++; If[j == 2, j++; j, j], {i, 1, 10}]
$\endgroup$
1
$\begingroup$

A different way of doing this might be:

Complement[Table[j, {j, 1, 11}], {2}]
$\endgroup$
  • 2
    $\begingroup$ You can format inline code and code blocks by selecting the code and clicking the {} button above the edit window. The edit window help button ? is useful for learning how to format your questions and answers. You may also find this meta Q&A helpful $\endgroup$ – Michael E2 Aug 30 '18 at 16:13

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