Skip to main content

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.

Filter by
Sorted by
Tagged with
-2 votes
4 answers

Block structured iteration and recursion

Programming languages traditionally have blocks that specifically cater to controlling iteration. There is the simple while-loop: while (i < 3) {...}; Then there is the more complicated for-loop: ...
Steve's user avatar
  • 9,272
0 votes
2 answers

Is this a case where 'else' is inevitable?

I am writing some code that enables / disables a certain kind of hardware function. To enable it on, I have to call some methods, and to disable it, some others. Of course I want this code to be clean,...
Bart Friederichs's user avatar
0 votes
0 answers

Do mainstream definitions of basic blocks and extended basic blocks say anything about SKIP instructions?

Not many architectures have skip instructions today. They are (usually) conditional instructions that skip the following instruction. A few architectures having these: PIC14, with its btfss and btfsc ...
Omar and Lorraine's user avatar
3 votes
2 answers

Control flow vs. Flow control

When it comes to name things like if, else, or return, many developers and a lot of documentation calls it control flow or control flow statements.
john c. j.'s user avatar
1 vote
3 answers

Is there an elegant way to structure consecutive short-circuited assignments?

The following code works and is clear, but it's also verbose. I suspect that there's a way to make it more terse, so that it could be skimmed quickly and it'd be more obvious what's happening. // The ...
Ian Dunn's user avatar
  • 125
3 votes
1 answer

Is there a pattern for choosing one of a set of options at random when their selection criteria are equal?

I want to choose between several options, and if the criteria I'm using for selection happen to be equal, I want one at random (reasonably so, so it's equal-chance each time, rather than arbitrary). ...
mattdm's user avatar
  • 149
2 votes
4 answers

is it worth rearrange if else structures to shorter form [duplicate]

In our project there are deep and lengthy if-else structures which can be significantly shortened by a simple rearrangement. To me the shorter version would be easier to understand also. As an ...
riskop's user avatar
  • 129
-1 votes
1 answer

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 ...
Pepijn's user avatar
  • 337
0 votes
3 answers

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 ...
Lance's user avatar
  • 2,615
3 votes
3 answers

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 ...
executor21's user avatar
4 votes
4 answers

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 ...
Bob Tway's user avatar
  • 3,636
2 votes
2 answers

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 ...
Tim's user avatar
  • 5,485
2 votes
2 answers

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 ...
cgew85's user avatar
  • 123
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 ...
Baldrickk's user avatar
  • 714
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 ...
Hexodus's user avatar
  • 77
2 votes
2 answers

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 ...
52d6c6af's user avatar
  • 730
1 vote
2 answers

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 ...
mlissner's user avatar
  • 131
1 vote
2 answers

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 (!...
Kristof's user avatar
  • 2,241
3 votes
2 answers

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. ...
mFeinstein's user avatar
0 votes
1 answer

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 ...
Alex Celeste's user avatar
6 votes
5 answers

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 ...
ADTC's user avatar
  • 679
0 votes
1 answer

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 ...
Wojciech Szczurek's user avatar
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)...
Paul Draper's user avatar
  • 6,022
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 ...'s user avatar
5 votes
2 answers

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 ...
Wrzlprmft's user avatar
  • 198
7 votes
2 answers

"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 ...
ThisIsNoZaku's user avatar
4 votes
2 answers

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 ...
rick's user avatar
  • 1,985
-5 votes
2 answers

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,
user1461119's user avatar
2 votes
3 answers

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 ...
Morwenn's user avatar
  • 1,766
8 votes
3 answers

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. Why did it happen? What are the language design goals ...
Tulains Córdova's user avatar
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 ...
tpaksu's user avatar
  • 249
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 ...
Swizzlr's user avatar
  • 503
3 votes
2 answers

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 ...
AncientSwordRage's user avatar
5 votes
4 answers

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 ...
Helder AC's user avatar
  • 295
31 votes
10 answers

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 ...
fredoverflow's user avatar
  • 6,884
1343 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 ...
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 ) ...
Svish's user avatar
  • 1,102
-1 votes
9 answers

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 ...
Dylan Cole's user avatar
268 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) {...
12 votes
22 answers

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 ...
Maniero's user avatar
  • 10.8k
302 votes
19 answers

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