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

Java Comparison of If/While Loop

Working on a leetcode problem described as: "Given a binary array nums and an integer k, return the maximum number of consecutive 1's in the array if you can flip at most k 0's." I am using a sliding window approach and had produced the below…
Siggyweb
  • 15
  • 6
-1
votes
1 answer

Unsure why output is 0. Trying to count months to pay downpayment

print("Please enter you starting annual salary: ") annual_salary = float(input()) monthly_salary = annual_salary/12 print("Please enter your portion of salary to be saved: ") portion_saved = float(input()) print ("Please enter the cost of your…
-1
votes
1 answer

addEventListener and control flow

Basically, when I put an addEventListener in my code, does the script stop executing until the event happens and gets listened or the code keeps getting executed no matter what? I mean, if an event that I want to listen and that is placed at the…
-1
votes
1 answer

How do I tell my program to go back to a certain point after the wrong answer is selected?

In my midterm CYA game, you come across 2 staircases, left and right. You have the option to choose which one you go to, the right advancing you and the left (eventually) sending you back because it is blocked. I don't know how to get it to do that…
MEDAKk
  • 1
  • 1
-1
votes
2 answers

Python list updaitng even when condition evaluates to False

Why is the longest_palindrome variable updated when the isPalindrome() function evaluates to False? The if/else condition is not behaving as I would expect. def palindromeCutting(s): if s == "": return "" letters = list(s) …
Zissou
  • 21
  • 3
-1
votes
1 answer

In what order does python execute print statements?

print("Klay", print("Thompson")) Running this code gives an output of Thompson Klay None Why does the internal print execute before the outer one? I expected the output to be Klay Thompson None
Kun.tito
  • 165
  • 1
  • 7
-1
votes
4 answers

Why is the last condition not executing?

The first two conditions for 100 int main() { int a,b; printf("Enter the first…
-1
votes
2 answers

Using increment inside an if statement

What is the difference between if(++x < 0){something} and if(x + 1 < 0){something} Thanks in advance
Sunny267
  • 27
  • 6
-1
votes
1 answer

Control flow in python with strings

I have a very basic snippet of code. print("a" or "b") >> a I'm not entirely sure what I expected to happen. Why does this print "a", and in general how are strings handled with python control flow?
relot
  • 651
  • 1
  • 6
  • 18
-1
votes
1 answer

Understanding Control flow in Exception Handling in Python

a=0 try: print("In Try Block !") a = 10/0 except Exception as e: print("In Exception Block !") raise e finally: print("In Finally Block !") print(a) pass When I run the above code I get output as below. I don't get the…
-1
votes
1 answer

What's the standard practice for naming labels in Go?

Go spec and Effective Go gives guidelines for naming packages, types, functions, and variables. There's also a blog post discussing about package names. But none of articles seem to talk about naming conventions of labels used with goto, break and…
Coconut
  • 2,024
  • 18
  • 25
-1
votes
2 answers

Tricky python control flow

I was working on a project and have replicated my doubt by a simple example. I am unable to understand the control structure of the following python code snippet. list1 = [1,2,3,4,5] for item in list1: if item == 3: print("Found") …
Omkar
  • 791
  • 1
  • 9
  • 26
-1
votes
1 answer

how to use if statements with expect in Tcl for telnet?

automated telnet session getting the weather: thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ tclsh main.tcl connect to wunderground with: ----------------- 1) noControlFlow 2) …
Thufir
  • 8,216
  • 28
  • 125
  • 273
-1
votes
3 answers

How does using two variables in for loop work

I started reading the python documentation. I ended up with this statement: Code that modifies a collection while iterating over that same collection can be tricky to get right. Instead, it is usually more straight-forward to loop over a copy…
-1
votes
1 answer

Understanding excution order in nested functions Python 3.8

Total newbie herem trying to understand the flow of execution,, look at the following code: from datetime import datetime from time import * def time_decorator(function,a=0): a +=1 print(a) def wrapper(*args,**kwargs): t1=…