0
0
C++programming~15 mins

Ternary operator in C++ - Mini Project: Build & Apply

Choose your learning style9 modes available
Using the Ternary Operator in C++
📖 Scenario: You are creating a simple program to decide if a person is an adult or a minor based on their age.
🎯 Goal: Build a C++ program that uses the ternary operator to check if a person is 18 or older and print the correct message.
📋 What You'll Learn
Create an integer variable called age with the value 20
Create a string variable called status that uses the ternary operator to set 'Adult' if age is 18 or more, otherwise 'Minor'
Print the value of status
💡 Why This Matters
🌍 Real World
Ternary operators help write short and clear decisions in programs, useful in many real-world apps like age checks, pricing rules, or UI changes.
💼 Career
Understanding ternary operators is important for writing clean, efficient code in software development jobs.
Progress0 / 4 steps
1
Create the age variable
Create an integer variable called age and set it to 20.
C++
Need a hint?

Use int to declare the variable and assign the value 20.

2
Create the status variable using the ternary operator
Create a string variable called status that uses the ternary operator to set it to "Adult" if age is 18 or more, otherwise "Minor".
C++
Need a hint?

Use the syntax condition ? value_if_true : value_if_false to assign status.

3
Include the <iostream> and <string> headers and add using namespace std;
Add the lines #include <iostream>, #include <string>, and using namespace std; at the top of the program.
C++
Need a hint?

These headers and namespace allow you to use string and cout easily.

4
Print the status variable
Write a main function that prints the value of status using cout.
C++
Need a hint?

Use cout << status << endl; inside main() to print the result.