Is using `continue`, `break` in non-`switch` loops and `?:` bad practice? [duplicate]

Back in college I’ve been told that using break; and continue; outside switch statements (e.g. to escape for or while loops) is wrong, bad practice and bad habits at the same time because it only means that you cannot write loops properly. Is it really?

A friend of mine also told me that using conditional ?: is bad practice too, however I’ve started using this more often because it allows me to write stuff in less than 70 lines of code while without it i’d end up with something like 200 lines of code. It comes in very useful in almost every project I work on and I use it quite often. Should I stop?

4

On break and continue

I don’t think there’s anything fundamentally wrong with a break or continue. I think the motivation to avoid them is really has to do more with readability than anything else. Consider the following loops (not strictly c/c++ code):

// loop 1
for(int i = 0; i < a.length; i++)
{
    if (!found(a[i]))
        continue;

    result = a[i];
    break;
}

// loop 2
for(int i = 0; i < a.length; i++)
{
    if (found(a[i]))
    {
        result = a[i];
        break;
    }
}

// loop 3
{
    bool resultFound = false;
    for(int i = 0; !resultFound && i < a.length; i++)
    {
        if (found(a[i]))
        {
            result = a[i];
            resultFound = true;
        }
    }
}

All three of these loops run essentially the same code, but are written in slightly different ways. The real question is which one most effective conveys the author’s intent?

  • loop 1 – very bad. The logic here seems to be inverted, and the continue is pretty much gratuitous. If translated to English this would read something like:

    Loop through the list while the item is not found, but if it is set the result then stop.

  • loop 2 – good. This is how such loops are generally written. If translated to English this would read something like:

    Loop through the list and if the item is found, set the result then stop.

  • loop 3 – slightly bad. The break / continue logic is found in the condition of the for
    loop, which is not as common. It means if someone reading your code wants to know what
    will happen next, they have to go back to the top of the loop. A break (as in loop 2)
    specifies that logic in exactly one place. If translated to English this would read something like:

    Loop through the list while the item is not found and if an item is found, set the result.

On the ?: operator

Again, there’s nothing fundamentally wrong with it. It’s really a matter of personal style and readability.

// Good uses of conditional operator
string result = (someBool) ? "YES" : "NO";
string oddness = (someInt % 2 == 0) ? "even" : "odd";

// Bad use of conditional operator
int result = ConditionA() ? (ConditionB() ? 0 : 1) : (ConditionC() ? 2 : 3);

Again the programmers intent is pretty clear in the first two examples, but is much more obscured in the last.

7

break and continue are perfectly fine, there are many programs that will be hard to express without them. The only time using them become a problem is if you want to formally prove correctness. In most real life code, correctness are ensured using unit test, not formal proof. Nevertheless, if a loop can be just as easily expressed without break and continue, don’t use them unnecessarily.

Using ?: is actually a good practice. Good practice for ?: though, is that all expressions (i.e. conditional part, true part, and false part) should have no side effect except for their return value. If the expression contains side effect, you might want to use conventional if-block statement instead. This is a case of command-query separation, command that have side effect should use if-block while queries that are side-effect-free should use :?, this is a guideline though, not a hard and fast rule, sometimes complicated queries might be better expressed using if-block.

break and continue are invaluable, they often make the code shorter and better readable.

Well, except when they don’t. If you find yourself using them too often or multiple times in one block, it may be that you really want the loop body to be a function (i.e. “method” in the OO terminology). It’s sometimes better to have a function with early returns than a block with multiple breaks and continues.

Regarding ?:, this is actually the functional cousin of the if statement. Functional programming is hip, so yes, use it. But remember, that functional code should be free of side effects, like others here said.

It is just a tool of programmers. No tool is bad or good by itself, it is on the user how efficiently he/she can use that tool and what he can produce with that.

It becomes necessary when you want to write optimized codes and when there is no sense to keep going further into the loops

Very common example to use break statement is into the Bubble sort algorithm. When make a check that if no swap is done into the loop stop further looping as the array is already sorted out.

Same time it is bad when it is used badly into nested loops and you embed into too much nested loops that you forgot the flow and highly chances are that you can get logical errors into your code.

? : —is a way to write code. Just a short hand, also is a tool and depends where you use it. Can reduce lines of code on some places and on the same time can reduce the code readability.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật