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.

learn more… | top users | synonyms

4
votes
4answers
339 views

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 ...
2
votes
3answers
113 views

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 ...
2
votes
2answers
135 views

Did labelled loops pre-date for loops?

Was the first implementation of looping control flow effectively the goto (or something like a labelled JMP in assembler) or was there another syntactic construction? for (für) was introduced in ...
1
vote
2answers
92 views

Factored out loop control in Python

I have a for loop in python like this: for item in items: onwards = make_flow_decision(item) if onwards == 'break': break elif onwards == 'continue': continue elif ...
1
vote
2answers
79 views

Is it valid to expect/throw an exception using a custom caching system?

Imagine some code like the following: class Cache { private Map<String, String> values = new HashMap<String, String>(); public String getFromCache(String key) { if ...
2
votes
2answers
117 views

Try Catch and Flow Control dilemma

I am aware that doing Flow Control on a program using a try-catch block is bad practice, but I can't see how to do it in another way when the error caught needs a redirection of the code's execution. ...
0
votes
1answer
105 views

Recursion, iteration, and …? [closed]

Here are three common code structures that apply a function multiple times: foo(x) { if basecase(x) return k else return foo(g(x)) } uses recursion. for i in 0..10 { n *= bar(i) } uses ...
4
votes
5answers
3k views

Are there real-life usage and applications for “do while” loops? [closed]

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 ...
0
votes
1answer
226 views

How to choose between different ways to write some code?

I would like to validate point in a two-dimensional rectangular Cartesian coordinate. I found following regular expression solution on stackoverflow ^(\-?\d+(\.\d+)?),\s*(\-?\d+(\.\d+)?)$ . Another ...
12
votes
1answer
5k views

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 ...
8
votes
1answer
368 views

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 ...
5
votes
2answers
519 views

Why do some programming languages have break statements, but not higher-order break statements? [closed]

I may have been exposed to exactly the wrong languages, but though many have loops and break statements, none of the languages I am familiar with have higher-order break statements¹. While a regular ...
7
votes
2answers
2k views

“Proceed if true” vs “stop if false” in if statements [duplicate]

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 ...
2
votes
2answers
283 views

In an if statement, what are an “if clause” and a “then clause”?

I am a bit confused about the nomenclature for the parts of an if statement. Consider the following example: 1: if condition then 2: statement_1; 3: else 4: statement_2; 5: end if; ...
-4
votes
2answers
93 views

what is a basic block called if it contains a branch instruction? [closed]

what is a basic block called if it contains a branch instruction? How about a block that does not contain a branch instruction? Thanks,
2
votes
3answers
257 views

Conditional construct for a kleenean data type

I was thinking of an hypothetical programming language with a kleenean data type which would implement Kleene's three-valued logic. To sum up, it's an extension of the boolean data type with the three ...
8
votes
3answers
2k views

Why was GOTO included in PHP 5? [closed]

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 ...
6
votes
5answers
339 views

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 ...
18
votes
8answers
5k views

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 ...
4
votes
2answers
241 views

Is it dangerous for me to give some of my Model classes Control-like methods?

In my personal project I have tried to stick to MVC, but I've also been made aware that sticking to MVC too tightly can be a bad thing as it makes writing awkward and forces the flow of the program in ...
4
votes
4answers
581 views

Is it bad practice for a module to contain more information than it needs?

I just wanted to ask for your opinion on a situation that occurs sometimes and which I don't know what would be the most elegant way to solve it. Here it goes: We have module A which reads an entry ...
22
votes
10answers
9k views

Do we still have a case against the goto statement? [duplicate]

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 ...
703
votes
7answers
94k views

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 ...
34
votes
5answers
3k views

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 ) ...
0
votes
9answers
13k views

Loops in real-life problems [closed]

I'm learning C as my first programming language. I'm confused on how do loops work in real-life. In programming every function has a reason. example - if/else statements are used if we have ...
116
votes
19answers
59k views

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) ...
12
votes
22answers
2k views

Which useful alternative control structures do you know? [closed]

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 ...
146
votes
19answers
46k views

Should I return from a function early or use an if statement? [closed]

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) ...