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
9
votes
1 answer

Can I translate an AST to SSA, or do I need to translate to a CFG then to SSA?

Can I translate an Abstract Syntax Tree directly into SSA form, or will I need to create a control flow graph and then create the Static Single Assignment form from said CFG? And in the context of a control flow graph: how do I represent this for a…
9
votes
11 answers

How Can I Avoid Using Exceptions for Flow Control?

I have been assigned a project to develop a set of classes that act as an interface to a storage system. A requirement is that the class support a get method with the following signature: public CustomObject get(String key, Date…
Benry
  • 5,298
  • 1
  • 24
  • 25
9
votes
8 answers

Replacement for goto statement in Swift

How can I transfer control to a specific line in Swift code? In Objective-C I would do something like the following, using goto if(a==b) { goto i123; } else { goto i456; } NSLog(@"the not reachable point"); i123: NSLog(@"line 123 is…
Pawan Joshi
  • 1,581
  • 3
  • 20
  • 40
9
votes
1 answer

Promises and irregular callbacks

I'm playing around with a promises control flow, using bluebird. Bluebird provides a .promisify() method for converting a regular callback function into a promise function, but I'm unclear what I should be doing when the function is irregular. For…
Dan
  • 3,229
  • 2
  • 21
  • 34
9
votes
4 answers

How do I break out of a loop in Haskell?

The current version of the Pipes tutorial, uses the following two functions in one of the example: stdout :: () -> Consumer String IO r stdout () = forever $ do str <- request () lift $ putStrLn str stdin :: () -> Producer String IO…
hugomg
  • 68,213
  • 24
  • 160
  • 246
9
votes
7 answers

Example of a while loop that can't be written as a for loop

I know a while loop can do anything a for loop can, but can a for loop do anything a while loop can? Please provide an example.
DiegoFuego
  • 93
  • 2
  • 4
9
votes
4 answers

Compute ingestible control flow graph from source code

I know that there are ways to automatically generate a CFG (C ontrol F low G raph) from source code. However, from what I understand, those methods give me a visual graph - an image. I can't really use such an image to do any computation with. Thus…
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
8
votes
4 answers

Could Scala's “if … else” have been implemented as a library function?

I'm wondering if if … else could have been implemented in Predef with special compiler treatment, in a similar way to what's being done with classOf[A]: the definition is in Predef, the implementation is filled in by the compiler. Granted, many…
Jean-Philippe Pellet
  • 59,296
  • 21
  • 173
  • 234
8
votes
3 answers

Confusing control flow analysis from Parasoft C++test

We use Parasoft C++test to statically analyze our code. It's having trouble with code like the following: void foo(int* x) { try { bar(); } catch(...) { delete x; throw; } *x; } It warns on the *x; line…
Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175
8
votes
2 answers

How do old CPUs execute the new ENDBR64 and ENDBR32 instructions?

At some point, Intel will begin shipping CPUs that support CET(Control-flow Enforcement Technology), which adds two instructions ENDBR64 and ENDBR32. These two will be encoded as F3 0F 1E FA for ENDBR64 and F3 0F 1E FB for ENDBR32…
user11472602
8
votes
4 answers

Tool for generating control flow in Java

I need a tool for generating control flow in java, but not a visual draw, something that I can work with like with path conditions or so. Anyone ?
Louro
  • 1,413
  • 4
  • 21
  • 27
8
votes
1 answer

Building a control-flow graph from an AST with a visitor pattern using Java

I'm trying to figure out how to implement my LEParserCfgVisitor class as to build a control-flow graph from an Abstract-Syntax-Tree already generated with JavaCC. I know there are tools that already exist, but I'm trying to do it in preparation for…
8
votes
1 answer

control flow graph generator for c# code

i need a tool that takes c# code and generate the control flow graph of the code if there's something like this in visual studio ............ do please point it out to me thanks
8
votes
6 answers

Dissecting a line of (obfuscated?) Python

I was reading another question on Stack Overflow (Zen of Python), and I came across this line in Jaime Soriano's answer: import this "".join([c in this.d and this.d[c] or c for c in this.s]) Entering the above in a Python shell prints: "The Zen of…
tel
  • 13,005
  • 2
  • 44
  • 62
8
votes
4 answers

How do production compilers implement destructor handling on flow control

Long story short - I am writing a compiler, and reaching the OOP features I am faced with a dilemma involving the handling of destructors. Basically I have two options: 1 - put all destructors for objects that need calling at that point in the…
user3735658