I want to create an if loop inside multiple if loops that are already present
    7 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
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 Commenti
Risposta accettata
  Image Analyst
      
      
 il 30 Mar 2023
        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
1 Commento
Più risposte (0)
Vedere anche
Categorie
				Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

