Skip to main content
Logo image

Subsection Additional Flow Control

MATLAB provides commands that modify normal loop flow. These are most often used inside an if-statement within a loop.
Table 39. Flow control commands for loops
Command Effect
continue Skip the rest of the current iteration and move to the next iteration.
break Exit the loop immediately and continue after end.
return Exit the current script or function immediately.
Important notes:
  • These commands only execute if MATLAB reaches the line where they appear.
  • continue and break may only be used inside a for- or while-loop; using them elsewhere causes an error.
  • return can be used anywhere inside a script or function to exit early.

🌌 Example 40. Searching with break.

A loop can search for a target value and stop early when found. The break command exits the loop immediately.
target = 10;

found = false;
location = -1;

for k = 1:50
		value = randi([-20 20]);   % generate one random integer

		if value == target
				found = true;
				location = k;
				break
		end
end

if found
		fprintf('The number %i was found on iteration %i\n', target, location)
else
		fprintf('The number %i was not found in 50 trials\n', target)
end
This patternβ€”test a condition, then exit early with breakβ€”is useful whenever you want to stop searching once you find what you need.

🌌 Example 41. Example: Skipping Multiples with continue.

Sometimes you want to skip certain iterations in a loop without exiting early. The continue command jumps to the next iteration immediately, skipping any remaining code in the current iteration.
For example, suppose you want to print all numbers from 1 to 10, but skip multiples of 3:
for num = 1:10
	if mod(num, 3) == 0
		continue
	end
	fprintf('%i ', num)
end
fprintf('\n')
Output: 1 2 4 5 7 8 10
When num is 3, 6, or 9, the continue command skips the fprintf statement and moves directly to the next value. This is cleaner than nesting the print statement inside an else block.

Checkpoint 42.

(a) What does continue do?

What does the continue command do in a loop?
  • Skips the rest of the current iteration and moves to the next iteration.
  • The continue command immediately jumps to the next iteration, skipping any remaining code in the current iteration.
  • Exits the loop completely.
  • This describes break, not continue. The continue command stays in the loop but skips to the next iteration.
  • Restarts the loop from the beginning.
  • The continue command moves to the next iteration, not back to the first one.
  • Exits the current function or script.
  • This describes return, not continue.

(b) Break exits the loop.

    The break command exits the loop immediately and continues execution after the end statement.
  • True.

  • Unlike continue which skips to the next iteration, break completely exits the loop.
  • False.

  • Unlike continue which skips to the next iteration, break completely exits the loop.

(c) Where can continue and break be used?

Where can the continue and break commands be used?
  • Only inside for- or while-loops.
  • These commands only make sense inside loops. Using them elsewhere causes an error.
  • Anywhere in a script or function.
  • This describes return. The continue and break commands require a loop context.
  • Only inside if-statements.
  • While often used with if-statements, they must be inside a loop, not just an if-statement.
  • Only at the end of a loop.
  • These commands can appear anywhere inside the loop body.

(d) When does return exit?

What does the return command do?
  • Exits only the current loop.
  • This describes break. The return command exits the entire script or function.
  • Exits the current script or function immediately.
  • The return command stops execution of the entire script or function, not just a loop.
  • Returns to the start of the loop.
  • The return command doesn’t return to any previous location; it exits completely.
  • Skips to the next iteration.
  • This describes continue, not return.

πŸ§‘πŸ»β€πŸ’» Class Activities πŸ§‘πŸ»β€πŸ’» Class Activities

These activities will help you practice using loops to solve interesting problems. Each activity requires only loops, variables, and basic arithmeticβ€”no arrays needed.

1. Using continue in a Loop.

(a)
Write a MATLAB script with a for-loop from 1 to 20 that adds the loop index (1..20) to a running total (for example, stored in a variable total). Use an if-statement with mod and continue so that values divisible by 3 are skipped.
(b)
Modify your script to also count how many values were skipped. Print both the final total and the number of skipped values.

2. Using break in a Loop.

(a)
Use a while-loop to generate random integers with randi([1, 10]). Keep a trial counter and use an if-statement with break to stop as soon as the value is 7.
(b)
Add a second stopping rule: if the trial counter reaches 15 before rolling a 7, break out of the loop and report that the trial limit was reached.

3. Using return in a Loop.

(a)
Write a function named find_first_multiple_of_six that loops through integers from 1 to limit. Inside the loop, use an if-statement and return to stop immediately when the first multiple of 6 is found.
(b)
Update your function so that it returns -1 if no value in the loop meets the condition. Test with a small limit and a larger limit to confirm both outcomes.