Link for Control Flow Statements Programs in Python
- Control flow statements in Python allow us to manage the order in which our code is executed.
Types of Control Flow Statements in Python
They are of three types –
- Decision/Conditional Statements (if, else, elif)
- Looping Statements (for, while)
- Control Flow Altering/Jumping Statements (break, continue, pass)
1. Conditional Statements
- Conditional statements allow us to execute code blocks based on certain conditions.
- They are of the following types –
(a) if Statement
-
- The if statement evaluates a condition. If the condition is True, the indented code block is executed.
- For example –
(b) if-else Statement
-
- The if–else statement provides an alternative block to execute if the condition is False.
- For example –
(c) if-elif-else Statement
-
- The if–elif–else structure allows checking multiple conditions.
- In this, If one condition is True, its corresponding code block is executed.
- For example –
(d) Nested if Statements
-
- We can nest if statements inside other if blocks to create more complex conditions.
- For example –
2. Looping Statements
- The concept of Loops allows us to execute a block of code multiple times, based on a condition or iterating over a sequence.
- They are of the following types –
(i) for Loop
-
- The for loop is used to iterate over a sequence (such as a list, tuple, or string) or any iterable object.
- For example –
-
- We can also loop through a range of numbers using the range() method.
(ii) while Loop
-
- The while loop continues to execute a block of code as long as the condition is True.
- For example –
3. Control Flow Altering/Jumping Statements
- These statements are used to modify the normal flow of loops or conditionals.
- They are of the following types –
(I) break Statement
-
- The break statement is used to exit a loop prematurely when a condition is met.
- For example –
(II) continue Statement
-
- The continue statement skips the rest of the current iteration and moves on to the next iteration.
- For example –
(III) pass Statement
-
- The pass statement is a placeholder that does nothing.
- It‘s used where a statement is syntactically required but no action is needed.
- For example –
0 Comments