Sometimes it can be difficult to see clearly where to use an "else" or what instructions after an "if" should be enclosed in braces. It is easier if we think about the problem instead of trying to directly type a C# program.
Graphical notations, such as flowcharts, can help us focus on the problem, as they allow us to see better what to do and when.
First, let's see the 4 basic elements of a flowchart, and then we will apply them to a specific case.
The beginning and the end of the program are indicated with a circle. Internal processes, such as arithmetic operations, in a rectangle. The input and output operations (writing on screen and reading from keyboard) are indicated by a parallelogram. Decisions are indicated within a diamond.
Let's see the example of a program that asks the user a number and answers if it is positive, negative or zero:
The steps from here to the corresponding C# program (which we saw in the example 11) should be almost immediate: we know how to read from keyboard, how to write on screen, that decisions will become an "if" statement, and so on.
Suggested exercises:
- Create the flowchart for the program which asks the user for two numbers and answers if one of them is positive, if both are, or if none of them is.
- Create the flowchart for a program that asks the user for three numbers and says which one is the greatest.
No comments:
Post a Comment