Jtaylorapps
Jtaylorapps

Reputation: 5770

Loop That Doesn't End Until Break

I'm trying to create a loop than continues to take input until the input gives the command to break the loop. What I'm doing now looks a little like this:

int start = 1;

while (start == 1) {

   //Program statement

}

However, I feel as though there is an easier, more effective way to create a loop that repeats until the user gives the command to stop it. Does something like that exist?

Upvotes: 2

Views: 1591

Answers (3)

Andre Mariano
Andre Mariano

Reputation: 308

do{

   userInput = readUserInput()

}while(userInput != exit_condition)

Any loop as for, while, or even goto can do this job. If you put a condition instead of "true" in the loop, You can reduce code and doesn't need to use the "break" statement.

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726509

A common idiom to express a "forever" loop in C and other C-like languages, including Objective-C, is to use an empty for:

for(;;) {
    // statements
}

Upvotes: 3

dexametason
dexametason

Reputation: 1133

You should do it like this:

while(true)
{
    if( exit_condition)
    {
        break;
    }
}

Upvotes: 1

Related Questions