Questions about looping or recursive structures that do not complete. Such structures include While, For, FixedPoint, and NestWhile. Also user defined functions that make recursive calls to themselves.
Questions tagged [infinite-loop]
132 questions
5
votes
1 answer
How do I limit the iterations in a while loop?
Is there a way to limit the number of iterations in a while loop? Right now I am cleaning up the code, and for now if I make a mistake, the loop keeps going forever. I'd like to be able to limit the number of times it will run through while I am…
Kaisey
- 381
- 2
- 7
5
votes
1 answer
While loop repeats itself using initialization cell
I am running While loops in an initialization cell for the purpose of submitting the notebook to the online cluster. However it appears to me that the While loop in an initialization cell would run twice. For example, if one evaluates
n = 1; While[n…
Ethan Eastwood
- 53
- 2
4
votes
1 answer
Coding `While` loops
I am interested in replicating the code in this question on MSE, whose contents I have copied for ease of reading:
Pseudocode:
Consider that 1 is the starting index of a list
1. input natural number n.
2. let s = list of all natural numbers {1,…
martin
- 8,678
- 4
- 23
- 70
3
votes
1 answer
Infinite loop with While is very processor-intensive
This simple line of code:
While[True, Pause[1]]
uses 55% of one CPU core on my computer. Are there more efficient ways to construct simple infinite loops in Mathematica?
Edit
I'm on OS X 10.9.1 using Mathematica 10.0.
Further…
shrx
- 7,807
- 2
- 22
- 55
2
votes
1 answer
Clear inside a loop
I want to solve a system of differential equations and each time I want to change two parameters. To prevent wrong result I want to clear every assignment after solving the equations in each iteration. I have a Do-loop with two iterator. The problem…
MOON
- 3,864
- 23
- 49
2
votes
1 answer
Abort when key is pressed
Is it possible to abort a while loop when a key is pressed?
Lets say I have this code
t=0;
While[t<10!,Print[t];t++]
Is there a way to stop this loop when I press space?
Is there a command like "keypressed"?
ZaMoC
- 6,697
- 11
- 31
1
vote
2 answers
Loop until convergence of two variables
I have the following program which i need to repeat steps until until two variables converge and I want this to be printed with the number of iterations:
What I have :
Input variables:
xTensionLaminate = 1325
SigmaFail = xTensionLaminate*VF
σApplied…
Nikolas
- 105
- 9
1
vote
2 answers
If/else re-evaluate (integer only solutions)
I am trying to get Mathematica to do an if/else loop. I can get as far as the if, but I would like it to re-evaluate until only integer solutions are found. Here is what I have:
If[sols =
Flatten[{x, y} /.
Solve[{(q1 =
…
martin
- 8,678
- 4
- 23
- 70
1
vote
1 answer
Why is this an infinite loop?
The following minimal non working example is an infinite loop: why is it so?
list={1};
n=2;
While[Last@list<10,Append[list,n];n++]
It seems that Append does not append the last value to list so While is always True, but why is it so?
mattiav27
- 6,677
- 3
- 28
- 64
1
vote
0 answers
loops in one loop,
I'm new Mathematica user. I try for loop but it didnt work.
I used
For[Mi = 10^8 * Msun, Mi < 10^9 * Msun, 10^8 * Msun,
Print[fvv[10^15, 0.001, Mi, 9.454140999315537`*^22]]
]
fvv is my function which is dependent from M,Mdot,as and nu…
JESUS_M
- 21
- 3
1
vote
0 answers
Repeatedly applying a function
I have writing a code in order to get pixel values of a image along a line. I want to repeatedly to apply the function to the image to get pixel values along multiple lines. My question is how can create such loop in mathematica.
IMGV1 =…
Ji Daoyuan
- 11
- 2
0
votes
2 answers
`Do` looping - break & output based on pattern
I have the following loop that I would like to stop after the pattern repeats & output cycle length:
gamePairs[{a_, b_}] := If[a < b, {2 a, b - a}, {2 b, a - b}]
gamePlong[pair_] :=
Do[Print@Sort@Nest[gamePairs, pair, n]; If[Nest[gamePairs, pair,…
martin
- 8,678
- 4
- 23
- 70
0
votes
1 answer
looping in mathematica for using the current value as initial state of next value
i have some initial state $psi[x,t]$. I perform some operation on it and store the value in another state $pi1[x,t]$. Now on the next step i have to use $psi1[x,t]$ as initial state and perform the functions again and store in $psi2[x,t]$, Please…
sam
- 1
- 2
0
votes
1 answer
While loop not breaking when condition is met
I am having trouble with the while loop not breaking,
f[x_, y_] = 2 x^2 + 5 y^2;
dfx[x_, y_] = D[f[x, y], x];
dfy[x_, y_] = D[f[x, y], y];
a = 10.;
b = 10.;
aold = 0;
bold = 0;
count=0;
While[ Abs[f[a, b] - f[aold, bold]] > 10^-5,
aold =…
user271827
- 3
- 3
0
votes
1 answer
Double For loop does not give the expected results?
Why does the j index start from 2 not 1 in this double For loop
For[i = 1, i <= 2, i += 1, For[j = 1, j <= 2, j += 1; Print[{i, j}]]]
{1,2}
{1,3}
{2,2}
{2,3}
MMA13
- 4,664
- 3
- 15
- 21