ArticleslgStudy

science

Control flow

Control flow is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Control flow rather than just read about it. In short: In software, control flow (or flow of control) describes how execution progresses from one command to the next. In many contexts, such as machine code and an imperative programming language, control progresses sequentially (to the command located immediately after the currently executing command) except when a command transfers control to another point – in which case the command is classified as a control flow comm…

Control flow — main illustration
Control flow — illustration

Key takeaways

  • Control flow belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Control flow to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Control flow from memory before moving on to harder problems.

Reference excerpt

In software, control flow (or flow of control) describes how execution progresses from one command to the next. In many contexts, such as machine code and an imperative programming language, control progresses sequentially (to the command located immediately after the currently executing command) except when a command transfers control to another point – in which case the command is classified as a control flow command. Depending on context, other terms are used instead of command. For example, in machine code, the typical term is instruction and in an imperative language, the typical term is statement. Although an imperative language encodes control flow explicitly, languages of other programming paradigms are less focused on control flow. A declarative language specifies desired results without prescribing an order of operations. A functional language uses both language constructs and functions to control flow even though they are usually not called control flow statements. For a central processing unit instruction set, a control flow instruction often alters the program counter and is either an unconditional branch (a.k.a. jump) or a conditional branch. An alternative approach is predication which conditionally enables instructions instead of branching. An asynchronous control flow transfer such as an interrupt or a signal alters the normal flow of control to a handler before returning control to where it was interrupted. One way to attack software is to redirect the flow of execution. A variety of control-flow integrity techniques, including stack canaries, buffer overflow protection, shadow stacks, and vtable pointer verification, are used to defend against these attacks.

Structure Control flow is closely related to code structure. Control flows along lines defined by structure and the execution rules of a language. This general concept of structure is not be confused with structured programming which limits structure to sequencing, selection and iteration based on block organization.

Sequences Sequential execution is the most basic structure. Although not all code is sequential in nature, imperative code is.

Labels A label identifies a position in source code. Some control flow statements reference a label so that control jumps to the labeled line. Other than marking a position, a label has no other effect. Some languages limit a label to a number which is sometimes called a line number, although that implies the inherent index of the line, not a label. None-the-less, such numeric labels are typically required to increment from top to bottom in a file even if not be sequential. For example, in BASIC:

In many languages, a label is an alphanumeric identifier, usually appearing at the start of a line and immediately followed by a colon. For example, the following C code defines a label Success on line 3 which identifies a jump target point at the first statement that follows it (line 4).

Blocks Most languages provide for organizing sequences of code as a block. When used with a control statement, the beginning of a block provides a jump target. For example, in the following C code (which uses curly braces to delimit a block), control jumps from line 1 to 4 if done is false.

Control Many control commands have been devised for programming languages. This section describes notable constructs; it is organized by functionality.

Functions A function provides for control flow in that when called, execution jumps to the start of the function's code and when it completes, control returns the calling point. In the following C code, control jumps from line 6 to 2 in order to call function foo(). Then, after completing the function body (printing "Hi"), control returns to after the call, line 7.

Branches A branch command moves the point of execution from the point in the code that contains the command to the point that the command specifies.

Jump A jump command unconditionally branches control to another point in the code, and is the most basic form of controlling the flow of code. In a high-level language, this is often provided as a goto statement. Although the keyword may be upper or lower case or one or two words depending on the language, it is like: goto label. When control reaches a goto statement, control then jumps to the statement that follows the indicated label. The goto statement has been considered harmful by many computer scientists, including Dijkstra.

Conditional branch A conditional statement jumps control based on the value of a Boolean expression. Common variations include:

if-goto Jumps to a label based on a condition; a high-level programming statement that closely mimics a similar used machine code instruction if-then Rather than being restricted to a jump, a statement or block is executed if the expression is true. In a language that does not include the then keyword, this can be called an if statement. if-then-else Like if-then, but with a second action to be performed if the condition is false. In a language that does not include the then keyword, this can be called an if-else statement. Nested Conditional statements are often nested inside other conditional statements. Arithmetic if Early Fortran, had an arithmetic if (a.k.a. three-way if) that tests whether a numeric value is negative, zero, or positive. This statement was deemed obsolete in Fortran-90, and deleted as of Fortran 2018. Operator Some languages have an operator form, such as the ternary conditional operator. When and unless Perl supplements a C-style if with when and unless. Messages Smalltalk uses ifTrue and ifFalse messages to implement conditionals, rather than a language construct. The following Pascal code shows a simple if-then-else. The syntax is similar in Ada:

In C:

In bash:

In Python:

In Lisp:

Multiway branch A multiway branch jumps control based on matching values. There is usually a provision for a default action if no match is found. A switch statement can allow compiler optimizations, such as lookup tables. In dynamic languages, the cases may not be limited to constant expressions, and might extend to pattern matching, as in the shell script example on the right, where the *) implements the default case as a glob matching any string. Case logic can also be implemented in functional form, as in SQL's decode statement. The following Pascal code shows a relatively simple switch statement. Pascal uses the case keyword instead of switch.

In Ada:

In C:

In Bash:

In Lisp:

In Fortran:

Loops

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Control flow

Start with the simplest possible case. Write down what Control flow claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Control flow before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Control flow ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Control flow

In research
Control flow appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Control flow in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Control flow is common in secondary-school and first-year university syllabi. It links to neighbouring topics Control flow, Iteration in programming, Programming language comparisons, so understanding it makes those chapters shorter.
In everyday life
Look for Control flow outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Control flow” →

How to study Control flow in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Control flow means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Control flow out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Control flow in simple terms?

In software, control flow (or flow of control) describes how execution progresses from one command to the next. In many contexts, such as machine code and an imperative programming language, control progresses sequentially (to the command located immediately after the currently executing command) e…

Why does Control flow matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Control flow?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Control flow.

Tags

  • Control flow
  • Iteration in programming
  • Programming language comparisons
  • Recursion

Keep exploring