Laboratory Exercise 2 Conditional Statements by Rabosa - J
Laboratory Exercise 2 Conditional Statements by Rabosa - J
➢ Problem 1: Write a program that accepts an integer from the user and checks whether it is
Here are two Java programs that accept an integer from the user and check if it's
positive, negative, or zero. The first uses a simple if-else if-else structure; the
second uses a switch statement (though a switch statement is less ideal for ranges
like this).
Sample Output 2:
➢ Problem 2: Create a program that asks the user to enter their age and determines whether they
Voting Eligibility
This program checks if a user is eligible to vote based on their age.
Sample Output 1:
Sample Output 2:
➢ Problem 3: Write a program that takes an integer input representing a day of the week (1 for
Monday, 2 for Tuesday, etc.) and displays the corresponding day. If the input is invalid, an error
message will be displayed.
This program displays the day of the week based on integer input. It includes error handling for invalid
input.
Sample
Output 1:
Sample Output 2:
Question
Conditional statements are the backbone of decision-making in programming. They allow programs to
execute different blocks of code based on specific conditions, making them dynamic and responsive to
varying inputs and situations.
Conditional statements provide a way to evaluate expressions and determine which code path to follow.
They act as branching points in the program's flow, allowing it to adapt to different scenarios.
- Example: Imagine a program that asks for a user's age. Using a conditional statement, we can check
if the user is over 18 and display a different message based on the result.
Without conditional statements, programs would execute the same instructions regardless of the input,
making them inflexible and less useful.
Both if-else and switch-case statements are used for decision-making, but they have distinct differences:
if-else Statements:
- Evaluation: if-else statements evaluate a single condition at a time. They execute the code block
associated with the first condition that evaluates to true.
- Flexibility: if-else statements are highly flexible and can handle complex conditions using logical
operators (&&, ||, !).
- Range of Values: if-else statements can handle a wide range of data types and can evaluate
conditions based on ranges of values.
switch-case Statements:
- Efficiency: switch-case statements can be more efficient than nested if-else statements, especially
when dealing with a large number of cases.
- Limited Scope: switch-case statements are typically used for evaluating equality comparisons and are
less flexible than if-else for complex conditions.
Example:
The default case in a switch statement acts as a catch-all for scenarios where none of the specified cases
match the evaluated expression. It's essential for several reasons:
- Error Handling: The default case allows you to handle unexpected input values gracefully, preventing
the program from crashing or behaving unexpectedly.
- Clarity: Including a default case explicitly indicates that you've considered all possible scenarios,
making your code more readable and maintainable.
- Program Robustness: By providing a fallback option, the default case enhances the overall robustness
of your program, ensuring it can handle unexpected situations.
Example:
In this example, if the grade variable holds a value other than 'A', 'B', or 'C', the default case will execute,
providing a clear message to the user.
Conclusion
This lab activity demonstrated the use of conditional statements (if-else and switch) for controlling
program flow and making decisions based on various conditions. The exercises highlighted the
importance of these statements in creating flexible and robust programs that can handle different
inputs and situations effectively. Understanding the nuances of if-else and switch, including the role of
the default case, is fundamental to writing well-structured and reliable code.