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