day07- the while loop process control - Review

Using a basic cycle. 
Print (1)
the while conditions:
Code 1
Code 2
Code. 3
Print (. 3)
: When the condition code block will run the same code is true composition retracted, until the condition to end the loop is false ,
after the end of the cycle is not affected top-level code continues to run.
Example:
COUNT =. 1
the while COUNT <. 3:
pirnt (COUNT)
COUNT + =. 1
Print ( "loop end") # top-level code

two, endless loop and efficiency

2.1 conditions are variable, cut variable does not occur conditions change due has been true, the loop is not infinite loop ends
Example:
COUNT =. 1
the while COUNT <. 3:
Print (COUNT) # condition as variables do not change, the loop condition is true has

2.1 true condition is not directly break the loop body and infinite loop to terminate cycle
Example:
the while True: # condition has been True
Print ( 'Hello') # loop not BREAK

2.3 infinite loop, the effects of different types of loop efficiency of
(1) the loop body only continue no other code:
Example:
the while True:
Continue
Due to continue to cycle skip, the maximum effect on the efficiency of
the most harmful

(2) has a loop-based mathematical operations
Example:
the while True:
. 1. 1 +
because of slower, the speed of operation with respect to continue operation speed soon, Effect followed by

(2) the loop body class of operation io
Example:
the while True:
X = iNPUT ( 'input')

the while True:
Print ( "it is a dead cycle")
because of the operation io it is io delay, operation speed with a previous level is not, in itself no
no much influence the efficiency of

three cycles of application
3.1 to validate user input
(1) duplicate code
(2) will not be repeated after the input of
Example:
Copy
the while True:
inp_name = iNPUT ( ' Please enter your account number: ')
inp_pwd the iNPUT = (' Please enter your password: ')

IF inp_name inp_pwd == == username and password:
Print (' Login successful ')
the else:
Print (' account name or password wrong)
Description: This is actually make a loop, but there io so there is no impact, the condition is true, obtain user input and to store the
comparison information, the same as the print: the login is successful, otherwise the print: account name or password wrong, after cycling has been running
3.2 to exit the loop of two ways
(1) set the condition to a variable, the initial value is set to True, when the need to exit the variables to False,
do not take effect the next time the conditional
Copy
Tag = True
the while Tag:
inp_name = input ( 'Please enter your account number:')
inp_pwd the iNPUT = ( 'Please enter your password:')

IF inp_name inp_pwd == == username and password:
Print ( 'Login successful')
Tag = False after # the code will run, not take effect until the next cycle to determine the condition

the else:
Print ( 'account name or password is incorrect')

Print ( 'End ==== ====')


(2) True condition plus break the way, break will immediately terminate the current cycle, after the code will not run
Example:
Copy
the while True:
inp_name the iNPUT = ( 'Please enter your account number:')
Inp_pwd the INPUT = ( 'Please enter your password:')

inp_name username and inp_pwd == IF == password:
Print ( 'Login successful')
BREAK # layer immediately terminate this cycle
the else:
Print ( 'account name or password is incorrect')

Print ( 'End ==== ====' ) #break not run code following

four loop embedded in the end
4.1 of each layer with a variable conditions, the initial variable value is True,
the inner variable to False, the conditions of each layer are to False, there are memory exit to the outer layer by layer
Tag = True
the while Tag:
the while Tag:
the while Tag:
Tag = False
Example:
Copy
Tag = True
the while Tag:
inp_name the iNPUT = ( 'Please enter your account number:')
inp_pwd the iNPUT = ( ' Please enter your password: ')

IF inp_name inp_pwd == == username and password:
Print (' Login successful ')
the while Tag:
cmd = the iNPUT ( "Enter the command>: ")
cmd == IF 'Q':
Tag = False
the else:
Print ( '{x} is running command' .format (X = cmd))
the else:
Print ( "account name or password ')

4.2 True condition and break with inlaid cycle, each layer may need BREAK
the while True:
the while True:
the while True:
BREAK
BREAK
BREAK
'' '
Example:
Copy
the while True:
inp_name the iNPUT = (' Please enter your account number: ')
inp_pwd the iNPUT = (' Please enter your password: ')

IF inp_name inp_pwd == == username and password:
Print (' Login successful ')
the while True:
cmd = the iNPUT ( "enter the command>:")
IF cmd ==' q ':
break
print ( '{x} is running command' .format (X = cmd))
BREAK layer of the present cycle is terminated immediately #
the else:
print ( 'account name or password')

print ( 'End ==== ====' )

8, the while + continue: the end of this cycle, the next time direct access to
emphasize: continue at the same level after adding the code meaningless, because never run

Guess you like

Origin www.cnblogs.com/zhangtieshan/p/12452802.html
Recommended