Java ProgramsControl FlowCheck Voting Eligibility

Check Voting Eligibility in Java

beginner·  Control Flow  ·  Conditional Statements

Problem

Voting eligibility is a simple age threshold check — a person qualifies once their age meets or exceeds the legal minimum.

Given a person's age, determine whether they are eligible to vote.

Input
age = 20
Output
Eligible to vote: true

Java Program

Java
public class VotingEligibility { public static void main(String[] args) { int age = 20; boolean canVote = age >= 18; System.out.println("Eligible to vote: " + canVote); } }

Output

Eligible to vote: true

Core Logic

A single relational comparison against the minimum voting age settles the question directly.

How It Works
  1. 1age >= 18 evaluates to a boolean, comparing the given age against the minimum voting age of 18.
  2. 2The result is stored directly in canVote — no if/else branching is needed for a plain true/false outcome.
  3. 3Printing the boolean concatenates it into the output string automatically.
For age = 20, 20 >= 18 evaluates to true, so the person is eligible.
💡

Key Point: A boolean expression can be assigned directly to a variable — there's no need to wrap a comparison in an if/else just to produce true or false.

Key Concepts

if statementboolean expressionrelational operator

Related Programs