Boolean Example – Check if You Are of Legal Age
Table of Contents
- Boolean Example – Check if You Are of Legal Age
- Introduction
- Objectives
- Beginner-Friendly Source Code & Step-by-Step Instructions
- Step 1: Create a new console project
- Step 2: Replace Program.cs with the following code:
- Step 3: Run the program
- Example Output
- Summary
- Multiple Choice Quiz
- Exercises, Assessment & Lab Exam
- Intro
- Exercises
- Assessment
- Lab Exam
Introduction
In programming, Boolean values (true
or false
) are essential when making decisions. They are often used in conditions to determine whether an action should be taken or not. One practical example is checking if a person is of legal age. This lesson will help you understand how to use the bool
data type and conditional logic in C#. You will also review declaring variables like int
, string
, double
, and bool
, which are the foundation of data handling in programming.
Objectives
Intro to Objectives: This lesson is designed to not only teach you about Boolean values but also to strengthen your skill in declaring variables and applying conditional checks in real scenarios. By the end of this activity, you should be able to understand, learn, practice, and apply what you studied.
- Understand – Explain how Boolean values (
true
/false
) work in C# and their role in decision-making. - Learn – Identify and declare different basic data types:
int
,string
,double
, andbool
. - Practice – Write a C# program that checks if a given age meets the legal age requirement.
- Apply – Modify the program to check multiple conditions (e.g., voting age, driving age, senior citizen).
Beginner-Friendly Source Code & Step-by-Step Instructions
Step 1: Create a new console project
dotnet new console -n legal_age_check
cd legal_age_check
Step 2: Replace Program.cs with the following code:
using System;
namespace LegalAgeCheckApp
{
class Program
{
static void Main(string[] args)
{
// Declare variables
string name = "Juan Dela Cruz";
int age = 20;
double height = 1.75; // in meters
bool isLegalAge = age >= 18;
// Display details
Console.WriteLine("Personal Details:");
Console.WriteLine("Name: " + name);
Console.WriteLine("Age: " + age);
Console.WriteLine("Height: " + height + " meters");
// Check legal age
Console.WriteLine("\nChecking legal age...");
if (isLegalAge)
{
Console.WriteLine(name + " is of legal age.");
}
else
{
Console.WriteLine(name + " is not of legal age.");
}
}
}
}
Step 3: Run the program
dotnet run
Example Output
Personal Details:
Name: Juan Dela Cruz
Age: 20
Height: 1.75 meters
Checking legal age...
Juan Dela Cruz is of legal age.
Summary
In this lesson, you learned how to use the Boolean (bool
) data type to check conditions in C#. By declaring variables such as string
, int
, double
, and bool
, you practiced handling different kinds of data. Using a conditional if-else
statement, you verified whether a person’s age qualifies as legal age. This fundamental concept is widely used in real applications such as account verification, access control, and eligibility systems.
Multiple Choice Quiz
- Which data type stores true/false values in C#?
A. int
B. string
C. bool
D. double - If
age = 15
, what willage >= 18
evaluate to?
A. true
B. false
C. error
D. null - Which operator is used to check if a person is of legal age?
A. ==
B. >=
C. <=
D. != - What is the output if
age = 20
?
A. Juan is not of legal age
B. Juan is of legal age
C. Error in the program
D. No output - Why are Boolean variables important in programming?
A. To store numbers
B. To represent decisions and conditions
C. To store decimal values
D. To display text
Exercises, Assessment & Lab Exam
Intro
To further improve your understanding, try these exercises. They will help you practice variable declarations, condition checks, and Boolean logic.
Exercises
- Modify the program to allow user input for name and age.
- Add another condition: check if the age qualifies as a senior citizen (60 years old and above).
- Add a new variable
double weight
and display it along with the other details. - Print a message saying “You can vote” if the age is 18 or above.
- Modify the program to check if the person is both legal age and tall enough (height >= 1.6 meters).
Assessment
- Quiz: Answer the 5 multiple-choice questions above.
- Performance Task: Modify the code to accept input for three people and check if each is of legal age.
Lab Exam
- Write a program that asks the user for their name, age, and height.
- Use a Boolean condition to check if they are of legal age.
- Display the result in a complete sentence.
You may visit our Facebook page for more information, inquiries, and comments. Please subscribe also to our YouTube Channel to receive free capstone projects resources and computer programming tutorials.
Hire our team to do the project.