Skip to content

Decision Making & Loops

surendrabisht edited this page Jul 13, 2020 · 3 revisions

Decision Making

Whenever we want to enable decision making in our program and need to run a part of code on conditional basis, we use if-elif-else constructs.
if this happens,then do this. if this doesn't happen, don't do this. if all the conditions fail , just do this. Such decisions what we make in our daily life can be simulated in programs.

if elif else

Flow of if-elif-else Contruct

-> Now, if if-condition is true, then statements inside it are executed and exited from the if-elif-else block.
   -> if not, then it will check if elif-condition true, then statements inside it are executed and program will exit from the block.
      -> if all above conditions are false, else block will execute and program will exit from the block.

flow diagram


Looping

If we want to perform some sequence of steps multiple times, we can't just copy and paste those lines of code again and again to have them executed N no.of times.
Here comes this topic, Loops. Loops as name says it runs in a circle i.e checks condition and then run the code and again checks condition and then runs the code and so forth number of times you want a particular code to run. The condition in the loops need to be false to exit out of the loops and run further line of code ahead.

There are 2 ways in python you can use looping: 1.) while loops 2.)for loops

While Loop

while loop

For loop

for loop


NOTE: Decision Making (i.e. if else ) and loops are the major building blocks of your program. Do practice a lot of questions before moving ahead.

Clone this wiki locally