If
you want to skip current iteration and immediately jump to a next iteration in
that case you can use the continue statement. Now we’ll show the use of the
continue statement by applying it to the program from the previous post. The
program is very simple and it will print out everything that user types but
when the user types done it will exit the program and print out ‘Done!’. The
new option is when you type anything with # (hashtag) for example #print this
it will not print the user input because of continue statement. So using
continue statement will skip the current iteration and jump to the next.
while True: line = raw_input('=> ') if line[0] == '#' : continue if line == 'done': break print line print 'Done!' => Print this Print this => #print this => print this! print this! => done Done!
As
you can see from the result all the lines that starts with ‘#’ sign are not
printed because when the continue command/statement is executed, it ends the
current iteration and jumps back to the while statement to start the next
iteration, so it will skip the print statement.
Nema komentara:
Objavi komentar