0
0
Javaprogramming~15 mins

Nested if statements in Java - Mini Project: Build & Apply

Choose your learning style9 modes available
Nested if statements
📖 Scenario: You are creating a simple program to check a person's age and citizenship to decide if they are eligible to vote.
🎯 Goal: Build a Java program that uses nested if statements to check if a person is at least 18 years old and is a citizen. If both conditions are true, print "Eligible to vote"; otherwise, print "Not eligible to vote".
📋 What You'll Learn
Create an integer variable age with the value 20
Create a boolean variable isCitizen with the value true
Use nested if statements to check age and isCitizen
Print the exact text "Eligible to vote" if conditions are met
Print the exact text "Not eligible to vote" if conditions are not met
💡 Why This Matters
🌍 Real World
Checking eligibility for voting is a common real-world decision based on multiple conditions like age and citizenship.
💼 Career
Understanding nested if statements is essential for writing clear decision-making code in many programming jobs.
Progress0 / 4 steps
1
Create variables for age and citizenship
Create an integer variable called age and set it to 20. Create a boolean variable called isCitizen and set it to true.
Java
Need a hint?

Use int for age and boolean for isCitizen.

2
Add nested if statements to check age and citizenship
Inside the main method, add a nested if statement that first checks if age is greater than or equal to 18. Inside this if, add another if that checks if isCitizen is true.
Java
Need a hint?

Use nested if statements: one inside the other.

3
Add print statements inside nested if and else blocks
Inside the nested if where isCitizen is true, add System.out.println("Eligible to vote");. Add an else block for the inner if that prints "Not eligible to vote". Also add an else block for the outer if that prints "Not eligible to vote".
Java
Need a hint?

Use System.out.println inside each block to print the correct message.

4
Print the final eligibility result
Run the program and print the output. The program should print Eligible to vote because age is 20 and isCitizen is true.
Java
Need a hint?

Check the console output after running the program.