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

Why does not my for loop work?

The code is as below. I want it to exit the loop when you type -1. Or I would like to find a better solution if there is one. if (aa == "hours") { std::cout << "Please enter hours below: (Type -1 to stop)" << std::endl; for(x; x > -1; x++)…
midcore
  • 5
  • 1
-1
votes
1 answer

Guard does not exit function

This might be a very stupid question but bear with me: To my understanding and after reading the documentation and various examples on other websites, a guard statement checks a bool. If it's true, the current scope continued in execution. If not,…
Marmelador
  • 947
  • 7
  • 27
-1
votes
1 answer

So i'm currently learning javascript from codecademy.com , i have got a issue trying to solve a code about control flow

So , i'm currently learning control flow and i have to build a program with else if conditions. The problem is that i keep getting this errors for else ifs : "Expected an identifier and saw else" and "Expected an assigment or function call and…
-1
votes
1 answer

C# Placing "Return" into a function?

Is there any way to manipulate the program flow in C# from within a function? I mean in C++ it was possible to place something like #define Verify(x) if(x==null) return; or #define Verify(x) if(x==null) goto _exit; into a macro and place Verify(x)…
be_mi
  • 529
  • 6
  • 21
-1
votes
1 answer

Flow Control, If statement looping, else statement not executing

Within the following code, I can't get past the first if statement and proceed to the else statement, even when I purposely make it false: while not scraped: print("sleep....") time.sleep(1) try: res = requests.get(url).content …
ColeWorld
  • 279
  • 1
  • 3
  • 15
-1
votes
1 answer

Loop the following code until if statement is True

Having trouble looping this code until the correct keywords are found, only then should this code continue executing past the comment line... As of now this code continues past the comment line whether the given keywords are found or not. from bs4…
ColeWorld
  • 279
  • 1
  • 3
  • 15
-1
votes
1 answer

Unexplainable flow with MVC async

I have limited experience with .NET async Tasks, but I've used BackgroundWorker and IAsyncResult in the past. The code below is in an MVC4 project. When there are no errors, everything works fine. The problem is when something fails. There seems to…
Echilon
  • 10,064
  • 33
  • 131
  • 217
-1
votes
1 answer

My code not working from case 5 up to 9

My code is working fine from case 2 upto case 4. But from case 5 up to case 9,it just produces the output of inside switch statements and do not check for if else conditions below for case 5 upto case 9. I think there is some minor error, I am not…
Uzma Khan
  • 139
  • 1
  • 3
  • 14
-1
votes
1 answer

How to wait for termination of a running function in a gui callback function?

My program runs a function when user clicks on an axes object. This function uses the position of cursor and shows its progress as an animation. What I need is to stop currently running call of function when user clicks a new position, and then call…
saastn
  • 5,717
  • 8
  • 47
  • 78
-1
votes
1 answer

Which of the following are valid keywords in Go for controlling a loop?

I saw this question that for correct answer had 'for and range'. But the for statement is the only available looping statement in Go,and the range keyword allows you to iterate over items of a list like an array or a map. For understanding it, you…
-1
votes
8 answers

If a is false, then does if(a) === if(false)?

I am working on a JavaScript exercise on an interactive website, and I just really need some assistance in understanding the logic behind this... The exercise asks that you define a variable programming, and set it equal to false. var programming =…
Nick
  • 47
  • 1
  • 2
  • 12
-1
votes
2 answers

How to see if an array has 4 of the same values. Java

I have a poker program and in that program I have method that checks if there is a four of kind in an array. public boolean isFourOfAKind(Card[] arr){} I change arr[] to an int array by grabbing each of the cards values, 2-14 and store them in a…
Sam Liokumovich
  • 91
  • 1
  • 2
  • 6
-1
votes
2 answers

SSIS script task control flow

I have 3 script tasks : Task1, Task2, and Task3. Now I need a control flow such that if both Task1 and Task2 completes successfully, Task3 should execute. I tried to use Precedence constraint, but don't know how it will work.
patel
  • 1
  • 1
-1
votes
4 answers

Not equals comparison on cased letters

I have this: salir = "" while salir != "s" or salir != "S": print("no has salido") salir = input("Digit s to exit or enter to continue) print("saliste") but the operator != doesnt work, but if I put this: salir = "" while not (salir…
HAM
  • 11
  • 3
-1
votes
2 answers

Function not working in this specific code - interprets wrong "if" statement - Python

Python function that I just wrote is keep on looping up to a hundred. On the interpreter (or rather RUN), I am entering "5", but it executes the wrong "if" statement. def range_v2(c): int(c) if c == 5: for x in range(1, 50, +5): …