0
0
Javaprogramming~15 mins

If statement in Java - Mini Project: Build & Apply

Choose your learning style9 modes available
If Statement Basics in Java
📖 Scenario: You are creating a simple program that checks if a person is old enough to vote. Voting age is 18 years or older.
🎯 Goal: Build a Java program that uses an if statement to check a person's age and print whether they can vote or not.
📋 What You'll Learn
Create an integer variable age with the exact value 20
Create an integer variable votingAge and set it to 18
Use an if statement to check if age is greater than or equal to votingAge
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 apps, like voting, driving, or buying age-restricted items.
💼 Career
Understanding if statements is essential for decision-making in programming jobs.
Progress0 / 4 steps
1
Create the age variable
Create an integer variable called age and set it to 20.
Java
Need a hint?

Use int age = 20; inside the main method.

2
Add the votingAge variable
Add an integer variable called votingAge and set it to 18 inside the main method, below age.
Java
Need a hint?

Declare int votingAge = 18; after age.

3
Write the if statement
Write an if statement that checks if age is greater than or equal to votingAge. Inside the if block, write System.out.println("You can vote!");. Add an else block that prints "You cannot vote yet.".
Java
Need a hint?

Use if (age >= votingAge) { ... } else { ... } structure.

4
Print the result
Run the program and observe the output. It should print You can vote!.
Java
Need a hint?

Make sure to run the program and check the console output.