Basic Math
In Figure 9, three different variables are defined, a, b, and c. The variable d is equal to the sum of the three variables. After the command is executed no results are automatically displayed. But the value of variable d is now 6. In immediate mode the value of the variable can be displayed by typing the variable name and pressing return. A simple print statement could have been used, which is the preferred method. In the next example parentheses are used as well as a multiplication signs. Python follows the mathematical order of operation system so a different result would be obtained with and without the addition of parentheses. A double asterisk is used to raise an item to a power. Note: the variable d has a new value after each operation, while the variables a, b, and c retain their original values.
Decision Statements
All computer languages have some type of a statement which will create a decision statement that uses Boolean Algebra. How the statements are formatted can work differently between computer languages. Generally, they are based on true or false logic. When the statement is true the program branches in one direction and when false a different set of commands are needed. In this section only a basic understanding of these types of statements will be explored with more detail in later modules.
Typically, in a flow chart a diamond shaped object is used when a decision is required. The diamond is used because there are four points for interaction, the variables are generally shown going into the top of the diamond, with paths going out the bottom and the sides. One of the more common types of decision is known as an if statement. Utilizing logic, a statement is written and if true it branches out of the loop, if false another comparison is made, known as an else if (elif), if the else if is true, a branch out occurs and if false either it branches to another else if or to an else statement which is the catch point for all elements that are not true. The concept is if (a) is true do action 1, else if (b) is true do action 2, else if (c) is true do action 3, and finally if all results fail do action 4.
Typically, in a flow chart a diamond shaped object is used when a decision is required. The diamond is used because there are four points for interaction, the variables are generally shown going into the top of the diamond, with paths going out the bottom and the sides. One of the more common types of decision is known as an if statement. Utilizing logic, a statement is written and if true it branches out of the loop, if false another comparison is made, known as an else if (elif), if the else if is true, a branch out occurs and if false either it branches to another else if or to an else statement which is the catch point for all elements that are not true. The concept is if (a) is true do action 1, else if (b) is true do action 2, else if (c) is true do action 3, and finally if all results fail do action 4.
When writing these statements, a colon must be placed at the end of the line of the if statement, else if statement and else statements in Python. The colon will cause the next line to indent and not to execute automatically on a return like other statements in immediate mode.
In Figure 10. The initial variables, a, b, and c are defined. The if statement determines if variable b is greater than variable c. If it is true it would print the value of variable a, but if it is false it executes what is in the else statement which subtracts the value of b from the value of a and calls it variable c and then prints this value. No matter which path would have been used it would then print Done. The coding logic is shown in Figure 11.
- The first step is to define the variables.
- When formatting the if statement, what is indented under the if statement is what is completed if true.
- The format of the else statement, has two items indented under it. Provided the if statement is false, what is indented under the else statement is the result.
- Note: the final print statement is back to the original indention so that no matter which path is taken Done is printed. This example is very simple, generally if statements have more than just two branches.
In Figure 12 an if/elif/else statement will depend on the values of a, b, and c.
The code in Figure 13 provides a look at the script, for the diagram in figure 12.
- Three variables are defined, a, b, and c.
- In the if statement, provided ‘b’ is greater than ‘c’ if it is True then the value ‘a’ is printed, if false the process drops to the elif statement.
- The elif (Else If) statement is another if statement, the question is if ‘b’ is equal to ‘c’ if true ‘b=c’ is printed if false the process would drop to the else statement and print ‘bad input’.
- No mater which pathway is taken the next operation would print ‘Done’.