Mark As Completed Discussion

If/Else Statements

If/else statements can be best visualized and understood in the form of flow chart diagrams. For example, suppose we want to determine if a number is odd or even, and display a helpful message accordingly. In a sequential program, we cannot do this as it executes the program in a single flow, executing all statements. However, here we need different path flows for different outcomes, as shown below.

If/Else Statements

Thus, we need to be able to control the flow of the program by specifying that if the number is even print "x is even" else the number is odd print "x is odd". Depending on the number, the decision and output will be shown automatically through the execution.

Below is another example, where multiple execution paths are defined. This allows for specification of more than 2 different conditions.

If/Else Statements

Let's see how these concepts take effect in Python and JavaScript.