Conditional Loops

A conditional loop is a loop that carried out until a particular condition is met. It will carry on until this condition is no longer True. Python only has one conditional loop structure , a WHILE loop, but others are available.

WHILE Statements

A while loop checks the condition before the loop is executed the first time, it is useful for input validation.

It is important when using conditional loops to ensure that you avoid an infinite loop. This is when the condition is never met so the loop will carry on forever. To avoid this always make sure (particularly during input validation) that you allow the user to re-enter data etc. An example flow chart is shown below.

The conditional loop is shown in the yellow shaded part. It tells the user that their input is invalid and allows them to re-enter their data. This only happens when the price is less than or equal to 0.

Python Example

The Python code above will ask the user to enter a price and validate it. The price will be classed as invalid if it is less than or equal to 0. It can be executed below.

Tutorial Video - Conditional Loops

SDD 6 Condtional Loops.mp4