Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [control-structures]

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.

-1
votes
1answer
68 views

What kind of of tools/architecture would work well for a student team competing in a robot soccer competition?

The scenario is as follows. There is a PC running a complex piece of AI software wirelessly controlling a number of soccer robots. This software is written and maintained by a team of students that ...
0
votes
3answers
136 views

How to Simulate Control-Flow without using Control-Flow Primitives

Basically, I want to know how to simulate while and if if I'm handling the control flow myself through an array of instructions. The while loop can be simulated by if, as seen with assembly branching ...
3
votes
3answers
284 views

Is there an elegant way to write code with differing paths having a common middle segment?

I started out with the following, simple routine: void originalCode(){ boolean status = getStatus(); function2(status); if(status){ return; } function3(); } But ...
4
votes
4answers
141 views

Is there a pattern for dealing with one consistent case that's different from all others?

Been given an awkward design challenge, and I'm not sure how best to handle it. The scenario is this: in the system there's a concept of "Client". Each client has various bits of supporting metadata ...
2
votes
2answers
172 views

Can conditional break in a loop be rewritten for easier understanding?

while cond1 ... if cond2 ... else ... break The while loop above has two termination conditions, cond2 and !cond1 !cond2 When the commands that are represented ...
2
votes
2answers
79 views

Question about a maintainable approach on how to handle changes in the UI inside the source code

Inside our UI there's an option to select something from a dropdown. Depending on what you choose inside that menu the rest of the fields inside the UI change accordingly. At the moment we handle it ...
5
votes
4answers
400 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 ...
1
vote
3answers
1k 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
141 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
100 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
406 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
922 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
135 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 ...
6
votes
5answers
11k 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
363 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 ...
22
votes
2answers
11k 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 exceptions)...
8
votes
1answer
500 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
998 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
4k 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 ...
3
votes
2answers
937 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; What ...
-5
votes
2answers
196 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
351 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
3k 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
373 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 ...
29
votes
8answers
9k 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
253 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
588 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 ...
26
votes
10answers
14k 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 "because ...
978
votes
9answers
160k 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 ...
37
votes
4answers
5k 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 ) ...
-1
votes
9answers
23k 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 ...
175
votes
20answers
113k 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 ...
268
votes
19answers
105k 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) ...