A Byte of Python

Chapter 6. Control Flow

Table of Contents

Introduction
The if statement
Using the if statement
How It Works
The while statement
Using the while statement
The for loop
Using the for statement
The break statement
Using the break statement
The continue statement
Using the continue statement
Summary

Introduction

In the programs we have seen till now, there has always been a series of statements and Python faithfully executes them in the same order. What if you wanted to change the flow of how it works? For example, you want the program to take some decisions and do different things depending on different situations such as printing 'Good Morning' or 'Good Evening' depending on the time of the day?

As you might have guessed, this is achieved using control flow statements. There are three control flow statements in Python - if, for and while.