Chapter 4: Flow Control
In this chapter, “Mastering Python Fundamentals”, we will explore flow control, an essential concept in programming that allows you to dictate the order in which your code executes. Flow control enables your program to make decisions, repeat actions, and handle multiple scenarios based on different conditions. You’ll learn about conditional statements (if
, elif
, else
), loops (for
, while
), and other control structures. By the end of this chapter, you’ll be able to write more dynamic and flexible Python programs.
4.1 Conditional Statements
Conditional statements allow your program to make decisions based on specific conditions. The most common conditional statements in Python are if
, elif
, and else
.
4.1.1 if
Statement
The if
statement checks if a condition is true. If it is, the code block under the if
statement will execute.
Example:
a = 10
if a > 5:
print("a is greater than 5")
- Explanation:
- The condition
a > 5
is checked. - Since
10
is greater than5
, the message “a is greater than 5” is printed.
Step-by-Step Guide:
- Open your text editor.
- Type the code above into your editor.
- Save the file as
if_statement_example.py
. - Run the program by typing:
python if_statement_example.py
- The output will display “a is greater than 5” because the condition is true.
4.1.2 elif
Statement
The elif
(short for “else if”) statement checks another condition if the previous if
condition is false.
Example:
a = 3
if a > 5:
print("a is greater than 5")
elif a == 3:
print("a is equal to 3")
- Explanation:
- The first condition
a > 5
is false, so the program checks the next conditiona == 3
. - Since
a
is3
, the message “a is equal to 3” is printed.
Step-by-Step Guide:
- Open your text editor.
- Type the code above into your editor.
- Save the file as
elif_statement_example.py
. - Run the program by typing:
python elif_statement_example.py
- The output will display “a is equal to 3” because the second condition is true.
4.1.3 else
Statement
The else
statement executes a block of code if all preceding if
and elif
conditions are false.
Example:
a = 1
if a > 5:
print("a is greater than 5")
elif a == 3:
print("a is equal to 3")
else:
print("a is less than or equal to 5 and not equal to 3")
- Explanation:
- The conditions
a > 5
anda == 3
are false, so theelse
block executes, printing “a is less than or equal to 5 and not equal to 3”.
Step-by-Step Guide:
- Open your text editor.
- Type the code above into your editor.
- Save the file as
else_statement_example.py
. - Run the program by typing:
python else_statement_example.py
- The output will display “a is less than or equal to 5 and not equal to 3” because none of the previous conditions were true.
4.2 Loops
Loops allow your program to repeat a block of code multiple times. Python has two main types of loops: for
loops and while
loops.
4.2.1 for
Loop
A for
loop iterates over a sequence (like a list or range) and executes a block of code for each item in the sequence.
Example:
numbers = [1, 2, 3, 4, 5]
for number in numbers:
print("Number:", number)
- Explanation:
- The loop iterates over each item in the
numbers
list. - Each number is printed on a new line.
Step-by-Step Guide:
- Open your text editor.
- Type the code above into your editor.
- Save the file as
for_loop_example.py
. - Run the program by typing:
python for_loop_example.py
- The output will display each number in the list on a new line.
4.2.2 while
Loop
A while
loop repeats a block of code as long as a condition is true.
Example:
count = 0
while count < 5:
print("Count is:", count)
count += 1
- Explanation:
- The loop continues to run as long as
count
is less than5
. - The value of
count
increases by1
each time the loop runs, and the loop stops whencount
reaches5
.
Step-by-Step Guide:
- Open your text editor.
- Type the code above into your editor.
- Save the file as
while_loop_example.py
. - Run the program by typing:
python while_loop_example.py
- The output will display “Count is: 0” through “Count is: 4” on new lines.
4.3 break
and continue
Statements
The break
and continue
statements are used to control the flow of loops.
4.3.1 break
Statement
The break
statement exits a loop before it has looped through all items.
Example:
numbers = [1, 2, 3, 4, 5]
for number in numbers:
if number == 3:
break
print("Number:", number)
- Explanation:
- The loop will stop when it encounters the number
3
, so only1
and2
are printed.
Step-by-Step Guide:
- Open your text editor.
- Type the code above into your editor.
- Save the file as
break_statement_example.py
. - Run the program by typing:
python break_statement_example.py
- The output will display “Number: 1” and “Number: 2” before the loop breaks.
4.3.2 continue
Statement
The continue
statement skips the current iteration of the loop and continues with the next iteration.
Example:
numbers = [1, 2, 3, 4, 5]
for number in numbers:
if number == 3:
continue
print("Number:", number)
- Explanation:
- When the loop encounters
3
, it skips printing it and continues with4
and5
.
Step-by-Step Guide:
- Open your text editor.
- Type the code above into your editor.
- Save the file as
continue_statement_example.py
. - Run the program by typing:
python continue_statement_example.py
- The output will display “Number: 1”, “Number: 2”, “Number: 4”, and “Number: 5”, skipping
3
.
4.4 pass
Statement
The pass
statement is a null operation; it’s used when a statement is syntactically required but you don’t want any command or code to execute.
Example:
for number in range(5):
if number == 3:
pass # Do nothing
print("Number:", number)
- Explanation:
- When the loop encounters
3
, thepass
statement is executed, which does nothing, so the loop continues as usual.
Step-by-Step Guide:
- Open your text editor.
- Type the code above into your editor.
- Save the file as
pass_statement_example.py
. - Run the program by typing:
python pass_statement_example.py
- The output will display “Number: 0” through “Number: 4”, with no effect from the
pass
statement.
Flow control structures are vital for writing efficient and dynamic code. They allow you to make decisions, repeat tasks, and handle complex scenarios with ease. By mastering flow control, you’ll be able to create programs that can adapt to various inputs and conditions, making your code more powerful and flexible. Happy coding!
Read Next :