You are on page 1of 2

1.

With the completion of this workshop, you must have begun to appreciate why code
formatting is important. Explain how you made your code – especially the iteration and
selection logic parts – easy to read and maintain.

To make iteration and selection logic easy to read and maintain in C I followed these steps:

1. Named variables clearly and concisely to improve code readability and make it easier to
understand the logic behind iteration and selection statements.

2. Used whitespace, because proper whitespace helps to visually distinguish different parts of
the code and make it easier to follow the flow of control.

3. Added comments, because comments help to explain the logic behind iteration and selection
statements. They can also make it easier for others to understand and maintain the code,
especially if the logic is complex.

2. Nested logic is an important feature of programming in any language. Identify and briefly
discuss the biggest impact on how this workshop could be coded if nesting were not possible.

Answer:

If nesting were not possible, it would have a significant impact on the ability to write complex
and efficient code. Nesting allows programmers to control the flow of execution of code based
on certain conditions.

Without nesting, it would be difficult to handle complex conditions and control structures,
making the code harder to write, understand, and maintain. For example, without nesting, it
would be difficult to implement if-else statements, loops within loops, and other complex
control structures.

3. Consider the following scenario: you are programming logic that requires a user to input a
value between 1 and 5, and for each value within that range, you had to perform something
unique. You would have to apply selection to validate the entered values so you can implement
the given logic for a specific entered range value. Explain why is it inefficient to achieve this
using a series of “if” optional path constructs instead of the preferred “if/else if...” alternative
path logic?

Answer:

Using a series of "if" statements to handle multiple conditions, each "if" statement is executed
and tested in sequence, one after the other. This process can be time-consuming and inefficient,
especially if there are many conditions to be tested.
When dealing with multiple conditions and ranges, the "if/else if.../else" allows to test multiple
conditions in an efficient and organized way.

In the scenario described, if use a series of "if" statements to handle the range of values from
1 to 5, it would need to write 5 separate "if" statements, each one testing for a specific value.
This would be time-consuming and would make the code more difficult to maintain and
understand. On the other hand, if you used "if/else if.../else" logic, you would only need to write
5 conditions, and the code would be more efficient, organized, and easier to understand.

You might also like