Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

This question already has an answer here:

I made a simple Python script that takes user input in order to generate a series of mathematical responses. In one part I did this:

while True:

And iterated through the loop until I used an if statement to break out of it, something similar to:

if answer == something_whatever:
  break

I know that the examples are vague, but I am not asking for a solution to a programming problem, what happened here is that my professor told me that break statements are not to be used in this way.....she said that break statements should only be used in case statements, using them otherwise is considered bad practice. I boldly told my instructor that there was no case switch construct in Python. Is using a break statement in such way considered bad practice? I understand that there are a lot of ways in which one could break out of a loop, but is using while True really that bad?

share|improve this question

marked as duplicate by gnat, Bart van Ingen Schenau, Kilian Foth, MichaelT, Dan Pichelman yesterday

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
In Python, using break to escape a loop is perfectly acceptable (see e.g. the docs). However, a case switch usually becomes a dict in Python (see e.g. here): choices = {something_whatever: function_to_run}; choices[answer](). –  jonrsharpe Aug 12 at 10:10

1 Answer 1

up vote 2 down vote accepted

When I was in college, once, I was helping my friend solving an exercise. The final result was similar to:

public int ComputeSomething(int a, int b)
{
    if (a == 0) {
        return 0;
    }

    // Business logic (5-6 LOC) goes here.
    return result;
}

Then the teacher came, looked at the solution and told us that it's wrong. He then rewrote it the right way:

public int ComputeSomething(int a, int b)
{
    int result;
    if (a == 0) {
        result = 0;
    } else {
        // Business logic (5-6 LOC) goes here.
    }

    return result;
}

Later, reading books of popular authors, I learnt that what I used is called a guard clause and in this example would actually be a preferred solution (especially since it makes it easier to separate exceptional conditions from the actual logic and to avoid additional indentation.

Long story short, bad practice is usually slightly subjective and highly dependent on the context. Something which would be a bad practice in one language fits well in a different language. Something which is used by experienced developers in the industry would be avoided by teachers in college.

In Python, using break to exit a while has nothing wrong. The case is similar to the example above: do you want to have a single exit point from your while, or do you accept to break out anywhere within the while block?

Rewritten:

answer_found = False
while not answer_found:

    # Business logic goes here.

    if answer == something_whatever:
        answer_found = True

the code has its benefit. The readability is slightly increased if you pick a good name for the variable. Here, a maintainer understands, without having to read the full piece of code, that while processes some questions until it finds some answer.

Also, don't confront your teacher telling that someone on a Q&A website told you so. Instead, rewrite your code to avoid break statements, showing that you actually know how to do that.

share|improve this answer
1  
Something which is used by experienced developers in the industry would be avoided by teachers in college. -- Exactly this. Academia has different goals than real code that actually solves problems, and academics typically don't have the industry experience to know exactly how they differ. –  Phoshi Aug 12 at 10:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.