I want to create an if loop inside multiple if loops that are already present
3 次查看(过去 30 天)
显示 更早的评论
I want to stop a piece of code once a certain variable goes past a certain number.
I am not sure exactly where to put it but essentially I want the code to stop once Vr is < arbitrary number, 0.004, let us say
if Vf_new < Vr(3,1) && delta_Vfc >= 1E-6
%
disp('update')
Vr(3,1) = Vf_new;
iter = iter + 1;
%
elseif delta_Vfc < 1E-6 || Vf_new <= Vr(3,1)
%
disp('do not update')
continue_iteration = 0;
%
end
It wants to appear here.
I am just not sure how to nest it inside an already existent if elif loop. I think it should look like this...
if Vf_new < 0.004
return
end
Help please
Thanks
Alex
0 个评论
采纳的回答
Image Analyst
2023-3-30
Not sure what you have: a for loop or multiple if/else blocks. An "if" block is not called a loop because it does not loop/iterate.
Let's say that you have a for loop and inside the for loop is your if tests and you want to check Vf_new at the beginning of the loop
for k = 1 : 10000
if Vf_new < 0.004
break; % Breaks out of the for loop and continues the program after the for loop.
% OR you can exit the loop and the entire function or program you're
% in by calling return;
% return; % Step out of the entire program.
elseif Vf_new < Vr(3,1) && delta_Vfc >= 1E-6
fprintf('Updating. Now Vf_new = %f.\n', Vf_new);
Vr(3,1) = Vf_new;
iter = iter + 1; % If iter is your for loop variable, then you don't need this line. If it's a while loop you may need this or something like it.
elseif delta_Vfc < 1E-6 || Vf_new <= Vr(3,1)
fprintf('NOT updating. Now Vf_new = %f.\n', Vf_new);
continue_iteration = 0; % If your loop is a for loop, then you don't need this line. If it's a while loop you may need this or something like it.
break; % Get out of loop. Do not continue iterating.
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!