Elias Mukhtarzada For Loop Versus While Loop

docx

School

George Mason University *

*We aren’t endorsed by this school

Course

109

Subject

Computer Science

Date

Dec 6, 2023

Type

docx

Pages

3

Uploaded by KidMetalKookabura5

Report
Elias Mukhtarzada IT-109-DL1 17 May 2022 Differences Between a For Loop and While Loop In Python, loops are generally used to allow the execution of several lines of code for a consecutive number of times. However, there are commonly two types of loops that include for loop and while loop. When it comes to while loop, this is a function that, once inserted into a Python program, it will run multiple statements over again until the condition is in fact true. Also, a while loop is more applicable in circumstances where we are unsure of the number of times that the code will be iterated. As stated by Software Testing Help, the while loop condition shares similarities to an if statement, but if statements will only execute the segment of code one time only if its condition is true (Software Testing Help, 2022). Another important factor that novice programmers need to understand is that while loop applies the comparison operators such as less than, greater than, or equal to for its condition in addition to Booleans such as X and Y for example (Software Testing Help, 2022). Software Testing Help provides another example where the Python file could check to see if a number is zero, but it instead returns a one. In this case, the while loop could have a predetermined condition that says n is greater than or equal to one (n>=1), so if it is true then the formula will be computed in the while loop block (Software Testing Help, 2022). These are all key features that distinguish the while loop from the for loop. As for the for loop, it works best when you iterate objects such as lists, strings, and tuples. It is also important to remember that objects in a for loop will be executed in repetition for a specific number of times depending on the number of items within the object. Software
Testing Help mentions that a condition inside a for loop will remain true if it has not iterated through every item in the iterable object (Software Testing Help, 2022). In one example, the for loop can iterate through a number sequence starting from one to twenty, and each consecutive number will be printed. Then, the process of iteration will end once all the numbers in the predetermined sequence are printed (Software Testing Help, 2022). There may be another example where you have a random sentence that is replaced with a string in Python, and you implement the for loop which could calculate how many times each word was used in that sentence. The for loop often has a syntax that may say “for n in”, and it has an index that starts at zero (Software Testing Help, 2022). These are just some of the facts to know about for loops with examples of how they can function. In hindsight, the major difference between while loop and for loop is that a while loop will execute lines of code as long as the condition is at a true state, whereas a for loop executes code for n number of times based on the number of items in a iterable object.
APA Citations Python loops - for, while, nested loops with examples. Software Testing Help. (2022, May 4). Retrieved May 17, 2022, from https://www.softwaretestinghelp.com/python/looping-in- python-for-while-nested-loops/#:~:text=Answer%3A%20Python%20generally %20supports%20two,for%20loop%20and%20while%20loop .
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help