0
0
MATLABdata~30 mins

While loops in MATLAB - Mini Project: Build & Apply

Choose your learning style9 modes available
Using While Loops in MATLAB
📖 Scenario: You are helping a cashier count the total number of coins collected in a jar. The jar contains coins of different values, but you only want to count coins until the total value reaches or exceeds a certain amount.
🎯 Goal: Build a MATLAB program that uses a while loop to add coin values one by one until the total reaches a target amount.
📋 What You'll Learn
Create a vector called coins with the exact values: [1, 5, 10, 25, 1, 10]
Create a variable called target and set it to 30
Use a while loop to add coin values from coins until the total is at least target
Print the final total value after the loop ends
💡 Why This Matters
🌍 Real World
Counting coins or items until a certain limit is reached is common in cash handling, inventory, and budgeting tasks.
💼 Career
Understanding while loops helps automate repetitive tasks and control program flow in many engineering and data processing jobs.
Progress0 / 4 steps
1
Create the coin values vector
Create a vector called coins with these exact values: [1, 5, 10, 25, 1, 10]
MATLAB
Need a hint?

Use square brackets [] to create a vector with the given numbers separated by commas.

2
Set the target total value
Create a variable called target and set it to 30
MATLAB
Need a hint?

Use the assignment operator = to set target to 30.

3
Use a while loop to sum coin values
Create variables total set to 0 and index set to 1. Then use a while loop with the condition total < target && index <= length(coins) to add coins(index) to total and increase index by 1 inside the loop.
MATLAB
Need a hint?

Remember to initialize total and index before the loop. Use while with the condition that total is less than target and index is within the vector length.

4
Display the final total value
Use disp to print the value of total after the loop ends.
MATLAB
Need a hint?

Use disp(total) to show the final total value on the screen.