can we use 'and' condition in "for"
2 views (last 30 days)
Show older comments
can we use 'and' condition in "for" like example _ for re1=1000 && rh=200. _
0 Comments
Accepted Answer
Cedric
on 23 Oct 2013
Edited: Cedric
on 23 Oct 2013
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 Comments
Cedric
on 23 Oct 2013
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.
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!