In programming, statements are directives instructing the computer to perform actions. They act as commands guiding the computer’s operations. Among various types of statements, Jump Statements in Python are discussed in this article, providing examples and covering their types and syntax. This article makes it easier to understand these concepts by highlighting the importance of Python training in Chennai.
What are Jump Statements?
Jump statements in Python are used to change the flow of code, preventing statements from being executed in the usual sequential order.
The types of Jump Statements in Python are:
- Break Statement
- Continue Statement
Break Statement
In Python, the break statement stops a loop when a certain condition is met. When the interpreter encounters it, the loop ends, and control moves to the next statement. Usually used with if statements in a loop or in switch-case statements.
Syntax:
Loop{
Condition:
break
}
Example:
# Example Python program illustrating the break statement
# Consider a list of fruits
fruits = [“apple”, “banana”, “orange”, “grape”, “watermelon”]
# Using a for loop to go through the list
for fruit in fruits:
print(“Current fruit:”, fruit)
# Break the loop if the fruit is “orange”
if fruit == “orange”:
break
print(“Exiting the for loop”)
Output:
Current fruit: apple
Current fruit: banana
Current fruit: orange
Exiting the for loop
Continue Statement:
The “continue” statement in Python is a loop control statement that instructs the loop to skip the remaining code in the current iteration and move on to the next. The code after the “continue” statement is omitted for the current iteration, enabling the loop to move on to the next one.
Syntax:
while True:
…
if x == 10:
continue
print(x)
Example:
# Consider a list of temperatures
temperatures = [25, 30, 22, 28, 35, 26]
# Utilizing a for loop to go through the list
for temp in temperatures:
if temp < 30:
continue
print(“Current temperature:”, temp)
print(“Exiting the for loop”)
Output:
Current temperature: 30
Current temperature: 35
Exiting the for loop