Branch Structure

You can divide the entire program into two or more parts based on the situation. Under the same condition, only one part can be executed.

In layman’s terms, it means that given a condition, if it is met, execute the instructions below; if it is not met, skip the above code and directly execute the code below. Similarly, if the condition is met, it will not execute the code that is not met.

Branch Structure Syntax

The simplest code structure is as follows:

1
2
3
4
if <condition>:
<statements>
else:
<statements>

Complete if structure:

1
2
3
4
5
6
if <logical condition>:
<statements>
elif <logical condition>: # can have multiple elif
<statements>
else: # only one
<statements>

Python’s branch structure combines the functional features of single and multiple branch structures, making the structure relatively complex. However, the application of the multiple branch elif structure is relatively singular, so the core structure of the if branch structure should be the one mentioned above. Note that once if is used, it must be followed by else or elif, otherwise it cannot run.

Indentation Symbol “:”

In Python, whenever there is a statement that is under the premise of another statement, a symbol “:” will appear before these statements, that is, in the context of the structure that has a dependency. Python editors will also automatically indent these statements that are under the premise to the premise statement.

This syntax feature is a major innovation of Python. It makes the structuring of programs part of the syntax itself and is highly sought after by many programmers. I should be one of the conflicting supporters of this syntax feature. Because this feature is extremely beneficial to beginners, but for programmers who are proficient in language control, it becomes a hindrance, and the beauty of the program may decrease.

In other words

1
2
3
4
if (a<b):
print(a)
else:
print(b)

if conditions must end with : and else must also have :.

At the same time, when executing statements, indentation must be added, usually with four spaces. As long as you add a colon, the IDE will automatically add indentation for you.

Application Examples

img

img

Exercises

Maximum Value Problem

Problem Analysis

1. Input two positive integers and output the larger one.

Input:

20

10

Output:

20

This is a typical comparison problem, as long as you can distinguish who is larger.

Input Values

This problem requires comparing two values, so we need to input two values using the input statement.

1
2
a = int(input("Please enter the first number:"))
b = int(input("Please enter the second number:"))

Value Comparison

We assume that a is larger than b, then use the if statement, set if a>b, but if a is smaller than b, then use else, that is, a<b.

1
2
3
4
5
if (a>b):
<statements>
else:
<statements>
#This code cannot run

Result Output

If a is greater than b, that is, the if condition is true, output the value of a.

1
2
3
if (a>b):
print(a)
#This code cannot run

Then, if a is smaller than b, that is, the if condition is not true, use the statement after else, that is, output the value of b.

1
2
3
else:
print(b)
#This code cannot run

Combined:

1
2
3
4
if (a>b):
print(a)
else:
print(b)

Code Example

1
2
3
4
5
6
a = int(input("Enter the first number:"))   #Input values
b = int(input("Enter the second number:")) #Input values
if (a>b): #If a is greater than b
print(a) #Output value a
else: #Otherwise
print(b) #Output value b

Absolute Value Problem

Problem Analysis

2. Input an integer and output its absolute value:

Input:

10

Output:

10

Input:

-10

Output:

10

This problem first needs to determine whether the number is greater than zero. If it is greater than 0, the absolute value is the number itself, and no calculation is needed, just output it directly. If it is less than zero, the absolute value is its opposite number, generally multiplied by -1.

Input Values

This problem requires inputting an integer.

1
a = int(input("Please enter an integer:"))

Positive and Negative Conversion

We need to set up, if a>0, that is a>0, just output a directly, otherwise, that is a<0, multiply by -1.

1
2
3
4
if (a>0):
print(a)
else:
print(a * -1)

Code Example

1
2
3
4
5
a = int(input("Please enter an integer:"))
if (a>0):
print(a)
else:
print(a * -1)