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.
Conditional processing


1. Conditional processing

2. Flow charts
An algorithm is a precise step by step method for solving a problem.

A design of a solution can be concisely and precisely expressed as an algorithm (using the data structures and data flow diagrams from the problem definition step).

Flow chart

3. Flaw charts
A flow chart, sometimes called a flaw chart, is a visual depiction of an algorithm.

A flow chart has the following properties. Flow charts went out of style in the 1970's, but people continue to use them. Avoid flow charts.

4. Control flow charts
There are many charts that are useful.

A data flow chart can be very useful.

But a control flow chart for an algorithm intended to be coded in a programming language is not very useful.

5. Pseudo-code
Instead of a flow chart, pseudo-code is used.

The pseudo-code idea developed in the 1970's as part of structured programming in order to express algorithms in a precise textual way such that reasoning about the correctness of algorithms and code could be facilitated.

6. Flow charts and pseudo-code

7. C: If statements

8. If statement
Here is a pseudo-code form for the if.
If condition Then    // then part Else    // else part    EndIf

An if statement has the following form in C.


9. If statement
The "else part" can be omitted if it is not needed, but the program must still work if nothing is done. Here is a pseudo-code form.
If condition Then    // then part Else    // else part    EndIf

An if statement has the following form in C.


10. Short form
In some languages such as C (Java, etc.), there is a short form to the if statement that does not use curly braces - if there is only one statement.
if (Condition) Statement;

Do not use this form unless you have a very good reason - and you are an expert programmer with a good reason. Another short form is the following.
if (Codition)    Statement;

Do not use this form unless you have a very good reason - and you are an expert programmer with a good reason.

11. General rule
The general rule for the above rules is the following. In a beginning course, there is (almost never) a good reason for not learning and adhering to the general rules.

12. Nothing is something
Nothing is something. Make sure the program works for the nothing part too.

13. If statement style

14. Style choices for if then else
Here are some possible style choices for curly braces and indents. Pick one and be consistent.

15. My style

   ...    if (Condition) {       ...       }    else {       ...       }    ...

My style, which I started using many years ago, is, essentially, the same style used by Python, Lua, Modula-2, and some others. That is, the open curly brace can be visually omitted, the ending one can be visually omitted (since indentation provides that cue), and fewer expensive screen lines are used (than other styles).

16. Another style

   ...    if (Condition) {       ...    }    else {       ...    }    ...

This style distracts the eye with the ending curly brace. The popular language Python shows that this ending brace is not needed and, in languages that do need it, it can be "hidden" from the eye with the previous style.

17. Another style

   ...    if (Condition)    {       ...    }    else    {       ...    }    ...

This style requires two more lines than the previous styles.

18. End of page

19. Multiple choice questions for this page