채택된 답변

Cedric
Cedric 2013년 10월 23일
편집: Cedric 2013년 10월 23일

0 개 추천

You can't, it is always
for loopIndex = someArray
...
end
which is not condition-based. You can do it with WHILE statements though..
re1 = 0 ;
rh = 0 ;
while re1 < 0.5 && rh > -1
...
re1 = rand(1) ;
rh = randn(1) ;
end
You can also use break and continue in either loop statement, but we usually don't use them in the context that you describe. It is more for situations as follows
for k = 1 : maxRetry
...
found = ...
if found
break ;
end
end

댓글 수: 2

trilochan
trilochan 2013년 10월 23일
can we use if condition inside the while loop
Cedric
Cedric 2013년 10월 23일
You can use IF statements inside both loops, yes. As mentioned, the difference between these two loops is that one is based on a loop index which takes iteratively all values of a 1D array (or all columns of a 2D array), and the other on a loop condition. Other than that, you can do whatever you want inside both statements, and even nest them.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2013년 10월 23일

댓글:

2013년 10월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by