Link for Control Flow Statements Programs in Python
Control flow statements in Python allow us to manage the order in which the 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. Decision/Conditional Statements
- Conditional statements allow us to execute code blocks based on certain conditions.
- They are of the following types –
Introduction
- In real life, we make decisions all the time. For example, if it rains, we take an umbrella; if we are hungry, we eat food; and if we are tired, we go to sleep. Similarly, in Python, we can make our programs make decisions using decision statements.
- These statements allow a program to choose what action to perform based on certain conditions.
Definition
- A decision statement in Python is a special type of instruction that tells the program to execute a particular block of code only if a given condition is true, and sometimes to execute an alternative block of code if the condition is false.
Types of Decision Statements in Python
(a) if Statement
- The if statement is the simplest form of decision-making.
- It checks whether a condition is true. If the condition is true, it executes the code inside it; otherwise, it simply skips that code.
- For example: “If your age is greater than or equal to 18, then the program allows you to vote.”
(b) if-else Statement
- The if-else statement provides two paths. If the condition is true, one block of code is executed(if block), and if the condition is false, another block of code is executed(else block).
- For example: “If it is raining, then the program tells you to take an umbrella. Otherwise, it tells you to go without an umbrella.”
(c) if-elif-else Statement
- The if-elif-else statement is useful when there are multiple conditions to check. Python will test each condition in order, and the first true one will have its block of code executed. If none are true, the else block is executed.
- For example: “If you score 90 or above, the program says you got an A grade. If you score between 75 and 89, the program says you got a B grade. If you score between 50 and 74, the program says you got a C grade. Otherwise, it says you failed.”
(d) Nested if Statement
- A nested if means putting one if statement inside another. This is useful when we want to check multiple levels of conditions.
- For example: “If the weather is sunny, then check if the temperature is above 30. If it is, the program says it is a hot day. If it is not, the program says it is a pleasant day.”
Characteristics of Decision Statements
- They help the program choose between different options.
- They make the program more intelligent by allowing it to react to situations.
- They are always based on conditions that evaluate to either True or False.
Advantages of Decision Statements
- They make the program flexible and interactive because it can behave differently under different circumstances.
- They reduce repetition of code by directing the flow instead of writing separate programs.
- They help in solving real-world problems logically, such as eligibility checks, grading systems, or weather notifications.
Disadvantages of Decision Statements
- If there are too many nested or complex conditions, the code can become harder to read and maintain.
- Wrongly written conditions may lead to logical errors that are sometimes difficult to find.
- They may slightly slow down the program if there are a very large number of conditions to check.
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