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
-3
votes
3 answers

What is wrong with this ruby for loop?

for current_iteration_number in 99..1 do puts "#{current_iteration_number} of beer on the wall. #{current_iteration_number} of beer. Take one down, pass it around #{current_iteration_number} of beer." end
-4
votes
2 answers

difference between controlling the flow with if and elif

i was running a program that return the maximum 2 number in a certain list and I found that there a difference between using if and elif in definition of function So if i used : def maxi (lst) : max_pos1 = 0 max_pos2 = 1 if…
-4
votes
3 answers

BEGINNER HERE If/else statements with booleans JAVASCRIPT

I am having some trouble with this current lesson on control flow with JavaScript... The question states: In this exercise, you will be given a variable, it will be called value. You will also be given another variable, it will be called…
-4
votes
3 answers

Ansi C: High flow control/Replace just the first occurence with a *star* and delete each duplicate

Here's another brain teaser i'm having issues with. The question is pretty easy to understand: A is a char array of length N. Write a replace(A) function which will delete not only every duplicate, but also replace just the first occurence with…
Yaron Pray
  • 13
  • 1
  • 3
-4
votes
1 answer

PYTHON (Controls & conditional flow)

# Make sure that the_flying_circus() returns True def the_flying_circus(): if ______: # Start coding here! # Don't forget to indent # the code inside this block! elif _____ # Keep going here. # You'll want…
-4
votes
1 answer

elif statement never executing

if IsInBrew() == 'InBrew': # first lets work out where we are... thisBrew = getCurrentBrewID() latestStage = getCurrentStage(thisBrew) elapsedtime = now - latestStage[1] hours = divmod(elapsedtime.total_seconds(),3600) …
garyk1968
  • 121
  • 3
  • 8
-5
votes
1 answer

Why am I getting this output in C++? Explain the logic

#include #include int main() { if(NULL) std::cout<<"hello"; else std::cout<<"world"; return 0; } The output to the above question is: world Kindly explain me why am I getting this output. I am not able to get the…
Palak Jain
  • 17
  • 6
-5
votes
1 answer

Advanced Rudimentary Computing?

Lets say that my definition of 'rudimentary programming' refers to the fundamental tools employed for a computer to perform a task. Considering programming rudiments, the learning spectrum usually looks something like this: Variables, data types…
-5
votes
3 answers

Why does this compile, when the return value could presumably be unassigned?

It seems to me that this code: public static bool InsertInventoryItem(DuckbillUtils.InventoryItem invItem) { bool addSuccess = true; try { InventoryItemsList invItems = new InventoryItemsList(); …
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
-6
votes
1 answer

Nested if-else statement not working probably

i need some assistance with the program. When the number of people is greater than the maximum number of people allowed in the room, the last statement does not print. I am not sure if i did something wrong, or missed an important item. I don't…
leet
  • 9
  • 3
1 2 3
55
56