📖 Scenario: You are creating a simple program that decides if a person is an adult or a minor based on their age. This is like checking if someone can watch a movie with age restrictions.
🎯 Goal: Build a program that uses the ternary conditional operator to decide if a person is an adult or a minor and then prints the result.
📋 What You'll Learn
Create an integer variable called age with a specific value
Create a string variable called status that uses the ternary conditional operator
Use the ternary operator to set status to "Adult" if age is 18 or more, otherwise "Minor"
Print the status variable
💡 Why This Matters
🌍 Real World
Ternary operators are useful for quick decisions in programs, like checking user age for access or setting simple flags.
💼 Career
Understanding ternary operators helps write concise and readable code, a skill valued in software development jobs.
Progress0 / 4 steps
1
Create the age variable
Create an integer variable called age and set it to 20.
C Sharp (C#)
Hint
Use int age = 20; to create the variable.
2
Prepare the status variable
Create a string variable called status but do not assign a value yet.
C Sharp (C#)
Hint
Declare string status; without assigning a value yet.
3
Use the ternary conditional operator
Assign status using the ternary conditional operator: if age is 18 or more, set status to "Adult", otherwise set it to "Minor".
C Sharp (C#)
Hint
Use status = age >= 18 ? "Adult" : "Minor"; to assign the value.
4
Print the status
Write a line to print the status variable using Console.WriteLine.
C Sharp (C#)
Hint
Use Console.WriteLine(status); to show the result.
Practice
(1/5)
1. What does the ternary conditional operator condition ? value_if_true : value_if_false do in C#?
easy
A. It executes both value_if_true and value_if_false regardless of the condition.
B. It is used to declare variables in C#.
C. It only checks the condition but does not return any value.
D. It returns value_if_true if the condition is true, otherwise value_if_false.
Solution
Step 1: Understand the ternary operator syntax
The ternary operator uses a condition followed by a question mark, then two values separated by a colon.
Step 2: Determine what it returns based on the condition
If the condition is true, it returns the first value; if false, it returns the second value.
Final Answer:
It returns value_if_true if the condition is true, otherwise value_if_false. -> Option D
Hint: Always include ':' between true and false parts [OK]
Common Mistakes:
Forgetting the colon ':'
Adding extra quotes or commas
Misplacing parentheses
5. Given a list of integers, which C# code snippet correctly uses the ternary operator to create a new list of strings labeling each number as "Positive" or "Non-positive"?
hard
A. var labels = numbers.Select(n => n > 0 ? "Positive" : "Non-positive").ToList();
B. var labels = numbers.Select(n => n > 0 ? "Positive" "Non-positive").ToList();
C. var labels = numbers.Select(n => if (n > 0) "Positive" else "Non-positive").ToList();
D. var labels = numbers.Select(n => n > 0 ? "Positive" : ).ToList();
Solution
Step 1: Understand the use of ternary operator inside LINQ Select
The ternary operator must have a condition, true value, and false value separated by '?' and ':'.
Step 2: Check each option for correct syntax and logic
var labels = numbers.Select(n => n > 0 ? "Positive" : "Non-positive").ToList(); correctly uses the ternary operator inside Select. Options B, C, and D have syntax errors or invalid expressions.
Final Answer:
var labels = numbers.Select(n => n > 0 ? "Positive" : "Non-positive").ToList(); -> Option A
Quick Check:
Correct ternary inside Select uses ? and : with both values [OK]
Hint: Use ? and : correctly inside lambda expressions [OK]
Common Mistakes:
Omitting colon ':' in ternary operator
Using if-else inside lambda without braces
Leaving false value empty
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.