Computers follow a list of steps.
Computers follow a list of steps.
Sometimes, the computer must jump to a new spot. It might jump to a special mark called a label. This helps it skip parts or go back.
Other times, the computer makes a choice. It looks at a rule to decide where to go. This is like a fork in a road.
A computer can also do a task many times. This is called a loop. It can repeat a task until a rule says to stop.
These jumps and loops help computers do many things.
Computers follow a list of commands to finish a task. This path is called control flow. Usually, the computer goes from one command to the next in a straight line. This is called sequential execution.
Sometimes, the computer needs to change its path. It can use a jump. A jump moves the computer to a new spot in the code. Some jumps happen every time. These are called unconditional branches. Other jumps only happen if a rule is met. These are called conditional branches. For example, an if-then statement lets the computer choose a path based on a fact.
A computer can also use a function. A function is a set of code with its own name. When the computer calls a function, it jumps to that code. When the work is done, it returns to where it left off.
Another way to change path is a loop. A loop repeats a set of steps many times. A loop might run for a set number of times. It might also run until a certain rule becomes true. Some loops can even run forever!
Software follows a specific path to finish its work. This path is known as control flow. Most of the time, a computer follows a straight line. It moves from one command to the very next one. This is called sequential execution.
There are many ways to change this path. A jump is one of the simplest ways. A jump moves the computer to a new spot in the code without asking questions. This is often called an unconditional branch. Other times, the computer makes a choice. This is a conditional branch. It only jumps if a specific rule is met. For example, an "if" statement checks a fact first. If the fact is true, the computer takes one path. If it is false, the computer might take a different path.
Programmers have used different tools for this for a long time. In the early days of the Fortran language, there was a tool called an arithmetic if. This checked if a number was negative, zero, or positive. By the year 1990, this tool was considered old and was removed. Later, in Fortran 2018, it was gone for good. Many famous scientists have studied how to write code well. A scientist named Dijkstra even said that the "goto" jump command could be harmful. He wanted code to be more organized and clear.
Computers use many different types of commands to manage flow. A function is a group of code with its own name. When a program calls a function, it jumps to that code. Once the work is done, it returns to where it started.
Control flow is like following a map or a recipe. A recipe tells you to do things in order. But if a recipe says, "If the oven is hot, put in the cake," that is a conditional branch. If a recipe says, "Stir the batter ten times," that is a loop.
In computer software, control flow describes how a program moves from one command to the next. Most programs follow a sequential path. This means the computer executes one instruction and then moves to the one immediately following it. However, many tasks require more than a straight line. Control flow commands allow the program to deviate from this sequence. In machine code, these are often called instructions. In imperative programming languages, they are known as statements. Control flow is essential because it allows software to make decisions and repeat tasks.
There are several ways a computer manages these paths. A jump, also called an unconditional branch, moves the execution to a new point without checking any rules. A conditional branch is different. It only moves the execution if a specific Boolean expression is true. For example, an "if-then" statement checks a condition first. If the condition is met, the computer follows a specific path. Some systems use a method called predication. Instead of jumping to a new location, predication simply enables or disables certain instructions based on a condition.
Programmers use specific tools to organize these paths. One tool is a label, which marks a specific position in the source code. A jump command can then reference this label to know exactly where to go. Another tool is the block. A block organizes a sequence of code into a single unit. When a control statement is used with a block, the start of that block becomes a jump target. Functions also manage control flow. When a program calls a function, the execution jumps to the start of that function's code. Once the function finishes its work, control returns to the point where it was originally called.
Different programming styles handle control flow in unique ways. Imperative languages encode control flow explicitly through sequences and branches. In contrast, declarative languages focus on the desired results rather than the exact order of operations. Functional languages use a different approach. They use functions and specific language constructs to manage flow, though they do not always call them control flow statements. Even the underlying hardware plays a role. In a central processing unit (CPU), a control flow instruction often alters the program counter to change the execution path.
Loops are a major part of control flow used to repeat actions. A loop consists of a body of statements that execute multiple times. There are three main types of loops. A numeric loop iterates over a specific range of numbers. For example, a loop might start at 1 and end at 10. A condition-controlled loop continues until a certain rule is met. Some check the rule at the start, while others check it at the end. An enumeration loop iterates through every item in a collection, such as a list of names.
History shows how these methods have evolved over time. Early versions of the Fortran language included an "arithmetic if." This was a three-way branch that checked if a number was negative, zero, or positive. By Fortran-90, this was considered obsolete. It was eventually removed entirely in Fortran 2018. Computer scientists have also debated the best way to structure code. The scientist Dijkstra famously argued that the "goto" statement could be harmful. He believed that relying on unconditional jumps made code harder to understand and manage.
Control flow is also a critical area for computer security. Attackers sometimes try to redirect the flow of execution to run unauthorized code. To prevent this, developers use control-flow integrity techniques. These include methods like stack canaries, which are values used to detect errors. They also use buffer overflow protection and shadow stacks. Vtable pointer verification is another method used to defend against these attacks. By protecting the path the computer takes, engineers keep software running safely and predictably.
🖼️ Images & Media (1)
More to explore
✨ What else?
Related topics you might enjoy
🔬 Go deeper
More advanced topics to explore
🪜 Step back
Simpler topics to build understanding
What is Nepedia?
A free, ad-free encyclopedia for children. Every article is written at five reading levels, so the same page works for a five-year-old and a fifteen-year-old — use the level switcher above to see this one change. No account needed to read.