Learning Goal #
- Use for-loops to run a section of code repeatedly until a certain condition has been satisfied.
1. For loops #
1.1. What is it? #
A for-loop in programming allows you to repeat a section of code until a condition has been satisfied. It’s a way to instruct the computer to do something for each item in a list or every number in a range, one by one, or every peak in a chromatogram. until all items were evaluated.
As we did in the previous tutorial about if-statements, we will walk through an example together. This time we will see if we can find the day with the best weather in a monthly forecast.

Now, the “best day” is very subjective, so for this example we will focus on the the day with the most sun and highest temperature. If you would do this on your own you would make a table which will log all the temperatures and weather types, so let’s do that here as well.
% Set parameters
temperature = [ 18; 17; 16; 17; 16; 21; 17;
22; 27; 24; 21; 15; 16; 22;
25; 22; 23; 26; 28; 30; 22;
24; 23; 22; 25; 22; 23; 24];
weather = [ "sun"; "rain"; "rain"; "rain"; "clouds"; "clouds"; "rain";
"sun"; "thunder"; "rain"; "sun"; "thunder"; "rain"; "sun";
"p_cloud"; "p_cloud"; "sun"; "sun"; "sun"; "p_cloud"; "thunder";
"sun"; "rain"; "sun"; "part_cloud"; "rain"; "sun"; "sun"];
Addionally, we will also add all the weather information in two vectors. These logging vectors will be used to store data over the for loop.
% Set logging vectors
max_temp = zeros(1,1);
idx_best = zeros(1,1);
1.2. Adding the first if-statement #
Before we will built the for-loop, we will first need to create a if-statement. Everytime the loop switches to the new day it first needs to check if that day has sun, then do something.
if weather[...] == "sun"
...
end
As you can see we added […] to the loop, this is called a iterator. This iterator is most often represented by a i. This will then indicate within the loop which value needs to be selected from the vector (see T1 for indexing).
1.3. Adding the second if-statement #
Secondly we need to think of an output for the for-loop. After we checked the sun, we want to see if this is the highest temperature so far. Let’s make a second if-statement for this.
% First if-statement, is it sunny?
if weather[i] == "sun"
% Second if-statement, is this new one the highest so far?
if max_tmp < temperature[i]
% Log the temperature
max_tmp = temperature[i];
% Log the index
idx_best = i;
end
end
1.4. Adding the actual for-loop #
% Loop parameters
length_of_loop = length(temperature);
The last part will be to put everything into one for-loop. You always need to make sure to check what needs to be inside and what needs to be outside of the loop. So for this example we will need to put the if-statements inside of the loop, and the rest we have written so far will be placed before the for-loop.
A for-loop generally has the following shape:
for i = 1:length
...
end
When putting eveything together, we will get the following script:
% Set parameters
temperature = [ 18; 17; 16; 17; 16; 21; 17;
22; 27; 24; 21; 15; 16; 22;
25; 22; 23; 26; 28; 30; 22;
24; 23; 22; 25; 22; 23; 24];
weather = [ "sun"; "rain"; "rain"; "rain"; "clouds"; "clouds"; "rain";
"sun"; "thunder"; "rain"; "sun"; "thunder"; "rain"; "sun";
"p_cloud"; "p_cloud"; "sun"; "sun"; "sun"; "p_cloud"; "thunder";
"sun"; "rain"; "sun"; "part_cloud"; "rain"; "sun"; "sun"];
% Best day = highest temp + sun
% Set logging vectors
max_temp = zeros(1,1);
idx_best = zeros(1,1);
% Loop parameters
length_of_loop = length(temperature);
for i = 1:length_of_loop
% First if-statement, is it sunny?
if weather[i] == "sun"
% Second if-statement, is this new one the highest so far?
if max_tmp < temperature[i]
% Log the temperature
max_tmp = temperature[i];
% Log the index
idx_best = i;
end
end
end
2. While loops #
In the previous part, we looked extensively at the for-loop. But there are also other loops we could use to extend our programming skills. For instance, we have while-loops. These loops will stop when residuals fall below a certain threshold. This is significantly different compared to the for-loops, as the for-loop as a set end-point. An example of a while-loop would be to fit peaks when the number of fitted peaks is equal to the number of peaks in a peak list. In this example, the number of peaks in the peak list could change, therefore the end is unknown.
while i <= A
code with “i”
end
Concluding remarks #
For more information also look at the MATLAB website. Here you can find even more information about how to work with everything from matrices to different functions, and so much more.