How to modify Loop variable inside the loop body
13 次查看(过去 30 天)
显示 更早的评论
for i=1:10
if randi(10)==3
i=i-1;
end
end
In the above code, i must get reduced by 1 whenever the if condition comes true. But it doesn't gets so. Any modifications so that loop variable i gets modified inside the loop body?
0 个评论
采纳的回答
Jan
2018-6-30
This does not work in a for loop, but in while:
i = 1;
while i <= 10
disp(i)
...
i = i + 1;
if randi(10) == 3
i = i-1; % Or i - 2?
end
end
更多回答(0 个)
另请参阅
类别
在 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!