I want to create an if loop inside multiple if loops that are already present
    7 ビュー (過去 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!

