0

I have a while loop within an else statement. while the condition in the while loop is true I have the variable k decreasing (k=k-1). I want for the while loop to stop before k==0. I have tried placing the while loop within another while loop (while k>1) thinking maybe that would cancel the inner loop if k dropped to 1. Any thoughts?

if yada yada
    do thing1
else
    while k>1
        while x==true
            k=k-1;
            do thing2
        end
    end
end
4
  • 2
    Shouldn't the inner while loop be an if statement instead? Commented Aug 13, 2015 at 5:59
  • Using an if statement inside should indeed do the trick, even though ... I really don't see the point of your while here ! Commented Aug 13, 2015 at 6:10
  • I may have oversimplified the code here..I am trying to iterate through a vector (originally this was all in a for loop), if a first condition is satisfied I want to do a thing and continue iterating through, else I want to look at a second condition, while that second condition remains true I want to do a second thing irrespective of the first condition which led me to that else statement...i figured a while statement within an else was the best way to do this? When condition 2 stops being true I want to return to the next iteration of the for loop (i.e. the if/else statement) Commented Aug 13, 2015 at 16:48
  • @user3470496, please consider accepting an answer if any one of those helped you solve the problem. It's the tick mark on the left side of the answer. (Questions that have answers, but appear unanswered clutters the front page, so accepting an answer is benefits all of us) =) Commented Jul 5, 2016 at 8:10

3 Answers 3

3

Doesn't this do the trick?

if yada yada
    x = f(x)   %// If you don't have anything here, you should use "if ~yada yada"
               %// instead of "else"
else
    while k>1 && x == true  %// BTW: Lower case t in true 
        x = g(y)  %// I hope you have something more than "k=k-1" in this loop
        k = k-1;
    end
end
Sign up to request clarification or add additional context in comments.

2 Comments

Using && in the loop condition (i.e. while k>1 && x == true) is definitely the way to go, except it should just be while k>1 && x
only because x is a bad choice of variable name. If is was called isChecked or something relevant then it would be clearer then having the == true (closer to English what you'd say in English at least)
0

You could add this sentence to be the last thing on your innermost while

if k <= 1
   break;

Comments

0

Since your while-loop is basically achieving a counter-increment, I would highly recommend replacing it (actually, both of them) with a for-loop. In addition to being much more readable, it will allow Matlab to apply optimizations which will result in much faster execution times. See here:

yadayada = false;
x = true;

if yadayada
else
    kmax = 5;          % or whichever maximum value you want k to have
    for k = kmax:-1:2  % have k start at kmax, decrement by 1 each time, until k==2
        fprintf('k = %d\n', k);

        % do something useful in the loop

        if ~x
            break;
        end
    end
end

Output:

k = 5
k = 4
k = 3
k = 2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.