0% found this document useful (0 votes)
12 views8 pages

Laboratory Exercise 2 Conditional Statements by Rabosa - J

The document outlines a laboratory exercise focused on conditional statements in programming, including tasks for checking integer values, voting eligibility, and day of the week identification. It explains the differences between if-else and switch-case statements, emphasizing their roles in decision-making and the importance of including a default case in switch statements. The exercises aim to enhance understanding of program flow control and the flexibility of conditional statements.

Uploaded by

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

Laboratory Exercise 2 Conditional Statements by Rabosa - J

The document outlines a laboratory exercise focused on conditional statements in programming, including tasks for checking integer values, voting eligibility, and day of the week identification. It explains the differences between if-else and switch-case statements, emphasizing their roles in decision-making and the importance of including a default case in switch statements. The exercises aim to enhance understanding of program flow control and the flexibility of conditional statements.

Uploaded by

xhan.2.8m
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

LABORATORY EXERCISE 2 CONDITIONAL STATEMENTS

➢ Problem 1: Write a program that accepts an integer from the user and checks whether it is

positive, negative, or zero.

Positive, Negative, or Zero

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).

Program 1 (if-else if-else):


Sample Output 1:

Sample Output 2:

➢ Problem 2: Create a program that asks the user to enter their age and determines whether they

are eligible to vote. (Voting age is 18 or older)

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.

Day of the Week

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

1. How do conditional statements help in decision-making within programs?

Conditional Statements: The Heart of Decision-Making in Programs

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.

How Conditional Statements Facilitate Decision-Making

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.

2. What is the difference between `if-else` and `switch-case` statements?

if-else vs. switch-case Statements

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:

- Evaluation: switch-case statements compare a single variable or expression against a set of


predefined cases. They execute the code block associated with the matching case.

- 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:

3. Why is it essential to include a `default` case in a `switch` statement?

The essential or importance of the default Case in switch Statements

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.

You might also like