Java If-Else Program Examples
Java If-Else Program Examples
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 .