Send Close Add comments: (status displays here)
Got it!  This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.nbsp; Note: This appears on each machine/browser from which this site is accessed.
C: If examples and refactoring


1. C: If examples and refactoring

2. Determine positive or nonnegative.
The following code gets an integer value for x and prints out whether x is nonnegative or negative.

Here is the C code.


3. Examples of input and output
Here are some examples of input and output for the above program code.

Here is an example input.

For the above example input, here is the expected output.

Here is an example input.

For the above example input, here is the expected output.

Here is an example input.

For the above example input, here is the expected output.


4. Viewpoints
Note the following.

5. Refactoring
In software development/engineering, the term refactoring refers to rewriting code to be better, in some way, but that does the same thing.

Below is a program that does the same thing as the above program but removes some redundancy in the output statements. Verify to yourself that the code does the same thing and that you understand how the if statement flow works. Here is the C code.


6. Examples of input and output
Here are some examples of input and output for the above program code.

Here is an example input.

For the above example input, here is the expected output.

Here is an example input.

For the above example input, here is the expected output.

Here is an example input.

For the above example input, here is the expected output.


7. More refactoring
Now study the refactored code below and assure yourself that you understand how it works. Here is the C code.


8. Examples of input and output
Here are some examples of input and output for the above program code.

Here is an example input.

For the above example input, here is the expected output.

Here is an example input.

For the above example input, here is the expected output.

Here is an example input.

For the above example input, here is the expected output.


9. Best program
The first program is considered the easiest to understand (for a programmer) but has more redundancy (repetition) of text.

The last program is considered the most to have the lest redundancy (repetition) of text but is not as easy to understand (for the programmer).

In general, the last program is better from a programming point of view.

10. Programmers
In general, companies who hire programmers who are not as capable will prefer the first program.

In general, companies who hire better programmers will prefer the first program.

General rule: Removing redundancy in programs makes the programs better but makes them require more time to understand.

11. End of page

12. Multiple choice questions for this page