Read this chapter, which reviews how computers make decisions using if statements. As you read this tutorial, you will understand that sometimes it is important to evaluate the value of an expression and perform a task if the value comes out to be true and another task if it is false. In particular, try the simulated program under the heading "Simulated Program" to see how a different response is presented to the user based on if a number is positive or negative.
Pay special attention to the "More Than One Statement per Branch" header to learn how the 'else' statement is used when there is more than one choice.
5. Number Tester
Answer:
-12 is negative.
Number Tester
An integer may be negative, or not. If it is not negative, then it is positive or zero. Here is that idea expressed as a flow chart:
The diamond box shows a two-way decision. Either the false branch or the true branch is taken depending on whether
num < 0
is true or false.
The "two-way split" of the program is easy to see in a two dimensional chart. It is harder to see this in a program where line follows line one after another. The flow chart shows the overall logic of the program. Most of the details of syntax are left out. It is often helpful to sketch a flowchart when you are designing a program. You can use the flowchart to get the logic correct, then fill in the details when you write the program.
Question 5:
The user runs the program and enters "12". What will the program print?