Sometimes there are more than two possibilities and we need more than two branches. For example, when we compare two numbers x and y, we have three possibilities:
Notice that we didnโt have to test x == y; if x isnโt greater than y, and it isnโt less than y, then they must be equal. A simple else takes care of that case.
There is no limit on the number of elif statements in a chain. If there is an else clause, it has to be at the end, but there doesnโt have to be one. Try running the following program to see what it does.
Each condition is checked in order. If the first is false, the next is checked, and so on. If one of them is true, the corresponding branch executes, and the statement ends. Even if more than one condition is true, only the first true branch executes.
Comment out the last two lines by putting a # at the beginning of each line. Now what do you think the output will be? Run the program again. Spoiler alert: Because there is now no else, the program will not print anything.