0
0
C++programming~15 mins

If–else statement in C++ - Mini Project: Build & Apply

Choose your learning style9 modes available
If-else statement
📖 Scenario: You are creating a simple program that checks if a person is old enough to vote.
🎯 Goal: Build a program that uses an if-else statement to decide if a person can vote based on their age.
📋 What You'll Learn
Create an integer variable called age with the value 20
Create an integer variable called voting_age with the value 18
Use an if-else statement to check if age is greater than or equal to voting_age
Print "You can vote." if the condition is true
Print "You cannot vote yet." if the condition is false
💡 Why This Matters
🌍 Real World
Checking age eligibility is common in many real-world applications like voting, driving licenses, or age-restricted content.
💼 Career
Understanding if-else statements is fundamental for any programming job because it helps programs make decisions based on conditions.
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 age = 20; to create the variable.

2
Create the voting_age variable
Create an integer variable called voting_age and set it to 18.
C++
Need a hint?

Use int voting_age = 18; to create the variable.

3
Write the if-else statement
Use an if-else statement to check if age is greater than or equal to voting_age. Inside the if, write std::cout << "You can vote.";. Inside the else, write std::cout << "You cannot vote yet.";.
C++
Need a hint?

Use if (age >= voting_age) { ... } else { ... } with the correct print statements.

4
Print the result
Add #include <iostream> at the top, wrap the code inside int main() function, and add return 0;. Then run the program to print the output.
C++
Need a hint?

Make sure to include #include <iostream> and wrap your code in int main(). Run the program to see the output.