{ "cells": [ { "cell_type": "markdown", "id": "6baeba6d", "metadata": {}, "source": [ "\n", "# `while` Loops" ] }, { "cell_type": "markdown", "id": "fe7bc82f", "metadata": {}, "source": [ "When programming, there's often a need to execute a specific set of instructions repeatedly until a certain condition is met. This is precisely where `while` loops come into play in Python. A `while` loop is a fundamental control structure that allows you to repeat a block of code as long as a specified condition remains `True`. It provides the flexibility and power to create dynamic and adaptable programs. \n", "\n", "## What is a While Loop?\n", "\n", "A `while` loop, as the name suggests, keeps looping or iterating as long as a particular condition holds `True`. The loop begins with the evaluation of a condition, and if that condition is met (evaluates to `True`), the code block within the loop is executed. After each iteration, the condition is checked again, and if it still holds `True`, the loop continues to execute. This process continues until the condition becomes `False`, at which point the loop terminates, and the program proceeds with the next set of instructions.\n", "\n", "## Why Use While Loops?\n", "\n", "`while` loops are used for a variety of reasons in programming:\n", "\n", "- **Repetition**: They allow you to perform a series of tasks repeatedly without the need to write the same code over and over again. This is particularly useful when you want to automate repetitive tasks or perform operations on a changing dataset.\n", "\n", "- **Dynamic Control**: `while` loops provide a dynamic way to control the flow of your program. The loop can adapt to changing conditions, making it a valuable tool for scenarios where the number of iterations is not known in advance.\n", "\n", "- **User Interaction**: `while` loops are often used to interact with users. They can keep a program running until the user decides to exit, ensuring continuous interaction and input.\n", "\n", "## Demonstrating While Loops\n", "\n", "Let's dive into a practical example to illustrate the concept of while loops in Python:" ] }, { "cell_type": "code", "execution_count": 1, "id": "65e8586b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Counter Value: 0.\n", "Counter Value: 2.\n", "Counter Value: 4.\n", "Counter Value: 6.\n", "Counter Value: 8.\n" ] } ], "source": [ "count = 0\n", "# while count is less than 10\n", "while count < 10:\n", " print(f\"Counter Value: {count}.\")\n", " count += 2" ] }, { "cell_type": "markdown", "id": "c43176c4", "metadata": {}, "source": [ "In this example, the `while` loop starts with `count` equal to 0. The condition `count < 10` is evaluated, and since it's `True` that `count` is less than 10, (it is 0), the loop executes the block of code within it. It prints the current value of `count`, increments `count` by 2, and then re-evaluates the condition. As long as `count` remains less than 10, the loop continues to execute, printing the updated value of `count` and incrementing it by 2 in each iteration. When `count` becomes 10, the condition becomes `False`, and the loop terminates." ] } ], "metadata": { "jupytext": { "cell_metadata_filter": "-all", "formats": "ipynb,md:myst", "main_language": "python", "notebook_metadata_filter": "-all" }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.4" } }, "nbformat": 4, "nbformat_minor": 5 }