0% found this document useful (0 votes)
19 views3 pages

Java If-Else Program Examples

The document provides five simple Java if-else program examples that can be used in a NetBeans project. Each example illustrates a different application of conditional statements, including checking if a number is positive, determining voting eligibility, checking for even or odd numbers, comparing two numbers, and calculating a student's grade. The code snippets are ready for direct use in Java classes.

Uploaded by

Tesu Rathia
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views3 pages

Java If-Else Program Examples

The document provides five simple Java if-else program examples that can be used in a NetBeans project. Each example illustrates a different application of conditional statements, including checking if a number is positive, determining voting eligibility, checking for even or odd numbers, comparing two numbers, and calculating a student's grade. The code snippets are ready for direct use in Java classes.

Uploaded by

Tesu Rathia
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java if-else Programs in NetBeans

Here are five simple examples of Java if-else programs, each demonstrating a different
application of the conditional statement. You can copy and paste this code directly into a Java
class in a NetBeans project.

1. Check if a Number is Positive, Negative, or Zero


This program takes an integer input from the user and uses if-else-if to determine if it's positive,
negative, or zero.
import [Link];​

public class NumberChecker {​
public static void main(String[] args) {​
Scanner scanner = new Scanner([Link]);​
[Link]("Enter a number: ");​
int number = [Link]();​

if (number > 0) {​
[Link]("The number is positive.");​
} else if (number < 0) {​
[Link]("The number is negative.");​
} else {​
[Link]("The number is zero.");​
}​
}​
}​

2. Determine if a Person is Eligible to Vote


This example checks a person's age to see if they are eligible to vote (assuming the voting age
is 18).
import [Link];​

public class VotingEligibility {​
public static void main(String[] args) {​
Scanner scanner = new Scanner([Link]);​
[Link]("Enter your age: ");​
int age = [Link]();​

if (age >= 18) {​
[Link]("You are eligible to vote.");​
} else {​
[Link]("You are not old enough to vote.");​
}​
}​
}​

3. Check for Even or Odd Number


This program uses the modulus operator (%) to check if a number is divisible by 2.
import [Link];​

public class EvenOddChecker {​
public static void main(String[] args) {​
Scanner scanner = new Scanner([Link]);​
[Link]("Enter an integer: ");​
int number = [Link]();​

if (number % 2 == 0) {​
[Link]("The number is even.");​
} else {​
[Link]("The number is odd.");​
}​
}​
}​

4. Compare Two Numbers


This simple program compares two numbers to see which is greater or if they are equal.
import [Link];​

public class NumberComparison {​
public static void main(String[] args) {​
Scanner scanner = new Scanner([Link]);​
[Link]("Enter the first number: ");​
int num1 = [Link]();​
[Link]("Enter the second number: ");​
int num2 = [Link]();​

if (num1 > num2) {​
[Link](num1 + " is greater than " + num2);​
} else if (num2 > num1) {​
[Link](num2 + " is greater than " + num1);​
} else {​
[Link]("The two numbers are equal.");​
}​
}​
}​
5. Simple Grade Calculator
This program calculates a student's grade based on a given score using multiple if-else-if
conditions.
import [Link];​

public class GradeCalculator {​
public static void main(String[] args) {​
Scanner scanner = new Scanner([Link]);​
[Link]("Enter the student's score: ");​
int score = [Link]();​

char grade;​
if (score >= 90) {​
grade = 'A';​
} else if (score >= 80) {​
grade = 'B';​
} else if (score >= 70) {​
grade = 'C';​
} else if (score >= 60) {​
grade = 'D';​
} else {​
grade = 'F';​
}​

[Link]("The student's grade is: " + grade);​
}​
}​

Common questions

Powered by AI

The Java program uses an if-else-if conditional statement to check the value of the integer input. It first checks if the number is greater than zero, in which case it is positive. If the condition is not met, it checks if the number is less than zero to determine if it is negative. If neither condition is met, the number is zero .

For instance, the GradeCalculator program could use logical operators to allow for composite conditions, such as combining multiple grading criteria into a single if condition, improving readability and reducing code duplication. Similarly, logical operators could allow the VotingEligibility program to consider additional conditions for voting eligibility, such as citizenship or criminal record status .

These Java programs exemplify imperative and procedural programming paradigms, focusing on sequences of statements to change a program's state. Through the explicit use of control structures like if-else conditions, they demonstrate decision-making processes crucial for algorithmic logic. These examples provide a foundational understanding for handling data validation and user interaction systematically .

The GradeCalculator program assigns grades by using a series of if-else-if conditions to evaluate the student's score. The score is checked in descending order of grade thresholds: 90 and above for 'A', 80 to 89 for 'B', 70 to 79 for 'C', 60 to 69 for 'D', and less than 60 for 'F'. The program outputs the corresponding grade once a condition is met .

The EvenOddChecker program determines if a number is even or odd by using the modulus operator (%). It checks if the remainder when the number is divided by 2 is zero. If it is, the number is even; otherwise, it is odd .

Using Scanner inputs can introduce inefficiencies due to the blocking call it represents—waiting for user input can delay program execution. It can also lead to errors if the user provides invalid input, such as a non-integer when an integer is expected, or if there is unexpected EOF, which could cause the program to throw an exception if not properly handled .

The main limitation of the NumberChecker program is that it relies solely on integer input, meaning it cannot handle decimal numbers. Additionally, it does not account for numeric overflows or errors from invalid inputs, which could potentially halt the program or produce incorrect results .

The programs could be modified to include try-catch blocks around the Scanner input operations to catch and handle InputMismatchException, ensuring the program does not crash from invalid input. Additional checks could be implemented to handle edge cases like empty inputs or irrelevant symbols, with feedback for the user to provide valid data .

The VotingEligibility program uses an if-else statement to determine voter eligibility by comparing the person's age input to the threshold of 18 years old. If the age is equal to or greater than 18, it deems the person eligible to vote; otherwise, it indicates that the person is not old enough to vote .

The NumberComparison program uses if-else-if statements to compare two integer inputs. It first checks if the first number (num1) is greater than the second number (num2), printing the result if true. If not, it checks if the second number is greater than the first, and prints that result. If neither condition is true, it concludes the numbers are equal .

You might also like