While loop inside another While loop (While in While)

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
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
Jorge Mario Guerra González 2017 年 1 月 20 日

3 投票

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 件のコメント

Kamran Abbasov
Kamran Abbasov 2017 年 1 月 20 日
Thank you very much. I have adjusted my code according to your example and it worked
Bachtiar Muhammad Lubis
Bachtiar Muhammad Lubis 2018 年 12 月 24 日
is there another way to boost the nested while loop ? i mean while loop inside the while loop.
Walter Roberson
Walter Roberson 2018 年 12 月 24 日
Batchtiar, could you explain your needs more?
Bachtiar Muhammad Lubis
Bachtiar Muhammad Lubis 2018 年 12 月 28 日
@Walter Roberson : i made code where i need to use while loop inside while loop, first ran the tes2_2.m worked properly, but the second time the tes2_2.m didn't show any output at all, even i couldn't terminate it operation.
i think the problem is in the while loop, can you help me how to solve this issue sir ?
Walter Roberson
Walter Roberson 2018 年 12 月 28 日
Please start a new question for that, as it is quite different than the current topic.
Logan
Logan 2023 年 2 月 22 日
Why did you need to use display [i j] in the first example?
DGM
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
0 0 0 1 0 2 0 3 0 4 1 0 1 1 1 2 1 3 1 4 2 0 2 1 2 2 2 3 2 4 3 0 3 1 3 2 3 3 3 4 4 0 4 1 4 2 4 3 4 4

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2017 年 1 月 20 日

コメント済み:

DGM
2023 年 2 月 22 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by