A control structure is a statement that changes the control flow in a program, based on the evaluation of one or more conditions. Common examples include the 'if-then-else' statement, the 'case-switch' statement, 'for' loops, and 'while' loops.
Questions tagged [control-structures]
41 questions
1330
votes
14 answers
Where did the notion of "one return only" come from?
I often talk to programmers who say "Don't put multiple return statements in the same method." When I ask them to tell me the reasons why, all I get is "The coding standard says so." or "It's confusing." When they show me solutions with a single…

fredoverflow
- 6,854
- 8
- 39
- 46
302
votes
19 answers
Should I return from a function early or use an if statement?
I've often written this sort of function in both formats, and I was wondering if one format is preferred over another, and why.
public void SomeFunction(bool someCondition)
{
if (someCondition)
{
// Do Something
}
}
or
public…

Rachel
- 23,979
- 16
- 91
- 159
259
votes
21 answers
Are `break` and `continue` bad programming practices?
My boss keeps mentioning nonchalantly that bad programmers use break and continue in loops.
I use them all the time because they make sense; let me show you the inspiration:
function verify(object) {
if (object->value < 0) return false;
if…

Mikhail
- 367
- 2
- 5
- 10
40
votes
8 answers
Why does Clang/LLVM warn me about using default in a switch statement where all enumerated cases are covered?
Consider the following enum and switch statement:
typedef enum {
MaskValueUno,
MaskValueDos
} testingMask;
void myFunction(testingMask theMask) {
switch (theMask) {
case MaskValueUno: {}// deal with it
case MaskValueDos:…

Swizzlr
- 503
- 1
- 5
- 8
39
votes
4 answers
Why are brackets required for try-catch?
In various languages (Java at least, think also C#?) you can do things like
if( condition )
singleStatement;
while( condition )
singleStatement;
for( var; condition; increment )
singleStatement;
So when I have just one statement, I…

Svish
- 1,092
- 8
- 14
31
votes
10 answers
Do we still have a case against the goto statement?
Possible Duplicate:
Is it ever worthwhile using goto?
In a recent article, Andrew Koenig writes:
When asked why goto statements are harmful, most programmers will say something like "because they make programs hard to understand." Press harder,…

fredoverflow
- 6,854
- 8
- 39
- 46
23
votes
2 answers
Why does Scala have return but not break and continue
Scala does not have break or continue, so some loop behavior takes a bit more of thinking.
Ending a loop early requires tail recursion, exceptions, or scala.util.control.Breaks (which uses exceptions).
The rationale for this is that, like goto, they…

Paul Draper
- 5,972
- 3
- 22
- 37
12
votes
22 answers
Which useful alternative control structures do you know?
Similar question was closed on SO.
Sometimes when we're programming, we find that some particular control structure would be very useful to us, but is not directly available in our programming language.
What alternative control structures do you…

Maniero
- 10,826
- 14
- 80
- 133
8
votes
1 answer
Is this a valid design pattern for a Haskell main function?
After developing several Haskell applications I've found myself rigorously segregating impure code and failable (partial) functions from their pure & total counterparts. These efforts have noticeably reduced maintenance cost associated with the…

recursion.ninja
- 476
- 3
- 11
8
votes
3 answers
Why was GOTO included in PHP 5?
I discovered some time ago that the GOTO control keyword was introduced in PHP 5.3.0.
http://php.net/manual/en/control-structures.goto.php
Why did it happen?
What are the language design goals behind this?
Did the PHP developers community asked…

Tulains Córdova
- 39,201
- 12
- 97
- 154
7
votes
2 answers
"Proceed if true" vs "stop if false" in if statements
While I was writing a private helper method in Java, I needed to add a check for a precondition that would cause the method to do nothing if not met. The last few lines of the method were just off the bottom of the editing area in the IDE, so in an…

ThisIsNoZaku
- 171
- 1
- 1
- 8
6
votes
5 answers
Are there real-life usage and applications for "do while" loops?
When I see for and while loops all over production codes and mammoth projects, the last time I saw a do while loop is for a university assignment involving menu-based console input program. About 50 lines long, at most.
Have you seen real-world…

ADTC
- 679
- 2
- 8
- 16
6
votes
5 answers
design for interruptable operations
I couldn't find a better topic but here it is;
1) When user clicks a button, code starts t work,
2) When another button is clicked, it would stop doing whatever it does and start to run the second button's code,
3) Or with not user interaction, an…

tpaksu
- 249
- 2
- 7
5
votes
4 answers
Coding style: Binary logic or multiple if()s?
Something that often comes up in code reviews is the structure of the code, particularly related to the use of control structures, where individuals can be fairly opinionated on what is "right" and what is "wrong".
I have my own opinions on the…

Baldrickk
- 714
- 5
- 12
5
votes
6 answers
Flow control in Go without a for loop
I've been set a challenge that I'm trying to get my head around, but am struggling with the best (or 'correct') way to implement it. The challenge is to create a simple console app written in Go that calculates the sum of squares of n numbers.…

Hexodus
- 77
- 1
- 3