×

Search anything:

Control Flow in Python

Binary Tree book by OpenGenus

Open-Source Internship opportunity by OpenGenus for programmers. Apply now.

Reading time: 15 minutes

Many times a person often come across situations and for that he have to choose the condition which is best for him. Similarly, In Control Flow, there are certain conditions which we put with the help of predefined syntaxes and the program then run in that order and chooses the condition which we want as a output.

The Control Flow can be categorized as:

  • Conditional statements
  • Iteration statements

In Conditional Statements, there are some specified set of conditions on which the output depends while in Iteration Statements, the program repeatedly executes the block of statements with respect to a given condition.

We are assuming that you know some basics of python so that you can understand the code easily.

Conditional Statements

In this, the condition will be checked first and then according to the condition the appropriate output will be printed.

If_ _else Statement

Code:
code1
Output:code1-1

If_ elif _else Statement

Code:
code-2
Output:
code1-2

Iteration Statements

The user will give condition and according to that condition, the loop will be executed repeatedly.

For Loop

In this loop the first condition will be checked then it will go inside the loop and execute the code in the loop.

Code:
Code-3
Output:code2

Do_ _While Loop

In this loop first, the code will be executed for once and then the condition will be checked if the condition is false then the loop will break.

Code:
Code-5
Output:code3

While Loop

In this loop for once condition will be checked then the program will be executed and after that increment will be done.

Code:
Code-6
Output:code4-1

MCQ Round

Question 1

In which loop the condition is checked afterwards?

For Loop
While Loop
Do While Loop
If Else Condition

Question 2

What is used for breaking the Loop?

Pass
Continue
Exit
Break
Control Flow in Python
Share this