can we use 'and' condition in "for"

2 views (last 30 days)
trilochan
trilochan on 23 Oct 2013
Commented: Cedric on 23 Oct 2013
can we use 'and' condition in "for" like example _ for re1=1000 && rh=200. _

Accepted Answer

Cedric
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
trilochan
trilochan on 23 Oct 2013
can we use if condition inside the while loop
Cedric
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!