Control Statements in Python
Loops are employed in Python to iterate over a section of code continually. Control statements are designed to serve the purpose of modifying a loop's execution from its default behaviour. Based on a condition, control statements are applied to alter how the loop executes. In this tutorial, we are covering every type of control statement that exists in Python.
Control Statements in Python
The if statement is arguably the most used statement to control loops. For instance:
1 2 2 1 2
In Python, the break statement is employed to end or remove the control from the loop that contains the statement. It is used to end nested loops (a loop inside another loop), which are shared with both types of Python loops. The inner loop is completed, and control is transferred to the following statement of the outside loop.
Itika of state Jaipur is eligible to vote
In the above code, if a candidate's age is less than or equal to 18, the interpreter won't generate the statement of eligibility. Otherwise, the interpreter will print a message mentioning that the candidate is eligible to vote in the console.
When a Python interpreter sees a continue statement, it skips the present iteration's execution if the condition is satisfied. If the condition is not satisfied, it allows the implementation of the current iteration. It is employed to keep the program running even when it meets a break while being executed.
Letter: I Letter: a Letter: m Letter: a Letter: c Letter: o Letter: d Letter: e Letter: r
In this code, when the if-statement encounters a space, the loop will continue to the following letter without printing the space.
If the condition is met, the pass statement, or a null operator, is used by the coder to leave it as it is. Python's pass control statement changes nothing and moves on to the following iteration without stopping the execution or skipping any steps after completing the current iteration.
A coder can put the pass statement to prevent the interpreter from throwing an error when a loop or a code block is left empty.
Letter: P Letter: y Letter: t Letter: h Letter: o Letter: n
Even if the condition was satisfied in the code above, as we can see, the pass statement had no effect, and execution went on to the subsequent iteration.
else in Loop
The else statement is used with the if clause, as we have previously learned. Python supports combining the else keyword with the for and the while loop. After the code block of the loop, the else block is displayed. After completing all the iterations, the interpreter will carry out the code in the else code block. Only when the else block has been run does the program break from the loop.
Current iteration: 1 Current iteration: 2 Current iteration: 3 Current iteration: 4 Current iteration: 5 This is the else block Outside the loop
A match statement compares the value of an expression to a series of patterns. These patterns are presented as some case blocks. Contrary to appearances, this is more akin to pattern checking in other languages like Rust than to switching clauses in C, Java (or any other coding languages). It can also pull components (sequential elements or object characteristics) from the values into variables, and only the first matching pattern is executed.
The most straightforward form contrasts a subject value with one or more regular expressions:
Error 400: Bad request Error 404: Link Not found
JavaTpoint offers too many high quality services. Mail us on [email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected]
Duration: 1 week to 2 week