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
17
votes
10 answers

Live javascript debugging by recording function calls and parameters

Is there a debugging system that allows me to record javascript function calls and their parameters as they occur? this would allow me to trace and debug applications in live/client situations without the performance hit due to manual logging. Edit:…
Robin Rodricks
  • 110,798
  • 141
  • 398
  • 607
17
votes
8 answers

How do I tell which guard statement failed?

If I’ve got a bunch of chained guard let statements, how can I diagnose which condition failed, short of breaking apart my guard let into multiple statements? Given this example: guard let keypath = dictionary["field"] as? String, let rule =…
Moshe
  • 57,511
  • 78
  • 272
  • 425
17
votes
3 answers

Stop execution of a script called with execfile

Is it possible to break the execution of a Python script called with the execfile function without using an if/else statement? I've tried exit(), but it doesn't allow main.py to finish. # main.py print "Main starting" execfile("script.py") print…
JcMaco
  • 1,258
  • 3
  • 16
  • 31
16
votes
6 answers

Get Control flow graph from Abstract Syntax Tree

I have an AST derived from the ANTLR Parser Generator for Java. What I want to do is somehow construct a control flow graph of the source code, where each statement or expression is a unique Node. I understand there must be some recursiveness to…
user5915
  • 253
  • 2
  • 5
  • 8
16
votes
7 answers

equivalent of Python's "with" in Ruby

In Python, the with statement is used to make sure that clean-up code always gets called, regardless of exceptions being thrown or function calls returning. For example: with open("temp.txt", "w") as f: f.write("hi") raise…
Claudiu
  • 224,032
  • 165
  • 485
  • 680
16
votes
3 answers

Why are simple for loop expressions restricted to integer ranges?

According to the F# spec (see §6.5.7), simple for loops are bounded by integer (int aka int32 aka System.Int32) limits start and stop, e.g. for i = start to stop do // do sth. I wonder why the iteration bounds for this type of for loop are…
Frank
  • 2,738
  • 19
  • 30
15
votes
3 answers

How to "break" out of a case...while in Ruby

So, I've tried break, next and return. They all give errors, exit of course works, but that completely exits. So, how would one end a case...when "too soon?" Example: case x when y; begin < ** terminate somehow ** >…
omninonsense
  • 6,644
  • 9
  • 45
  • 66
15
votes
2 answers

Execution flow in MVC

I am trying to learn MVC in detail, and I am wondering what's the exact functional flow internally, in the sense of which functions (important functions) are called and what they do when the application starts and what functions are called apart…
14
votes
3 answers

In Swift, what is a 'progression'?

According to the Control Flow section in the Swift Language Guide, The for-in loop performs a set of statements for each item in a range, sequence, collection, or progression. I'm pretty sure I know what three of these are: range: something…
David Moles
  • 48,006
  • 27
  • 136
  • 235
14
votes
4 answers

Convincing Swift that a function will never return, due to a thrown Exception

Because Swift does not have abstract methods, I am creating a method whose default implementation unconditionally raises an error. This forces any subclass to override the abstract method. My code looks like this: class SuperClass { func…
exists-forall
  • 4,148
  • 4
  • 22
  • 29
14
votes
6 answers

How to run code inside a loop only once without external flag?

I want to check a condition inside a loop and execute a block of code when it's first met. After that, the loop might repeat but the block should be ignored. Is there a pattern for that? Of course it's easy to declare a flag outside of the loop. But…
danijar
  • 32,406
  • 45
  • 166
  • 297
14
votes
3 answers

Get names of list in for loop

In PHP you can access both the names and values of an array in a for loop with foreach ( $array as $key => $value ) { Is there anything comparable in R, when looping over named lists?
naught101
  • 18,687
  • 19
  • 90
  • 138
13
votes
1 answer

How do I use AS with a switch in swift to get the class type

I have an array of SomeClass which is the super class of various other classes. The array has all of those random classes in it. Is there a way using switch (as opposed to else if let something = elm as? TheSubClassType) In pseudo code: for…
Itay Moav -Malimovka
  • 52,579
  • 61
  • 190
  • 278
13
votes
2 answers

what is the control flow of django rest framework

I am developing an api for a webapp. I was initially using tastypie and switched to django-rest-framework (drf). Drf seems very easy to me. What I intend to do is to create nested user profile object. My models are as below from django.db import…
cutteeth
  • 2,148
  • 3
  • 25
  • 45
13
votes
4 answers

Java: Exceptions as control flow?

I've heard that using exceptions for control flow is bad practice. What do you think of this? public static findStringMatch(g0, g1) { int g0Left = -1; int g0Right = -1; int g1Left = -1; int g1Right = -1; //if a match is found, set…
Nick Heiner
  • 119,074
  • 188
  • 476
  • 699
1 2
3
55 56