Control Flow

Control flow refers to the order in which statements are executed in a program. It allows you to control the flow of execution based on certain conditions or criteria. In Python, control flow is achieved using conditional statements (if, elif, else) and looping statements (for, while). Here's an explanation of control flow constructs in Python:

Conditional Statements(if, elif, else)

OUTPUT:-

You are underage.

if age=25

o/p:- You are an adult.

Loop Statements

Looping statements in Python allow you to repeat a block of code multiple times. There are two main looping statements in Python: for and while. Here's an explanation of each:

for loop:

while loop:

Break and Continue Statements

The break and continue statements are used to control the flow of a loop. Here's an explanation of how break and continue work:


Break statement:

The break statement is used to exit the loop entirely, regardless of the loop's condition.

When the break statement is encountered within a loop, the loop is immediately terminated, and the program continues with the next statement after the loop.

It is typically used to exit a loop prematurely when a certain condition is met.

In this example, the loop iterates from 0 to 9. When i equals 5, the break statement is encountered, and the loop is exited immediately. As a result, the numbers 0 to 4 are printed, and the loop ends.

Continue statement

The continue statement is used to skip the current iteration of the loop and move to the next iteration.

When the continue statement is encountered, the remaining code in the loop for that iteration is skipped, and the loop proceeds to the next iteration.

It is typically used to skip specific iterations when certain conditions are met.

In this example, the loop iterates from 0 to 9. When i is an even number (divisible by 2), the continue statement is encountered, and the remaining code for that iteration is skipped. As a result, only the odd numbers 1, 3, 5, 7, and 9 are printed.