8. Basic use of while loop
8.1 Basic syntax of while statement
Initial condition setup - usually a counter that repeats itself while condition (judging whether the counter reaches the target number of times): When conditions are met, do something 1 When the condition is met, do something 2 When the conditions are met, do something 3 ...(omit)... processing condition (counter + 1)
Note :
- The while statement and the indented part are a complete block of code
8.2 break 和 continue
break and continue are keywords used exclusively in loops
- When a certain condition of break is satisfied , exit the loop and no longer execute subsequent repeated code
- Continue When a certain condition is met , the subsequent repeated code will not be executed, and the loop will be executed.
break and continue are only valid for the current loop
- break
-During the loop , if you no longer want the loop to continue executing after a certain condition is met , you can use break to exit the loop
- continue
- During the loop , if you don't want to execute the loop code after a certain condition is met , but you don't want to exit the loop , you can use continue
- That is: in the entire loop, there are only certain conditions , the loop code does not need to be executed, and other conditions need to be executed
- Note: When using continue, the code in the conditional processing part needs special attention , and an infinite
continue is only valid for the current loop
8.3 Nested while loops
- while nesting is: while there is a while inside
while condition 1 : When conditions are met, do something 1 When the condition is met, do something 2 When the conditions are met, do something 3 ...(omit)... while condition 2 : When conditions are met, do something 1 When the condition is met, do something 2 When the conditions are met, do something 3 ...(omit)... Processing condition 2 Processing condition 1
9. Escape characters in strings
- \t prints a tab to help keep the vertical alignment of the output text
- \n prints a newline
The function of the tab character is to vertically align text by column without using a table
escape character |
describe |
\ |
backslash symbol |
\' |
apostrophe |
\" |
Double quotes |
\n |
newline |
\t |
horizontal tab |
\r |
Enter |