Flow control statement

  • Conditional (branch) statements
  • Looping statements
  • Break and continue

1.1 Branch Statements

  • If branch statement

    • The branch statement executes different branch code depending on the condition.
    • process
    • A branch
    • Multiple branch

The code example

a = 10
b = 20
c = 30
if a > b:
    if a > c:
        print("A is the largest of the three, and the value is %d." % a)
    else:
        print("C is the largest of the three, and the value is %d." % c)
elif b > c:  
    print("B is the largest of the three, and the value is %d." % b)
else:
    print("C is the largest of the three, and the value is %d." % c)
​
Copy the code

Code Exercise 1

# if statement
Conditional statement of a branch if conditional expression: statement block Note: 1. Conditional expressions must be followed by a colon (:) 2. The next line of code is executed with an indent of 4 Spaces: if the conditional expression is True, the following statement block "" is executed.# baozi=10
# xigua=2
# if xigua>0:
# baozi=1
# print("----")# print(baozi)If condition 1: block 1 elIF condition 2: block 2... Note: 1. Do not write conditions after else, write colon (:) 2. Start the statement under the branch on another line, indent 4 Spaces, and align the 3. Else branch is not required to execute: 1. If condition 1 is True, execute block 1 to terminate the condition; if condition 1 is False, continue with 2. If condition 2 is True, execute block 2 to terminate the condition; if condition 2 is False, continue with 3. Execute the block "" under the else branch if none of the list conditions are standing
# score=70# if score==100:
#     print("太棒了!")
# elif score>=90:
# print(" Excellent!" )
# elif score>=80:
#     print("良好")
# elif score<60:
# print(" Come on!" )
# else:
# print(" Qualified! ") )Exercise 1. Input an integer, is the output positive? The negative? Zero? ' ' '
​
num = int(input('Enter an integer:')) 
​
if num>0:
    print(num,It's positive.)
elif num==0:
    print(num,"Zero")
else:
    print(num,"It's negative.")
Copy the code

2.2 Loop Statements

  • While loop statement

    • We can loop through statements with some conditional judgments.
    • process
    • else

The code example

  • While loop statement
i = 1
sum = 0
while i <= 100:
    sum += i
    i += 1
print("1 + 2 +.... + 100 = %d" % sum)
​
Copy the code
  • While loop statement + condition statement
Example: Calculate the sum of all even numbers from 1 to 100
i = 1
sum = 0
while i <= 100:
    if i % 2= =0:
        sum += i
    i += 1
print("1 + 2 +.... + 100 = %d" % sum)
​
Copy the code
  • The while loop + the else
num = 1
while num <= 5:
    print(num)
    num += 1
else:
    print(num, "If the condition is not met, the loop exits.")
Copy the code

Code Exercise 2

# the while loop
While condition: statement block Note: 1. The condition is followed by a colon (:) 2. 3. Accidentally write an infinite loop, you can press Ctrl+C to terminate execution: 1. If the condition is True, continue, otherwise end the loop. 2. Execute the block under while, continue with 1 ""
```py
n=1
total=0

while n<=100:
     total+=n 
n+=1

print("total:",total) 
print("n=",n)   
Copy the code

While while condition with else branch: statement block else: statement block n Execution: 1. If the condition is True, continue, otherwise execute the block in the else branch, ending loop 2. Execute the block under while (the body of the loop), and continue with 1

n=1
total=0

while n<=100:
    total+=n 
    n+=1
else:
    print("n=",n) 

print("total:",total) 

Copy the code

1.3 break, continue to pass

  • break

    • When a condition is met, the loop exits and the loop language code is no longer executed. The else branch after break while is not executed.
  • continue

    • When one of the conditions is met, the loop exits and the next loop code executes.
  • pass

    • Pass is an empty statement that does nothing and is generally used to keep the structure intact.

The code example

Break and continue

i = 0
while(i <= 5):
    i += 1
    if i == 2:
        continue  # exit the current loop and proceed to the next loop
    elif i == 4:
        break  # exit loop
    print(i)
​
Copy the code

pass

if True:
    pass
print("done")
​
Copy the code

Code Exercise 3

#break - The loop is broken to exit the loop# n=0
# while n<100:
# n+=1
# print(n)
# if n==4:
# break# print("n=",n)#continue - Continue is executed in a loop, the rest of the loop is not executed, and the next loop judgment continues# n=0
# while n<100:
# n+=1# if n==4:
# continue
# # break
    
# print(n)
# else:
# print("-----------")
    
​
# print("n=",n)Exercise # Calculate the sum of odd numbers up to 100
# n=1
# sum=0
# while n<100:
# if n%2==1:
# sum+=n
# n+=1
# else:
# # print("sum:",sum)
# print("sum:"+str(sum))Calculate the sum of odd numbers up to 100 using cotinue
n=0
sum=0
while n<100:
    n+=1
​
    if n%2= =0:
        continue
    
    sum+=n 
else:
    print("sum:".sum)
Copy the code

Code Exercise 4

# loop nesting
While condition 1: statement block WHILE condition 2: inner loop statement block execution: 1. If condition 1 is True, continue, otherwise end outer loop 2. If condition 2 is True, execute inner loop block, continue 2; if condition 2 is False, end inner loop, continue 1 ""Print the multiplication table
i=1while i<=9:
    j=1
    while j<=i:
        print(str(j)+"x"+str(i)+"="+str(j*i),end="\t")      Print the character specified by end after normal content output. The default end = "\ n"
        j+=1
    else:
        print(end="\n")   # in a row
​
    i+=1
Copy the code

1.4 a for loop

  • The for loop

The code example

# for statement
For variable in String: statement block Note: 1. The string should be followed by a colon 2. The following blocks should be indented with 4 Spaces and aligned with ""
mystr="hello"for c in mystr:
    print(c) 
​
Print from back to front
i = len(mystr)-1    # maximum index
while i>=0:
    print(mystr[i]) 
    i-=1
Copy the code

\