Photo by Shay on Unsplash

Your programming assistant #4: what about jump statements?

Luiz Henrique Guerra

--

Jump statements in programming are used to alter the control flow of a program. They allow a programmer to transfer the control of the program to a different part of the code. In the article about control flow we saw the basics of it, and now we’re going deeper to understand how they work.

Break Statements

They are used to terminate a loop statement such as for, while or do-while loop. When a break statement is encountered inside a loop, the control of the program is transferred to the next statement outside the loop.

Here’s an example of how the break statement can be used in JavaScript:


for (let i = 0; i < 10; i++) {
if (i === 5) {
break;
}
console.log(i);
}

// Output:
// 0
// 1
// 2
// 3
// 4

In this example, we use a for loop to iterate from 0 to 9. Inside the loop, we check if the current value of i is equal to 5. If it is, we use the break statement to terminate the loop early. As a result, the loop stops executing when i is equal to 5 and the program skips over the remaining iterations.

In the output, you can see that the program only logs the values of i from 0 to 4. When i is equal to 5, the loop is terminated and the program continues to execute the code after the loop.

Continue Statements

They are used to skip the current iteration of a loop and move to the next iteration. When a continue statement is encountered inside a loop, the control flow of the program is transferred to the beginning of the loop for the next iteration.

Here’s an example of how the continue statement can be used in PHP:

$numbers = array(1, 2, 3, 4, 5);

foreach ($numbers as $num) {
if ($num == 3) {
continue;
}
echo $num . "\n";
}

// Output:
// 1
// 2
// 4
// 5

In this example, we use a foreach loop to iterate over an array of numbers from 1 to 5. Inside the loop, we check if the current value of $num is equal to 3. If it is, we use the continue statement to skip over the current iteration of the loop and move on to the next iteration. As a result, the program skips over the number 3 and moves on to the next number in the array.

In the output, you can see that the program only prints the numbers 1, 2, 4, and 5. When $num is equal to 3, the continue statement is used to skip over that iteration of the loop and move on to the next number.

Return Statements

They are used to terminate the execution of a function and return a value to the caller. When a return statement is encountered inside a function, the control of the program is transferred back to the caller and the value specified in the return statement is returned.

Here’s an example of how the return statement can be used in Ruby:

def add_numbers(a, b)
return a + b
puts "This code will never execute"
end

result = add_numbers(3, 4)

puts result

# Output:
# 7

In this example, we define a method called add_numbers that takes two parameters a and b. Inside the method, we use the return statement to return the sum of a and b. Any code after the return statement will never execute.

We then call the add_numbers method with the values 3 and 4, and store the result in a variable called result. Finally, we print the value of result to the console.

In the output, you can see that the value 7 is printed to the console. This is because the add_numbers method returns the sum of a and b, which is 7. Any code after the return statement is never executed, so the puts statement inside the method is not executed.

This article ends the first chapter of your programming assistant. Understanding control flow is essential to enable people to read and understand code, as well as write it consistently. Keep posted for the next concepts!

--

--

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

No responses yet