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

How to use else with a for loop?

Maybe that's a stupid question and a stupid mistake by me but i can't get this control flow to work. here is my simplified code: for x in range(1,10): print(x) if x==2: print("working") break else: print("stop") here is…
elunaery
  • 69
  • 1
  • 7
-2
votes
3 answers

Complicated if else logic in Java

I have three if statements: if (!(a == 0 && b == 0)) {...} if (!(a == 0 && b != 0)) {...} if (!(a != 0 && b != 0)) {...} I would like to combine them in one code block such as a method. I don't want the other statements to run if one has run. There…
Sandah Aung
  • 6,156
  • 15
  • 56
  • 98
-2
votes
3 answers

Simple java input Strings

import java.io.*; public class ManyTickets { public static void main (String [] args) throws IOException { String userInput; String userInput2; int intInput = 0; int intInput2 = 0; double total = 0.00; //(1) BufferedReader…
karthick
  • 68
  • 1
  • 6
-2
votes
1 answer

How do I use jQuery for flow control from an if statement?

example: if(login()) { // jQuery to change CSS attribute from hidden to visible } I want to present the user with a input form if logged in, otherwise they don't see the form. Thanks,
-2
votes
1 answer

How to create an if statement which loops until the if statement is no longer satisfied

In my Windows-Phone application I need to use a condition which needs to run for so long until it is no longer satisfied. How can I create some form of logic-loop? I have tried using an if statement but that only goes round once. I'm fairly new to…
Newbie
  • 1,160
  • 2
  • 11
  • 24
-2
votes
3 answers

homework: Trying to sort out what operation I am attempting to compute C

Sorry for asking you guys and not my professor. (its due in a couple of hours and she is not available) but I am just looking for a point in the right directions. one piece of what I am writing tonight states two things... Single-character…
RandomNumberFun
  • 638
  • 2
  • 8
  • 25
-2
votes
1 answer

new to this, flow of control no working the way I think it should

So I'm trying to make an automation script to help rip a very large CD library to mp3 with a program called CDex. The problem is that the script doesn't stay open. The last time I tried to make a script it was really bad, but at least it worked.…
-3
votes
4 answers

how to avoid logic errors in python using the control flow and nested if statements?

I'trying to write code that helps you decide whether you should or shouldn't eat food if you have droppped it. But it doesn't print the decision if too many conditions are met e.g if someone who is a boss/lover/parents has seen me droping the food…
-3
votes
2 answers

How can I 'continue' the outer most for loop when an if statement is satisfied?

How can I jump to the next value (not the next val) when the condition is satisfied? Example: lines = iter(open('something.txt', 'r')) for value in list: for val in value: for v in val if v == "!": #execute code …
-3
votes
4 answers

multiplying the elements of an array - converting a for loop into a while loop

so I figured out how to write this using a for loop int mult_for(int* array, int len) { int mult = 1; for (int i = 0; i < len; i++) { mult *= array[i]; } return mult; } but I'm unsure of how to go about doing the same thing…
balbat
  • 1
  • 1
-3
votes
1 answer

Why is the print statement running twice?

image of the executed codeThe code executes, but the print statements run twice. Can someone explain why is it so? n = input('you are lost in the woods, turn left or right\n**********\n**********\n:)\n**********\n**********\n') m = 0 if n ==…
-3
votes
3 answers

Exiting a for loop using a conditional

I had a problem which was that I was using a loop e.g for(let i=0; i<10; i++){ if(i === 3){ // go to the next iteration of the loop } console.log(i) } and I was struggling to see how to get to the next iteration. I tried a "return" statement…
Matthew Player
  • 318
  • 4
  • 17
-3
votes
1 answer

JavaScript: incorrect if statement is executed

I'm have several if statements which prints out the strings from corresponding list depending on the input (feelsLike). Code First it prints out the range in which feelsLike is in and then adds everything in the list to the string. From the…
user10132084
-3
votes
1 answer

need proper flow control logical condition in javascript

Below is my truth table ( OP is desired OUTPUT ) X | Y | Z | OP -------------------------- F | F | F | F F | F | T | F F | T | F | T F | T | T | T T | F | F | T T | F | T | F * T…
xkeshav
  • 53,360
  • 44
  • 177
  • 245
-3
votes
1 answer

Java code for converting C program to Control Flow Graph

I require a java code for converting a C program into a control flow graph. Can any one please help me out with it?
1 2 3
55
56