If, Elif and Else |
When a decision is required typically, a diamond shaped symbol is used and the input is shown coming into the top of the diamond and outputs are shown coming out of the sides and bottom. Care should be taken to make sure that lines in the diagram have arrows to show flow direction of the process. Typically, the symbol pathways are labeled as true and false.
The statement uses logic to determine how the branch will proceed. If the statement is true then it will branch in one direction and if false, a different pathway will be followed. Contained within the diamond symbol is a function, which can be extremely simple or very complex. Logical operations such as equal, greater than, greater than or equal, less than and less than or equal are typical logic that might be used, which includes Boolean algebra. For example, simply comparing one variable to another can be the operation. The variables can be numeric or strings, but not a combination of the two. |
The process will be to determine a logical decision in the blue diamond shaped symbol. If true, the pathway would lead to the green shaded box. Multiple functions can be located inside this box, when the operation is completed, the flow would be to the blue box with the curved corners, which would be outside of the loop. If the logic in the blue diamond shaped symbol is false then the flow would be to the tan colored box to the left, which would contain an Else statement. Multiple processes could be completed within this element just as was done with the true pathway. Once these operational parameters are completed, the flow would be to the blue box with the curved corners. Thus when the if statement is completed the program continues to the same logical location no matter which process pathway was followed.
|
If/Else Code
These concepts are applicable to any scripting or programming language, but the specific Python script completes the application. The code should be accomplished in programming mode and not in immediate mode. The code contains three unused statements, which include the import of arcpy, the import of the environment and the workspace pathways. These statements were part of the template used in previous lessons. The If statement must have a specific format, which has a colon at the end of it, the dash at the beginning of the statement is not typed and was added by the IDE. In the IDE the indention of the line is added, if an IDE is not used it would manual indentation. The indentions are critical and must always have the same number of spaces.
- The If statement would be in the blue diamond in Figure 2 and the “beginning freshman” string would be in the green box in Figure 2. The studentType of “beginning freshman” would only occur under the condition that the If statement was true, which means the student had less than 12 credit hours.
- The value of the variable the hours can be loaded in immediate mode prior to writing the script or could be a line added above the If statement defining the variable.
- If the statement is false, i.e. the value is 12 or greater than the pathway would lead to the Else statement. The pathway would have followed the red arrow to the tan box in Figure 2. The tan box would contain the Else statement and anything below the Else statement that is indented. The value of the studentType variable would be “student has at least 12 credit hours”.
If/Elif/Else Code
In the first example, there was only one decision and it was either true or false. In Python, scripting multiple decision statements can be grouped together by using the Elif statement, which means Else If. So when the If statement is false the process can move to an Elif statement. When the Else If statement when is true it provides a different solution and when false either another Elif statement could be used or an Else statement is used to end the routine. Independent of the pathway, the script will then return to a common point, outside of the indentions in the script.
For example, if a college wants to determine the status level (in plain text and not as a number) of each student that has been geocoded automatically, the if, elif, else might be used. Therefore, using the number of credit hours earned, the student could be classified. The criteria might be like the following:
For example, if a college wants to determine the status level (in plain text and not as a number) of each student that has been geocoded automatically, the if, elif, else might be used. Therefore, using the number of credit hours earned, the student could be classified. The criteria might be like the following:
- Less than 12 credit hours they are listed as beginning freshmen.
- Between 12 and 29 hours, they are listed as freshmen.
- Between 30 and 44 credit hours, they are listed as a second year students.
- In addition, if 45 hours or more they are listed as nearing graduation.
- At the end of the process the status level of each student should be printed, it could also be placed into the data as a new field.
- The value is compared to see if it is less than 12 and when true the process will drop straight down to the green box below the first blue diamond and the studentType will be set to ‘beginning freshmen’.
- If false than the value must be 12 or more, so the next comparison is with 30, which will give a true for all values between 12 and 29 and would drop to box below the second blue diamond and the studentType will be set to ‘freshmen’.
- If false, we know the value of credit hours must be at least 30 and the next decision will determine if it is less than 45. If true it uses the commands in the third green box from the right and the studentType will be set to ‘second year’.
- If false it uses the tan colored box, since the student must have at least 45 credit hours and the studentType will be set to ‘nearing graduation’.
- In all cases the student type is printed.
The code view for the flow chart of Figure 4.
- As previously noted the first three commands are not used in this process.
- A value for hours would need to be defined outside of this script.
- The if statement is comparing hours to the number 12.
- If the hours are less than 12 then it will complete items that are indented and move then to the print statement,
- If false, it will move to the first elif statement.
- The first elif statement is comparing to see if the value is less than 30.
- If the hours are less than 30 then it completes the items indented and moves to the print statement,
- If false the hours must be more than 29 and moves to the second elif statement.
- The second elif state is comparing to see if the value is less than 45.
- If the hours are less than 45 then it completes the items indented and moves to the print statement.
- If false the hours are more than 44 and moves to the else statement.
- The else statement provides a value for the studentType and moves to the print statement.
- The key to all of the logic statements is the proper indention of the lines.