Photo by Tobias Carlsson on Unsplash

Your programming assistant #1: the control flow concept in programming languages

Luiz Henrique Guerra

--

I've been training lots of fresh programmers in the last years, and I'd like to share with you one of the most important concepts (in my opinion, of course) that make people have that "click" moment when they feel the understanding is happening: control flow.

The concept

In programming, control flow refers to the order in which the statements in a program are executed. The way in which the statements are executed is determined by control flow statements. These statements determine the path that the program takes and allow the programmer to specify the conditions under which specific blocks of code are executed.

Breaking it down

There are three main types of control flow statements: conditional statements, loop statements, and jump statements.

Conditional Statements

Conditional statements in programming are control flow statements that allow the program to make decisions based on whether a certain condition is true or false. They are used to specify the conditions under which specific blocks of code are executed.

The most common type of conditional statement is the “if” statement. The basic syntax of an “if” statement is as follows:

if (condition) {
// code to be executed if condition is true
}

An “if” statement can also be combined with an “else” statement to specify the code to be executed if the condition is false:

if (condition) {
// code to be executed if condition is true
} else {
// code to be executed if condition is false
}

In addition to “if” and “else” statements, there are also “else if” statements, which allow for multiple conditions to be tested:

if (condition1) {
// code to be executed if condition1 is true
} else if (condition2) {
// code to be executed if condition1 is false and condition2 is true
} else {
// code to be executed if condition1 and condition2 are both false
}

Loop Statements

Loop statements in programming are control flow statements that allow the program to repeat a block of code until a certain condition is met. They are used to execute a block of code multiple times, either a specified number of times or until a certain condition is true.

There are two types of loop statements: “for” loops and “while” loops.

A “for” loop is used to repeat a block of code a specified number of times. The basic syntax of a “for” loop is as follows:

for (initialization; condition; increment) {
// code to be executed in the loop
}

The initialization is executed before the loop starts, condition is checked before each iteration of the loop, and increment is executed after each iteration of the loop. If condition is true, the loop continues to execute; if condition is false, the loop terminates.

A “while” loop is used to repeat a block of code while a certain condition is true. The basic syntax of a “while” loop is as follows:

while (condition) {
// code to be executed in the loop
}

The condition is checked before each iteration of the loop. If condition is true, the loop continues to execute; if condition is false, the loop terminates.

Jump Statements

Jump statements in programming are control flow statements that allow the program to transfer control from one part of the code to another. They are used to change the normal flow of a program’s execution.

There are three main types of jump statements: break, continue, and return.

The break statement is used to exit a loop prematurely. When a break statement is encountered within a loop, the loop is terminated and the program continues with the next statement after the loop.

The continue statement is used to skip the current iteration of a loop and move on to the next iteration. When a continue statement is encountered within a loop, the program skips the rest of the statements in the current iteration of the loop and moves on to the next iteration.

The return statement is used to exit a function and return a value to the calling code. When a return statement is encountered within a function, the function is terminated and the program returns control to the calling code. The return statement can also be used to return a value to the calling code.

There will be more

This is a series about programming concepts that will be posted regularly, so don't forget to follow to keep posted!

--

--

Luiz Henrique Guerra
Luiz Henrique Guerra

Written by Luiz Henrique Guerra

Just trying to make some thoughts last. I like to write about software development and agility