0
0
MATLABdata~30 mins

Switch-case statements in MATLAB - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Switch-Case Statements in MATLAB
📖 Scenario: You are creating a simple program that helps a coffee shop decide the price of a drink based on the size selected by the customer.
🎯 Goal: Build a MATLAB program that uses a switch-case statement to assign prices to different coffee sizes.
📋 What You'll Learn
Create a variable called size with a specific coffee size value.
Create a variable called price to store the price of the coffee.
Use a switch statement on the size variable with cases for 'small', 'medium', and 'large'.
Assign the correct price for each size: 2.5 for small, 3.5 for medium, and 4.5 for large.
Add a otherwise case that assigns 0 to price if the size is not recognized.
Print the price using fprintf with the format: 'The price is $X.XX\n'.
💡 Why This Matters
🌍 Real World
Coffee shops and many businesses use switch-case logic to decide prices or actions based on customer choices.
💼 Career
Understanding switch-case statements is important for writing clear and efficient decision-making code in many programming jobs.
Progress0 / 4 steps
1
Create the coffee size variable
Create a variable called size and set it to the string 'medium'.
MATLAB
Need a hint?

Use single quotes for strings in MATLAB, like 'medium'.

2
Create the price variable
Create a variable called price and set it to 0 as a starting value.
MATLAB
Need a hint?

Initialize price to zero before the switch-case.

3
Use switch-case to assign price
Write a switch statement on the variable size with cases for 'small', 'medium', and 'large'. Assign price = 2.5 for 'small', price = 3.5 for 'medium', and price = 4.5 for 'large'. Add an otherwise case that sets price = 0.
MATLAB
Need a hint?

Remember to end the switch statement with end.

4
Print the price
Use fprintf to print the price in the format: 'The price is $X.XX\n', where X.XX is the value of price.
MATLAB
Need a hint?

Use %.2f in fprintf to format the price with two decimal places.