While Loop and For Loop

0
9كيلو بايت

In Python, while and for loops are fundamental constructs for repeated execution of code blocks. They serve different purposes, so understanding their strengths is crucial for writing efficient and readable code.

While Loop:

  • Syntax:

Python
while condition:
    # Code to execute as long as the condition is True

  • Functionality:

    • The while loop repeatedly executes a block of code as long as a certain condition remains True.
    • The condition is evaluated at the beginning of each loop iteration.
    • If the condition becomes False, the loop terminates.
  • Example:

Python
count = 0
while count < 5:
    print(f"Count: {count}")
    count += 1  # Increment counter

This loop prints "Count:" followed by the current value of count five times. The loop continues as long as count is less than 5.

For Loop:

  • Syntax:

Python
for item in iterable:
    # Code to execute for each item in the iterable

  • Functionality:

    • The for loop iterates over elements in a sequence (like a list, tuple, or string) called an iterable.
    • In each iteration, the current element is assigned to the loop variable (item in this example).
    • The code block is executed for each element in the iterable.
  • Example:

Python
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(f"I like {fruit}.")

This loop iterates over the fruits list. In each iteration, the current fruit (e.g., "apple") is assigned to fruit, and the message is printed.

Choosing the Right Loop:

  • Use a while loop when you don't know the exact number of iterations beforehand, and the loop continues based on a condition.
  • Use a for loop when you need to iterate over a sequence of elements in a known order. It's generally more concise and readable for this purpose.

Additional Considerations:

  • You can use the break statement to exit a loop prematurely.
  • The continue statement skips the current iteration and moves to the next one.
  • for loops can sometimes be rewritten as while loops (and vice versa), but using the appropriate loop for the situation improves code clarity.
البحث
الأقسام
إقرأ المزيد
Computer Programming
HTML Table Sizes: Controlling Dimensions
HTML tables can be sized using the width and height attributes, which specify the dimensions of...
بواسطة HTML PROGRAMMING LANGUAGE 2024-09-06 01:29:20 0 11كيلو بايت
Technology
Ethics of Using Computers Between People
The use of computers and digital technology in interactions between people raises numerous...
بواسطة ALAGAI AUGUSTEN 2024-07-13 07:16:31 0 12كيلو بايت
Computer Programming
Creating your first HTML file
Step-by-Step Guide 1. Choose a Text Editor: Basic Text Editors: Notepad (Windows), TextEdit...
بواسطة HTML PROGRAMMING LANGUAGE 2024-08-13 03:22:25 0 9كيلو بايت
التعليم
Immigration timeline 1849-1924
1849: America’s first anti-immigrant political party, the Know-Nothing...
بواسطة Modern American History 2024-08-02 16:41:54 0 11كيلو بايت
Mathematics
Understanding Quadratic Equations:
A quadratic equation is a polynomial equation of degree 2. It can be expressed in the general...
بواسطة Mpatswe Francis 2024-08-30 06:49:57 0 11كيلو بايت
Talksphare https://talksphare.com