Questions tagged [control-flow]

Control flow (or flow of control) refers to the order in which statements are evaluated or executed.

Control flow (or flow of control) refers to the order in which statements are evaluated or executed.

There are many methods to control the flow of execution in a program:

Unconditional jumps can use labels:

foo:
printf ("Hello, world.\n");
goto foo;

... or line numbers:

10 PRINT "something"
20 GOTO 10

Conditional branching can use constructs:

if ($x > 3) {
    print "$x is many.";
} else {
    print "$x is few.";
}

... or statements:

case n of
    1 : writeln('one');
    2 : writeln('two');
    3 : writeln('three');
end;

Loops of various kinds exist, including loops:

for (var i = 0; i < 9; i++) {
    window.alert(i);
}

... loops:

foreach (int x in myArray)
{
    Console.WriteLine(x);
}

... and loops:

while read z
do
    echo "Hello, ${z}."
done

Loops can also be terminated early, or made to bypass some of the loop body, with and statements respectively:

for club in ("groucho", "fight", "fight", "kitkat", "drones"):
    if club == "fight":
        continue
    if club == "kitkat":
        break
    print(club)

Functions temporarily divert execution to named sections of code, before returning:

hello = (name) -> "Hello, #{ name }."
console.log hello "world"

Exceptions are used to divert control flow in exceptional circumstances:

try {
    x = 1 / 0;
    System.out.println("Hello, world."); // is not reached
} catch (ArithmeticException e) {
    System.out.println("Goodbye, world.");
} 
837 questions
12
votes
2 answers

Practical differences between control flow graph and call (flow?) graph?

Wikipedia has a definition for a control flow graph. I've also heard terminology thrown around referring to 'call (flow?) graph', but can't find any relevant resources. What is the relationship between the two?
ChaimKut
  • 2,759
  • 3
  • 38
  • 64
12
votes
4 answers

Async beforeAll() does not finish before beforeEach() is called

In Jest, beforeAll() is supposed to run before beforeEach(). The problem is that when I use an async callback for beforeAll(), Jest doesn't wait for the callback to finish before going on to beforeEach(). How can I force Jest to wait for an async…
Asker
  • 1,299
  • 2
  • 14
  • 31
12
votes
2 answers

Using a class dictionary to map to instance methods in Python

I have a long if-elif chain, like this: class MyClass: def my_func(self, item, value): if item == "this": self.do_this(value) elif item == "that": self.do_that(value) # and so on I find that…
sourcenouveau
  • 29,356
  • 35
  • 146
  • 243
11
votes
6 answers

Is there a way to distinguish normal loop termination from break termination in Rust?

Idiomatic for/else in rust: In Python, I can use for/else to check whether or not a for loop terminated at a break statement or finished 'normally': prod = 1 for i in range(1, 10): prod *= i if prod > 123: break else: …
hiro protagonist
  • 44,693
  • 14
  • 86
  • 111
11
votes
5 answers

Is this `try..catch..finally` redundant?

public Foo doDangerousStuff() throws Exception { try { dangerousMethod(); return new Foo(); } catch (Exception e) { throw e; } finally { mustBeCalledAfterDangerousMethod(); } } Does this behave any…
Dan Burton
  • 53,238
  • 27
  • 117
  • 198
11
votes
3 answers

What is Objective C execution order after UIApplicationMain in Main?

Could someone please explain how control of execution flows in an iOS application? I know that UIApplicationMain is called first from main. Then what? What is the relationship between my defined methods and main? Is it all event-driven or can there…
Old McStopher
  • 6,295
  • 10
  • 60
  • 89
11
votes
3 answers

Control flow graph & cyclomatic complexity for following procedure

insertion_procedure (int a[], int p [], int N) { int i,j,k; for (i=0; i<=N; i++) p[i] = i; for (i=2; i<=N; i++) { k = p[i]; j = 1; while (a[p[j-1]] > a[k]) {p[j] = p[j-1]; j--} p[j] = k; } } I…
AJ.
  • 2,561
  • 9
  • 46
  • 81
11
votes
2 answers

networkx: Draw text on edges

for my thesis I need to draw some probabilistic control flow graphs. i.e. control flow graphs with probabilities depicted on the edges. I found graph-tool which seems quite useful, since it can use deep-copies of existing graphs and my graphs are…
Dominik G
  • 1,459
  • 3
  • 17
  • 37
11
votes
3 answers

What is a good way to exit a node.js script after "everything has been done"

My node.js script reads rows from a table in database 1, does some processing and writes the rows to database 2. The script should exit after everything is done. How can I know if everything has been done and exit node then? If I have a callback…
SHernandez
  • 1,060
  • 1
  • 14
  • 21
11
votes
5 answers

IEnumerable foreach, do something different for the last element

I have an IEnumerable. I want to do one thing for each item of the collection, except the last item, to which I want to do something else. How can I code this neatly? In Pseudocode foreach (var item in collection) { if ( final ) { …
Colonel Panic
  • 132,665
  • 89
  • 401
  • 465
10
votes
4 answers

Insufficient control flow analysis of enum switch in GCC

In the following C++ code: typedef enum { a, b, c } Test; int foo(Test test) { switch (test) { case a: return 0; case b: return 1; case c: return 0; } } a warning is issued when compiling with -Wall, saying that…
ulidtko
  • 14,740
  • 10
  • 56
  • 88
10
votes
2 answers

Examples of Custom Control Flow Compiling Words

Forth famously allows users to alter the language by defining new words for control flow (beyond those given by the standard: DO, LOOP, BEGIN, UNTIL, WHILE, REPEAT, LEAVE IF, THEN, ELSE, CASE, ENDCASE, etc.) Are there common examples of people…
Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
10
votes
1 answer

asyncio as_yielded from async generators

I'm looking to be able to yield from a number of async coroutines. Asyncio's as_completed is kind of close to what I'm looking for (i.e. I want any of the coroutines to be able to yield at any time back to the caller and then continue), but that…
freebie
  • 2,161
  • 2
  • 19
  • 36
10
votes
11 answers

If and else, were should I put the more likely part?

I was wondering if there is a big performance difference in languages, whether you should put the more likely to be executed code in the if or in the else clause. Here is an example: // x is a random number, or some key code from the…
Bartlomiej Lewandowski
  • 10,771
  • 14
  • 44
  • 75
9
votes
6 answers

Alternative to "last" in do loops

According to the perl manual for for last (http://perldoc.perl.org/functions/last.html), last can't be used to break out of do {} loops, but it doesn't mention an alternative. The script I'm maintaining has this structure: do { ... if (...)…
DAG
  • 157
  • 1
  • 2
  • 6