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

AngularJS weird flow

I've 2 functions in the scope f1 and f2, and I call f2 in the middle of f1.I can't get why f2 is called at the end of f1.(f2 edits the view). For example, with ..... $scope.f1 = function() { console.log("A"); $scope.f2(); …
-1
votes
1 answer

Is there a cleaner way to handle (consecutive) exclusive if-statement in for-loop?

I'm new to Python (I've only used C) and I've discovered new loops such as for/else... So I wonder if I'm ignoring a cleaner way to handle this loop: flag = 0 for i in range (n): if not flag and condition: statement_1 flag = 1 …
Domenico Modica
  • 167
  • 1
  • 8
-1
votes
1 answer

Code for finding if a "point" (x,y) in between a user inputed, xmax,xmin,ymax,ymin and printing "point" name using flow control commands

Given a nested list of points and locations (x,y): pointlist = [[[-174.6, 52], 'A'], [[-152, 58], 'B'], [[-166.1, 53], 'C']],[[90, -179.7], 'D']] I need help creating a code that will tell if a point is between a user inputted xmax, xmin, ymax,…
JDT198
  • 3
  • 1
-1
votes
1 answer

Loop condition using getch() does not end the loop as expected

I have following code, but it is not ending the while loop even if done is true. It keeps waiting for _getch(): while (done || ((c = _getch()) != EOF)) { cout << "Operation aborted" << endl; break; } cout << "Operation…
user1833852
  • 51
  • 2
  • 10
-1
votes
2 answers

Exit infinite while loop (python) when user enters "exit", but if users enters a number compare it to 50

i need help with this problem. ask user to enter a number inside an infinite while loop, compare this number to let say 50 (less, greater or equal). but in order to exit this while loop user must enter "exit". i have the following code that is…
-1
votes
2 answers

Redirecting the flow of the code for an Error Message

I want to redirect the flow of code to "1" part after the else statement. I have tried to achieve it by creating loops and different methods but I couldn't make it. The thing that I want is, it will give an error when an invalid answer is given…
E.Canberk
  • 9
  • 4
-1
votes
1 answer

Control flow for R does not behave as expected with multiple if statements

I have written the following function in R with two sequential if conditions: f <- function(a) { if (a > 10) { c <- 'a is greater than 10' c } if (a < 10) { c <- 'a is less than 10' c } } Calling f(1) returns 'a is less than 10',…
DLim
  • 93
  • 5
-1
votes
3 answers

If-Else Control Flow without curly braces

I am trying to run the below code with x =10 ."X is not equal to 10!" is getting printed. but As per documentation, the else belongs to the innermost if()? Also if we replace "1==2" in the if block with 1==1 then also "X is not equal to 10!" is…
parteek goyal
  • 41
  • 1
  • 5
-1
votes
2 answers

display output of an algorithm on webpage using javascript

I tried making an small fizzbuzz algorithm function fizzbuzz(num){ for (let i = 1; i <= num; i++) { if(i%3===0 && i%5===0){ console.log("Fizzbuzz"); } else if (i%3===0) { console.log("fizz"); } else…
siddhant
  • 69
  • 5
-1
votes
1 answer

Controlling program flow in Python, when should I handle an invalid or None response from a method?

I am new to Python and I want to know am controlling the flow of the program correctly. I am wondering if the following method is the correct way to return a value from a method. extract_nouns() retunes an empty string if there are no nouns in…
bloopiebloopie
  • 309
  • 1
  • 4
  • 11
-1
votes
1 answer

What the meaning of the ~once~ variable in the following code snippet?

The question is about the usage once variable in the following snippet extracted from the pipe.go of the standard go library for once := true; once || len(b) > 0; once = false { select { case p.wrCh <- b: nw := <-p.rdCh b =…
steveyang
  • 9,178
  • 8
  • 54
  • 80
-1
votes
2 answers

JavaScript Promise - logic when multiple promises fail

How can I apply logic in case a set of Promises are rejected (all of them)? validUserIdPromise = checkValidUserId(id) .then(() => console.log("user id") .catch(() => console.log("not user id") validCarIdPromise = checkValidCarId(id) .then(()…
nagy.zsolt.hun
  • 6,292
  • 12
  • 56
  • 95
-1
votes
1 answer

In JavaScript, can I use a ternary expression without the else part?

I am pretty new to JavaScript and I am trying to make a little control flow example but it doesn't want to work because of multiple errors.For example on line 5 it says unexpected token: let raceNumber = Math.floor(Math.random()*1000); let…
itsolidude
  • 1,119
  • 3
  • 11
  • 22
-1
votes
2 answers

Weird behavior with recursive function

I have this code: public void myFunction(String label, String type, string command, int attempts = 0) { try { Utility.Logger("myFunction attempt " + attempts.ToSafeString() + " label " + label + " type " + type,…
Piero Alberto
  • 3,823
  • 6
  • 56
  • 108
-1
votes
1 answer

Python List control flow - why won't skip elements already in list

Essentially trying to add numbers to list L based on a pattern. starting at index point 0 you print the element of that index point. The next index point then becomes becomes the int of the element you just printed and the pattern continues until…
jpazzajpeg
  • 133
  • 2
  • 9