The while Loop

Repeat a block for as long as a condition holds โ€” colon and indentation instead of braces, and the multiplication table exercise that ties it together.

Overview

This topic covers the while loop in Python โ€” one of Python's two main looping constructs (the other being for). The while loop repeats a block of code as long as a given condition remains True. The notes also touch on how while looks in C-style languages vs. Python, and briefly mention do-while (which Python does not have natively).

Key Concepts

Detailed Explanation

1. while Syntax โ€” C-style vs Python

The instructor compares the general/C-style while syntax with Python's syntax to highlight the difference:

C-style (curly braces):

while(condition){
    code
}

Python-style (colon + indentation):

while condition:
    code
๐Ÿ’ก Tip: In Python, indentation is not just for readability โ€” it defines the block of code that belongs to the loop. Incorrect indentation will cause errors or unexpected behavior.

A related comment block also lists # while, # do while, and # for โ€” indicating these are the loop types being discussed/compared in the video.

โš ๏ธ Note: Python does not have a built-in do-while loop (unlike C/C++/Java). This was likely mentioned only as a comparison point, not something implemented in Python. โš ๏ธ Screenshot text unclear โ€” the video doesn't show a Python do-while example, only the label in a comment.

2. Basic Loop Structure (Anatomy)

A while loop generally needs 3 parts:

  1. Initialization โ€“ set up a counter/loop variable before the loop starts (e.g., i = 1)
  2. Condition โ€“ checked before every iteration (e.g., i < 11)
  3. Update/Increment โ€“ changes the loop variable inside the loop body so the condition eventually becomes False (e.g., i += 1)

If you forget the update step, the condition never becomes False โ†’ infinite loop.

Example 1 โ€” Simple Multiplication Table

number = int(input("Enter the number"))

i = 1

while i < 11:
    print(number * i)
    i += 1

Explanation of the code:

Example run (input = 11):

Enter the number11
11
22
33
44
55
66
77
88
99
110

Important Points:

Example 2 โ€” Multiplication Table with Full Equation Format

number = int(input("Enter the number"))

i = 1

while i < 11:
    print(number, "*", i, "=", number * i)
    i += 1

Explanation of the code:

Example run (input = 24):

Enter the number24
24 * 1 = 24
24 * 2 = 48
24 * 3 = 72
24 * 4 = 96
24 * 5 = 120
24 * 6 = 144
24 * 7 = 168
24 * 8 = 192
24 * 9 = 216
24 * 10 = 240

Important Points:

Things to Remember

Quick Revision

A while loop in Python repeats a block while a condition is True, using a colon and indentation instead of curly braces (unlike C-style languages). Every while loop needs an initialized variable, a condition, and an update step inside the loop โ€” skip the update and you get an infinite loop. The example used throughout is a multiplication table generator: take a number as input, loop i from 1 to 10, and print either number * i or the full equation number * i = result. This is a classic beginner exercise for practicing loop initialization, condition-checking, and incrementing.