Control Flow in Python and control flow in Python
1. if statement:
The if statement is used to test a condition. if the condition is true, we run a statement (called the if-block). Otherwise, we will process another statement (called the else-block ). The else clause is optional.
You can also use the elif statement, which combines two associated if else-if else statements into an if elif else statement to simplify the program and reduce the number of indentation required.
Note: whether it is an if statement, an elif statement, or an else statement, it is followed by a colon.
Elif and else statements are always optional.
The if statement can be written again, which becomes the nested if statement.
Note: There is no switch statement in Python.
2. while statement:
If one condition is true, the while statement allows you to execute one statement repeatedly. A while statement is an example of a loop statement. The while statement has an optional else clause.
Note: The while statement is followed by a colon and the else statement is optional.
3. for statement:
For... in is another loop statement that iterates over a sequence of objects, that is, each item in the sequence is used one by one.
Similarly, the for and else statements must be followed by colons. else statements are optional.
The for... in loop applies to any sequence. We often use a list of numbers generated by the built-in range function, but in a broad sense, we can use a sequence of any types of objects!
4. break statement:
The only statement of a break statement is to terminate the loop, that is, it jumps out of the loop when a break is encountered.
An important note is that if you terminate a for or while LOOP, no corresponding loop else block will be executed. That is, they think that their else statements are part of the loop and also jump out.
5. continue statement:
The continue statement is used to tell Python to skip the remaining statements in the current loop block and continue the next loop.