Switch vs if comparison
📖 Scenario: Imagine you are building a simple program that tells the day of the week based on a number input. You want to compare two ways to do this: using switch and using if statements.
🎯 Goal: You will create a program that takes a number from 1 to 7 and prints the corresponding day name. You will first set up the input number, then create a variable to hold the day name, then use switch statement to assign the correct day name to dayName, and finally print the result.
📋 What You'll Learn
Create an integer variable called
dayNumber with the value 3Create a string variable called
dayName initialized to an empty stringUse a
switch statement on dayNumber to assign the correct day name to dayNamePrint the value of
dayName💡 Why This Matters
🌍 Real World
Programs often need to choose actions based on a value, like days of the week or menu options.
💼 Career
Understanding switch and if statements is essential for writing clear, efficient decision-making code in many software jobs.
Progress0 / 4 steps