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

Possible ways to check if a routine can run (and run it) in C#

I want to know how can I check for pre-requisites of a routine when I call it, so that it might execute or notify its non-executability. Language is C#. I am implementing a Pipeline design-pattern with a Controller object, a DataContainer object,…
heltonbiker
  • 26,657
  • 28
  • 137
  • 252
0
votes
1 answer

Flow Control Instructions in a virtual machine

I've been implementing my own scripting language + virtual machine from scratch for a small experiment. A script reader parses the script and translates it to a stream of instructions that a runtime engine will execute. At the beginning I didn't…
user390507
  • 133
  • 1
  • 4
0
votes
7 answers

How to check if(Object) in Java?

I have a code in Java: for(Iterator it = c.getArrayList().iterator(); it.hasNext(); ) { Object i = it.next(); // Here I have an error, i is not a boolean if (i) { System.out.format("Delete %s%n", i); it.remove(); } …
Green
  • 28,742
  • 61
  • 158
  • 247
0
votes
1 answer

Hadoop data and control flow

I'm writing a Hadoop application but it seems that I have misinterpreted how exactly hadoop works. My Input files are tiles of a map, named according to the QuadTile principle. I need to subsample those, and stitch those together until I have a…
KarelV
  • 487
  • 1
  • 5
  • 20
0
votes
2 answers

jquery stop() all animations without letting deferreds $.when, .promise(), .done() return complete?

Seems like every 50-100 jsfiddle updates lead me to the stackoverflow community to help solve a new issue I create! so Thanks! Background: I have several functions containing animations, functions are to be fired after the previous one is…
twinturbotom
  • 1,504
  • 1
  • 21
  • 34
0
votes
5 answers

Should event handlers be the last statement in a method?

I have a method which is called onClick of some element. In that function I have an event handler( JQuery $().click() ), that detects the click of a button and performs some action. I have noticed that the event handler works fine as long as it is…
Sayan
  • 2,053
  • 3
  • 25
  • 36
0
votes
2 answers

execute multiple processes from a master process

I want to create multiple processes from one master process. I know I want to use a function from the exec family, but it does not seem to be preforming in the way I intended it to. It seems that exec() is a blocking call, or maybe I am just using…
nook
  • 2,378
  • 5
  • 34
  • 54
0
votes
1 answer

what information we can get from a control flow graph?

I have got a control flow graph of a trace of a C program(executed in a VM) which is highly complicated.I want to know what information can i extract if i have a CFG of a program trace apart from control dependencies ! Thank you
archies50
  • 103
  • 2
  • 9
0
votes
1 answer

async control flow in javascript

I'm writing a browser extension for chrome that uses Web SQL for local storage. The code for both of these components seems to heavily rely on async operations. I have a good understanding of asynchronous operations but not a whole lot of…
NSjonas
  • 10,693
  • 9
  • 66
  • 92
0
votes
1 answer

How to use methods containing mongoose functions with control flow frameworks (e.g. Async or Step)

I am struggling a little with how to call asynchronous functions in a serial manner. In particular, functions which incorporate mongoose calls to the database. I have a class definition which includes two methods: (MovieFile.exists) checks if a…
Benjen
  • 2,835
  • 5
  • 28
  • 42
0
votes
3 answers

How to go out of the conditional IF statement? Return doesn't work correctly

I have an example of a class. It creates a new card and puts it into the array. I want to control the number of cards and leave the flow if the number of cards is more than 54: public class Card { private final String rank; private final…
Green
  • 28,742
  • 61
  • 158
  • 247
0
votes
1 answer

mysql stored procedure: flow control

i'm practicing with this stored procedure on a MYSQL database to make payments between accounts. it takes inputs of two accounts(acct1, acct2), an amount(amt) to be paid and outputs a confirmation message(pmessage) it's supposed to make payment only…
okey_on
  • 2,888
  • 8
  • 28
  • 36
0
votes
1 answer

Issue using step (control flow library) and mongoose

If I query for all the documents in a collection the normal way, I have no issues: https://gist.github.com/2562954. The output of this is found docs [] However, if I introduce Step (https://github.com/creationix/step), then the output is not an…
Rafael
  • 540
  • 4
  • 8
0
votes
1 answer

GOTO, CONTINUE, BREAK in procedural programming, how do they effect state?

In my attempt to gain a better knowledge of procedural programing, both for practical and academic use I am trying to clarify the effect CONTINUE and BREAK statements have on state. I have come to understand that GOTO is essentially forbidden as I…
RyanS
  • 3,964
  • 3
  • 23
  • 37
-1
votes
2 answers

Nested if else statement is not working as desired

In the program, no errors. PASS, CLEARED user inputs are working as desired. But when I enter NO as user input in the nested if, control picks "else statement" correctly, but also it is going inside, and executing the next S.O.P display like "Have…