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
23
votes
2 answers

Any other reason why we use if-let rather than if in Rust?

I don't see the reason why we use if let and just usual if. In Rust book, chapter 6.3, the sample code is below: let some_u8_value = Some(0u8); if let Some(3) = some_u8_value { println!("three"); } The code above is same with: let some_u8_value…
Yosi Pramajaya
  • 3,895
  • 2
  • 12
  • 34
22
votes
7 answers

How do I return from a function inside a lambda?

Consider the following toy code to determine whether a range contains an element: template bool contains1(Iter begin, Iter end, const T& x) { for (; begin != end; ++begin) { if (*begin == x) return true; …
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
22
votes
1 answer

How do I conditionally return different types of futures?

I have a method that, depending on a predicate, will return one future or another. In other words, an if-else expression that returns a future: extern crate futures; // 0.1.23 use futures::{future, Future}; fn f() -> impl Future
Dominykas Mostauskis
  • 7,797
  • 3
  • 48
  • 67
22
votes
3 answers

Swift for-in loop dictionary experiment

I'm almost a complete programming beginner and I've started to go through a Swift ebook from Apple. The things I read are pretty clear, but once you start to experiment things get tricky :). I'm stuck with the experiment in the Control Flow section.…
al_x13
  • 305
  • 1
  • 3
  • 7
21
votes
4 answers

Intellij Idea hint: Condition is always false - can that be true here? (Java)

I have the following code: public String testExitPoints() { boolean myBoolean = false; try { if (getBoolean()) { return "exit 1"; } if (getBoolean()) { throw new RuntimeException(); } …
Mathias Bader
  • 3,585
  • 7
  • 39
  • 61
20
votes
2 answers

Python Walrus Operator in While Loops

I'm trying to understand the walrus assignment operator. Classic while loop breaks when condition is reassigned to False within the loop. x = True while x: print('hello') x = False Why doesn't this work using the walrus operator? It ignores…
20
votes
6 answers

Is there a shortcut to unwrap or continue in a loop?

Consider this: loop { let data = match something() { Err(err) => { warn!("An error: {}; skipped.", err); continue; }, Ok(x) => x }; let data2 = match something_else() { Err(err) =>…
Yuri Geinish
  • 16,744
  • 6
  • 38
  • 40
19
votes
3 answers

What is the best control flow module for node.js?

I've used caolan's async module which is very good, however tracking errors and the varying way of passing data through for control flow causes development to sometimes be very difficult. I would like to know if there are any better options, or what…
Robin Duckett
  • 2,094
  • 3
  • 16
  • 15
19
votes
4 answers

SSIS Control Flow vs Data Flow

I don't entirely understand the purpose of control flow in an SSIS package. In all of the packages I've created, I simply add a data flow component to control flow and then the rest of the logic is located within the data flow. I've seen examples…
Paul
  • 1,590
  • 5
  • 20
  • 41
18
votes
3 answers

Node.js control flow: callbacks or promises?

I know there are many control flow libraries for node.js. Some of them let one chain async functions with callbacks (like async, asyncblock, etc), the others use promise concept (Q, deferred, futures, etc). Given a long running script making a…
nab
  • 4,751
  • 4
  • 31
  • 42
18
votes
2 answers

C++ Else statement in Exception Handling

I would like to know if there is an else statement, like in python, that when attached to a try-catch structure, makes the block of code within it only executable if no exceptions were thrown/caught. For instance: try { //code here } catch(...)…
J3STER
  • 1,027
  • 2
  • 11
  • 28
18
votes
4 answers

How to pattern match optionals in Kotlin?

Is it possible to write something like this, or do we have to revert back to manual null checking in Kotlin? val meaningOfLife : String? = null when meaningOfLife { exists -> println(meaningOfLife) else -> println("There's no meaning") }
TomTom
  • 2,820
  • 4
  • 28
  • 46
18
votes
11 answers

Do Perl loop labels count as a GOTO?

Generally, it is good practice to avoid GOTOs. Keeping that in mind I've been having a debate with a coworker over this topic. Consider the following code: Line: while( <> ) { next Line if (insert logic); } Does using a loop label…
Kavet Kerek
  • 1,305
  • 9
  • 24
18
votes
12 answers

Control Flow via Return vs. If/Else

Which one is better (implicit control flow via return or control flow via if) -- see below. Please explain what you see as advantage/disadvantage to either one. I like option A because it's less code. Flow via Return: public ActionResult…
Alex
  • 75,813
  • 86
  • 255
  • 348
17
votes
7 answers

How to print the next N executed lines automatically in GDB?

I have been trying to find a way for some time to automate the progress in GDB of tracing the control flow of a program. Even just a simple way of automating the n command so you can see in what order routines are called. I realise that you can…
sice
1
2
3
55 56