Control Flow in Python
Do not miss this exclusive book on Binary Tree Problems. Get it now for free.
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:
Output:
If_ elif _else Statement
Code:
Output:
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:
Output:
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:
Output:
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:
Output:
MCQ Round
Question 1
In which loop the condition is checked afterwards?
Question 2
What is used for breaking the Loop?
Sign up for FREE 3 months of Amazon Music. YOU MUST NOT MISS.