0
0
Javaprogramming~15 mins

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

Choose your learning style9 modes available
If-else statement
📖 Scenario: You are creating a simple program to check if a person is old enough to vote. In many countries, the voting age is 18 years.
🎯 Goal: Build a Java 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 votingAge with the value 18.
Use an if-else statement to check if age is greater than or equal to votingAge.
Print "You can vote." if the person is old enough.
Print "You cannot vote yet." if the person is too young.
💡 Why This Matters
🌍 Real World
If-else statements help computers make choices, like checking if someone can vote or not.
💼 Career
Understanding if-else is essential for programming jobs because decision-making is everywhere in software.
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
Create the votingAge variable
Create an integer variable called votingAge and set it to 18 below the age variable.
Java
Need a hint?

Use int votingAge = 18; after age.

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

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

4
Print the result
Run the program and ensure it prints You can vote..
Java
Need a hint?

Run the program to see the output in the console.