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 wont the else section of this function execute?

The Pen is here I've got two elements set to change width, increase or decrease, when a link/button is clicked based on the presence of a class for the two elements. Only the if section of the if...else in the .toggle function seems to be working.…
tjfwalker
  • 494
  • 3
  • 18
-1
votes
2 answers

Haskell: Continue program execution

I am very new to Haskell. My question might be very basic for you. Here I go- I am writing a program to create a series of numbers using a specific mathematical formula. After creating this series, I am supposed to perform some operation on it like…
BW12
  • 13
  • 1
  • 6
-1
votes
1 answer

What is wrong with the if/else code in Java? its not working

I'm trying to programme in android, but my if/else code is not working. Basically, its a quiz application that determines whether a person's answers are correct or not. But whatever the answer, the output is the output given in…
-2
votes
9 answers

Is it possible to execute code even after the program exits the main()

Is there any possibility to execute code even after the program exits the main() method?
Prabhu
  • 723
  • 2
  • 10
  • 29
-2
votes
1 answer

indentation to make software control flow apparent. What do you think about it?

How many times have you encountered a function with a return statement deep within the code? Or failed to notice a break or a continue in a loop? While I have not been blindsided by return recently, I have still been caught off guard by continue and…
-2
votes
1 answer

Is there a pythonic way to control the flow depending on kwargs in a function?

i have register_users list that have list of user objects and every user object have password, username, email property i want to create a login function that get a **kwargs from input. the kwargs input can be given to the function in 3 different…
-2
votes
2 answers

Create a function "Are you playing banjo?". If your name starts with the letter "R" or lower case "r", you are playing banjo

Can anybody explain where I'm going wrong using the ruby program? I'm having trouble remembering how to be specific enough for the computer to understand without writing too long of a code any advice would be much appreciated? def…
DCARSON99
  • 7
  • 1
-2
votes
2 answers

Else statement keeps executing PYTHON

This functions searches for records in my database. Apart from if condition the else statement ( "Record not found") also keeps on executing even if the condition is true. OUTPUT screenshot def displaySearchAcc(): try: command = "SELECT * FROM…
-2
votes
1 answer

Why I am not getting desired output for my c++ problem

I am solving a question which states: to change every '?' with 'a' in a string if doesn't contain if won't form consecutive 'a' else substitute with 'b', eg. a?b will be abb and not aab because here 2 a's are consecutive. My problem is for i = 3 my…
-2
votes
3 answers

Using ternary operator inside a function in js

I've got a question: whenever the code runs the output is false. Why? function isGreaterThan (numberOne, numberTwo) { switch (isGreaterThan) { case numberOne > numberTwo: return true; break; default: return false; …
-2
votes
1 answer

Is there a more concise method to initialize these variables?

m is either 0, 1, 2, or 3: if m==0 afit0=afit(1); elseif m==1 afit0=afit(1); afit1=afit(2); elseif m==2 afit0=afit(1); afit1=afit(2); afit2=afit(3); elseif m==3 afit0=afit(1); afit1=afit(2); afit2=afit(3); …
-2
votes
2 answers

Confusion with block statements

Noob Question I was going through the ES6 features website and I found this piece of code function f (x, y, z) { if (y === undefined) y = 7; if (z === undefined) z = 42; return x + y + z; }; alert(f(1)) When I…
Brr Switch
  • 974
  • 1
  • 9
  • 21
-2
votes
2 answers

If function in common lisp

I have the following function (defun testIf (n) (if (<= n 0) t) print "Hello World") My issue is when I test (testIf -1), it returns "Hello World". Therefore I am wondering why the if was completely ignored. Keep in mind, I just want an if in…
JmanxC
  • 377
  • 2
  • 16
-2
votes
1 answer

Control flow and syntax errors with elif

I am trying to create a program that executes the following: secret_num == generate a random # between 1-10 ask user to guess this number between 1-10 if guess == secret_num congratulate them and provide the number of guesses number_guesses it took…
-2
votes
1 answer

Two Control Variables in R for loop (elegantly)

After searching for a while I haven't found an elegant solution to this (usually pedantic answers like "just vectorize it" which may not apply all the time), so I thought I'd ask. The simple problem is this: I need to loop over 2 control…
Rufus Shinra
  • 383
  • 1
  • 9