V. Loop structures
2. Programming language
Excursus: Loop structures
Skilled workers constantly perform repetitive tasks, such as processing transactions, creating calculations and performing data operations.
In programming, we can create programs that perform repetitive tasks automatically. To do this, we can use repetition structures such as “for” or “while”.
Take, for example, an economist who repeatedly analyzes his clients' data and makes calculations to show them which investment strategy will bring the greatest benefit over the years. Instead of doing the calculations for each year individually, he can use the “for” statement to calculate the interest over several years at once.
Run the following example and observe how the program creates the simulation for a simple interest investment over 10 years with an initial principal of 10,000 euros and an interest rate of 5%:
In this example:
- The for loop runs through each year and updates the customer principal according to the formula for simple interest.
- This type of loop-based structure allows economists to easily simulate changes in interest rates over time.
For loops run through a defined range of values and allow you to repeat actions a certain number of times. Iterations refer to the number of iterations of a loop block in which actions are repeated within a predefined and restricted range or until a condition is met.
The repetition structure is particularly important in the context of machine learning. Computers often learn a task through repetition. They repeat a task several times until it is performed well.
EXERCISES
An economist wants a program with which he can carry out investment simulations based on his customers' data in order to support their decisions.
Can you help him by completing the following tasks?
EXERCISE 1
The economist has developed a program for calculating compound interest (annual interest). With this program, he can quickly simulate different scenarios when he receives new customer data and thus help customers make informed financial decisions.
Now follow the code and answer the questions. Achieve 100% of the points in the following quiz. You can try again if you have done something wrong.

EXERCISE 2
Now the economist would also like to include the calculation of simple interest.
Analyze and execute the code provided, submit it and receive a “Congratulations” message.
Instructions: Blockly (OPTION I)

- Replicate the code above in the Blockly interface.
- Click on 'Submit' to check that the result is actually what we wanted.
Instructions: Python (OPTION II)
# Define initial amount, interest rate, and number of years
principal = 10000 # initial amount in euros
interest = 0.05 # 5% interest rate
years = 5 # duration in years
# Calculate simple and compound interest over time
for year in range(1, years + 1):
simple_interest = principal * (1 + interest * year)
formatted_message = "After year " + str(year) + " the investment with simple interest is worth: " + str(simple_interest)
print(formatted_message)
compound_interest = principal * ((1 + interest) ** year)
formatted_message = "After year " + str(year) + " the investment with compound interest (calculated annually) is worth: " + str(compound_interest)
print(formatted_message)
- Copy the code from the section above and paste it into the lower code area (on the right-hand side).
- Click on “Submit” to check that the result is actually what we wanted.
Just for fun: What do you think the code will output? Make a guess and run the program to see if your guess was correct. Why do you think this result was output?
2. Possible solution

# Define initial amount, interest rate, and number of years
principal = 10000 # initial amount in euros
interest = 0.05 # 5% interest rate
years = 5 # duration in years
# Calculate simple and compound interest over time
for year in range(1, years + 1):
simple_interest = principal * (1 + interest * year)
formatted_message = "After year " + str(year) + " the investment with simple interest is worth: " + str(simple_interest)
print(formatted_message)
compound_interest = principal * ((1 + interest) ** year)
formatted_message = "After year " + str(year) + " the investment with compound interest (calculated annually) is worth: " + str(compound_interest)
print(formatted_message)
EXERCISE 3
Examine the previous code and answer the following questions.
Solve the following quiz and achieve a score of 100%. If you get some questions wrong, you are welcome to try again.
EXERCISE 4
The economist has a client who wants to see how her principal grows each year with both simple interest and compound interest (interest paid once a year) in order to better decide which option to choose. The program should provide the necessary information and at the end calculate and print out the difference between the two investment values after the specified period to see how much more or less the compound interest approach earns compared to simple interest.
Modify the program according to the instructions and submit it to receive a congratulatory message.
Instructions
Step 1:
- Search for the variables “principal” and “years” at the beginning of the code.
- Update these values based on the new scenario:
* principal: Set this to the starting amount for investing of 4000 Euro.
* years: Set this to the number of years for the investment, i. e. 4 years.
Step 2:
- Add a line of code after the for loop to calculate the difference between the sum of compound interest (compound_interest_amount) and the sum of simple interest (simple_interest_amount).
- Save the result in a new variable, e.g. “difference_amount”.
Step 3:
- Add a print statement to display the calculated difference.
- Format the output so that it matches the following text, including the number of years: "The difference amount after 4 years is: 62,025 EUR."
Step 4:
- If your program runs without errors and displays the message “The difference after 4 years is: EUR 62,025”, you will receive the message “Congratulations”.
4. Possible solution

# Define initial amount, interest rate, and number of years
principal = 4000 # initial amount in euros
interest = 0.05 # 5% interest rate
years = 4 # duration in years
# Calculate simple and compound interest over time
for year in range(1, years + 1):
# Simple interest calculation for each year
simple_interest = principal * (1 + interest * year)
formatted_message = "After year " + str(year) + " the investment with simple interest is worth: " + str(simple_interest)
print(formatted_message)
# Compound interest calculation for each year
compound_interest = principal * ((1 + interest) ** year)
formatted_message = "After year " + str(year) + " the investment with compound interest (calculated annually) is worth: " + str(compound_interest)
print(formatted_message)
# Calculate difference between compound and simple interest after full duration
difference = compound_interest - simple_interest
formatted_message = "The difference after " + str(years) + " years is: " + str(difference) + " EUR."
print(formatted_message)
EXERCISE 5 (OPTIONAL)
The economist has a customer who wants to compare two investment options. She wants to know how much her investment will grow each year over a period of two years: a simple interest investment with an interest rate of 5% or a compound interest investment with an interest rate of 2.5% and annual interest. Your task is to write a program that calculates the total amount for each option each year over the two years. Then compare the two investments to see which one grows more by the end of the period and print the result.
Execute a program according to the given instructions and send the code to receive the congratulatory message.
Instructions
Step 1:
- Set an initial investment amount: Choose a starting amount for the investment (e.g. EUR 10,000).
- Define the interest rates:
- Simple interest rate: Use a rate of 5% (0.05).
- Compound interest rate: Use a rate of 2.5% (0.025).
- Define the investment period: Define the number of years to observe the growth, e.g. 2 years.
Step 2:
- Use a loop to calculate the simple interest for each year:
- Calculate the total amount for each year using the following formula:
- simple_interest = principal * (1 + simple_interest_rate * year)
- Print out the amount at the end of each year.
Step 3:
- Use another loop to calculate the compound interest for each year:
- Calculate the total amount for each year using the following formula:
- compound_interest = principal * ((1 + compound_interest_rate) ** year)
- Print out the amount at the end of each year.
Step 4:
- After calculating both investments, compare their final values.
- Print a message showing which investment is growing faster by the end of the period.
- If simple_interest > compound_interest, then print: „The investment with simple interest is better.“
- Else print: „The investment with compound interest is better.“
Step 5:
- Send the program and receive the message “Congratulations”.
5. Possible solution

# Define initial amount, interest rate, and number of years
principal = 10000 # initial amount in euros
simple_interest_rate = 0.05 # 5% simple interest rate
compound_interest_rate = 0.025 # 2.5% compound interest rate
years = 2 # duration in years
# Simple interest calculation
for year in range(1, years + 1):
simple_interest = principal * (1 + simple_interest_rate * year)
print(simple_interest)
# Compound interest calculation
for year in range(1, years + 1):
compound_interest = principal * ((1 + compound_interest_rate) ** year)
print(compound_interest)
# Compare final values
if simple_interest > compound_interest:
print("The investment with simple interest is better.")
else:
print("The investment with compound interest is better.")
Message to take away
We have learned that by using loops and iterations, we can repeat the execution of a code as many times as we want. Use for loops for a certain number of iterations and while loops to repeat the code until a certain condition is met.