Search code examples
c#loopsconditional-statementscontrol-flow

How to create an if statement which loops until the if statement is no longer satisfied


In my Windows-Phone application I need to use a condition which needs to run for so long until it is no longer satisfied.

How can I create some form of logic-loop? I have tried using an if statement but that only goes round once.

I'm fairly new to c# and aren't sure of all of the different logic conditions.

Thanks for your help.


Solution

  • You're looking for a while-loop:

    while(condition)
    {
        // do stuff
    }
    

    Or possibly a do-while-loop:

    do
    {
        // do stuff
    } while (condition);
    

    The difference here is that the while-loop always evaluates it's condition before the first iteration while the do-while-loop evaluates it after the first iteration.

    Further Reading