While loop inside another While loop (While in While)
85 次查看(过去 30 天)
显示 更早的评论
Hello everyone. I have faced with problem where I have to do two while loops simultaneously and I decided to put another one inside the existing while loop. Is there any other way or I am doing something wrong, or even it is impossible. Thank you!
1 个评论
Adam
2017-1-20
编辑:Adam
2017-1-20
You need to give more information on exactly what you are doing. 'Simultaneously' implies that the two while loops are (probably) independent of each other, but both happening at the same time. This obviously will not be achieved by nesting them, but maybe you just don't really mean 'simultaneously'.
Obviously nested while loops are possible. Do they do what you want? I have no idea. I there a better way? Again, I have no idea without knowing what you are trying to do.
回答(1 个)
Jorge Mario Guerra González
2017-1-20
I don't think there is a problem with the loops by themselves. You must have a problem with the logic, can you show the code you have. Here is an example of how to use a while into another while.
i=0;
while i<5
j=0;
while j<5
disp([i j]);
j=j+1;
end
i=i+1;
end
Also If you want to run them 'simultaneusly' I understand that as running them in parell, you can try to do domething like this.
parpool(4);
i=0;
spmd
while i<5
disp([labindex i]);
i=i+1;
end
end
7 个评论
DGM
2023-2-22
The use of disp() was simply to demonstrate how the two values are being incremented as the execution progresses through both loops. j increments from 0 to 4 five times. Each time j recycles back to 0, i is incremented.
i=0;
while i<5
j=0;
while j<5
disp([i j]);
j=j+1;
end
i=i+1;
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!