Use of if, elif, else, while, break and continue

Posted by BTalon on Mon, 07 Mar 2022 12:22:59 +0100

Today's content

  • Process control theory
  • if judgment
  • while Loop

Process control concept

Process control is to control the execution process of things

Classification of execution process

Sequential structure

Execute from top to bottom. The flow chart of code operation is as follows

Branching structure

Make different operation processes according to certain conditions

Cyclic structure

According to some judgments, doing the same thing repeatedly (there should be a condition to end the cycle if it won't be cycling all the time) is similar to a person doing one thing every day, such as the operator on the assembly line. At the code level, it is actually making some code execute again and again

The flow chart is as follows

Branch structure classification

  1. Single if branch
    '''
    Grammatical structure
        if Conditions:
            Subcode after the condition is established (multiple lines are allowed)
    '''
    # eg
    # If a girl is over 38, she is called aunt
    # First define the age of a girl
    age = 39
    # Judge whether the age is greater than 38
    if age >38:
        print ('Hello, aunt')
    

    So the flow chart of this code is

  2. if and else branches
    '''
    Grammatical structure
        if Conditions:
            Subcode executed after the condition is established (multiple lines are allowed)
        else: 
        Sub code executed when the condition is not satisfied (multiple lines are allowed)
    '''
    # eg
    # If a girl is older than 38, she is called aunt, otherwise she is called little sister
    age = 18
    if age > 38"
    	print('Hello, aunt')
    else: 
    	print('cute girl')
    

    Then the flow chart of this code is:

  3. if and elif and else branches
'''
Grammatical structure
	if Condition 1:
		Sub code executed when condition 1 is true (multiple lines are allowed)
	elif Condition 2:
		Condition 1 does not hold, condition 2 holds the sub code executed (multiple lines are allowed)
	elif Condition 3:
		Condition 1 and 2 are not valid, and condition 3 is valid. The sub code executed (there can be multiple lines)
	else: 
		All the above conditions are not true. The execution is in the sub code (there can be multiple lines)
***middle elif You can have more***
'''
# eg
# If the user name is tuzi, the boss will be printed; if it is tom, the cat will be printed; if it is jeery, the mouse will be printed; if it is others, I will not know you
username = input('username>>>:')
if username == 'jason'
	print('boss')
elif username == 'tom'
	print('tom')
elif username == 'jerry'
	print('mouse')
else: 
	print('Don't know you')

Then the flow chart of this code is:

Nesting of if
# If the girl is older than 38, say she's sorry to recognize the wrong person, or go up to wechat
# If you succeed, go to eat, watch movies and go shopping until it's dark and the dormitory is locked!
    age = 22
    is_success = True
    if age < 38:
        print('I've been watching you for a long time and want a contact information')
        if is_success:
            print('Eat and watch movies. It's dark and sleep')
        else:
            print('Get off')
    else:
        print('Oh, sorry to recognize the wrong person')

Then the execution flow chart of this code is:

Cyclic structure

While loop

while statement is used to execute a program circularly, that is, under certain conditions, execute a certain program circularly to deal with the same tasks that need to be processed repeatedly.

'''
Its syntax structure is:
while Judgment conditions:
    The loop body code executed after the condition is established
'''
# The execution statement can be a single statement or statement block, the judgment condition can be any expression, and any non-0 non empty value is true. When the judgment condition is false, the loop ends
# eg:
while True:
    #Get user name and password
    username = input('username>>>:')
    password = input('password>>>:')
    # Verify / judge whether the user name and password are correct
    if username == 'tuzi' and password == '123':
        print('Login successful')
    else:
        print('Wrong user name or password')

The operation result is:

If the user name and password have been input incorrectly, the user will be asked to re-enter until the input is correct. Before that, it is really interesting today, and this sentence can not be output, because the program has not jumped out of the while loop. As shown in the figure below

Then the flow chart of the program is:

So now I found a problem, that is, whether it is correct or not, it will run the code in a loop, that is, it falls into an endless loop. Next, let's talk about how to solve it

#while+break
	break It is used to directly end the cycle of this layer
    break You can only end the cycle on that layer
# eg:
# Get user name and password
username = input('username>>>:')
password = input('password>>>:')
# 2. Verify / judge whether the user name and password are correct
if username == 'tuzi' and password == '123':
    print('Login successful')
    break  # End the cycle of this layer
else:
     print('Wrong user name or password')
print('Today is so interesting!!!')

The operation result diagram is:

If the input is wrong, let you re-enter it

Then the running process of the program is

Except that break is used to jump out of the loop, continue is also used to jump out of the loop

continue is used to end this cycle and directly start the next cycle

# eg
count = 1
while count < 11:
    if count == 4:
        count += 1
        continue  # End this cycle and start the next cycle
    print(count)
    count += 1

The running result of the program is

The execution process of the program is: