📖 Scenario: You are building a simple program to check the age of a person and categorize them as a child, teenager, adult, or senior.
🎯 Goal: Create a program that uses relational patterns in a switch expression to print the correct age category based on the person's age.
📋 What You'll Learn
Create an integer variable age with the exact value 25.
Create a string variable category to hold the age group.
Use a switch expression with relational patterns to assign the correct category:
< 13 is "Child"
< 20 is "Teenager"
< 65 is "Adult"
Anything else is "Senior"
Print the category variable.
💡 Why This Matters
🌍 Real World
Age categories are used in many apps like games, websites, and services to customize content or permissions.
💼 Career
Understanding relational patterns helps you write clear and concise code for decision making in professional C# development.
Progress0 / 4 steps
1
Create the age variable
Create an integer variable called age and set it to 25.
C Sharp (C#)
Hint
Use int age = 25; to create the variable.
2
Create the category variable
Create a string variable called category to hold the age group. Initialize it with an empty string "".
C Sharp (C#)
Hint
Use string category = ""; to create the variable.
3
Use switch expression with relational patterns
Use a switch expression on the variable age with relational patterns to assign the correct string to category. Use these patterns: < 13 returns "Child" < 20 returns "Teenager" < 65 returns "Adult" Anything else returns "Senior".
The relational pattern syntax uses is followed by the operator and value without spaces inside the operator, like is <= 5.
Step 2: Check each option
if (n is <= 5) { } uses correct syntax: if (n is <= 5). if (n <= 5) { } is normal comparison, not a relational pattern. if (n is < = 5) { } has a space between < and = which is invalid. if (n is <=5) { } misses a space after is, which is required.
Final Answer:
if (n is <= 5) { } -> Option A
Quick Check:
Relational pattern syntax: is <= value [OK]
Hint: Relational patterns need 'is' then operator with no spaces inside operator [OK]
Common Mistakes:
Using normal comparison without 'is'.
Adding spaces inside the operator like '< ='.
Omitting space after 'is'.
3. What will be the output of this C# code?
int x = 7;
if (x is >= 5 and < 10)
Console.WriteLine("In range");
else
Console.WriteLine("Out of range");
medium
A. No output
B. Out of range
C. In range
D. Compilation error
Solution
Step 1: Evaluate the relational pattern condition
The condition x is >= 5 and < 10 checks if x is between 5 and 9 inclusive. Since x = 7, it satisfies both parts.
Step 2: Determine which branch runs
Because the condition is true, the Console.WriteLine("In range") runs, printing "In range".
Final Answer:
In range -> Option C
Quick Check:
7 is between 5 and 10, so output is "In range" [OK]
Hint: Check both parts of 'and' relational pattern carefully [OK]
Common Mistakes:
Thinking 'and' means 'or'.
Ignoring one part of the condition.
Assuming syntax error due to 'and'.
4. Identify the error in this code snippet using relational patterns:
int score = 85;
switch (score)
{
case is > 90:
Console.WriteLine("Excellent");
break;
case is >= 80 and < 90:
Console.WriteLine("Good");
break;
default:
Console.WriteLine("Average");
break;
}
medium
A. The 'and' keyword is not allowed in switch case patterns
B. Missing braces around case blocks
C. Relational patterns cannot be used in switch cases
D. The pattern 'is > 90' should be 'case > 90:' without 'is'
Solution
Step 1: Identify the syntax error in switch case patterns
Relational patterns in switch statements use syntax like case > 90:, without the is keyword. The code incorrectly uses case is > 90: and case is >= 80 and < 90:.
Step 2: Evaluate the options
The 'and' keyword is not allowed in switch case patterns is incorrect; and is allowed to combine patterns like case >= 80 and < 90:. The pattern 'is > 90' should be 'case > 90:' without 'is' correctly identifies the misuse of is. Relational patterns cannot be used in switch cases is false; relational patterns are supported in switch cases. Missing braces around case blocks is wrong; braces are not required around case contents.
Final Answer:
The pattern 'is > 90' should be 'case > 90:' without 'is' -> Option D
Quick Check:
Switch case relational pattern: case > value, no is [OK]
Hint: No 'is' keyword needed for relational patterns in switch cases [OK]
Common Mistakes:
Adding 'is' before relational operators in switch cases.
Thinking 'and' cannot combine patterns in switch cases.
Confusing switch statements with 'is' patterns.
5. You want to write a method that returns "Child", "Teen", or "Adult" based on age using relational patterns. Which of these switch expressions correctly implements this?