Differences between revisions 3 and 4
Revision 3 as of 2010-12-24 14:58:24
Size: 1693
Editor: EnalIcho
Comment:
Revision 4 as of 2012-09-29 17:53:26
Size: 1704
Editor: PaulBoddie
Comment: wiki restore 2013-01-23
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
Line 2: Line 3:
Line 5: Line 7:
Line 7: Line 10:
''While'' loops, like the ForLoop, are used for repeating sections of code - but unlike a ''for'' loop, the ''while'' loop will not run ''n'' times, but until a defined condition is met. As the ''for'' loop in Python is so powerful, ''while'' is rarely used, except in cases where a user's input is required, for example -
Line 9: Line 11:
{{{#!python numbers=disable
n=raw_input('Please enter\'hello\':')
while n.strip()!='hello':
    n=raw_input('Please enter\'hello\':')
''While'' loops, like the [[ForLoop|ForLoop]], are used for repeating sections of code - but unlike a ''for'' loop, the ''while'' loop will not run ''n'' times, but until a defined condition is met. As the ''for'' loop in Python is so powerful, ''while'' is rarely used, except in cases where a user's input is required, for example -







{{{
n = raw_input("Please enter 'hello':")
while n.strip() != 'hello':
    n = raw_input("Please enter 'hello':")
Line 14: Line 24:

Line 17: Line 29:
{{{#!python numbers=disable





{{{
Line 19: Line 37:
    n=raw_input('Please enter\'hello\':')
    if n.strip()=='hello':
    n = raw_input("Please enter 'hello':")
    if n.strip() == 'hello':
Line 23: Line 41:
As you can see, this compacts the whole thing into a piece of code managed entirely by the ''while'' loop. Having ''True'' as a condition ensures that the code runs until it's broken by n.strip() equalling 'hello'. The only problem with this is that it slows things down a lot - this is due to first testing the True condition for the ''while'', then again testing the n.strip() value compared to the string 'hello'. Another version you may see of this type of loop uses ''1'' instead of ''True'' - this is exactly the same, though in Python 2.*, using while ''1'' is minutely faster, due to the ability to reassign ''True'' to a different value - the interpretor needs to look up the value of the variable, as opposed to loading a constant.
Line 25: Line 42:
As a programmer, it is up to you which style to use - but always remember that readability is important, and that speed is also important.

As you can see, this compacts the whole thing into a piece of code managed entirely by the ''while'' loop. Having ''True'' as a condition ensures that the code runs until it's broken by n.strip() equalling 'hello'. The only problem with this is that it slows things down a lot - this is due to first testing the True condition for the ''while'', then again testing the n.strip() value compared to the string 'hello'. Another version you may see of this type of loop uses ''1'' instead of ''True'' - this is exactly the same, though in Python 2.*, using while ''1'' is minutely faster, due to the ability to reassign ''True'' to a different value - the interpretor needs to look up the value of the variable, as opposed to loading a constant.


As a programmer, it is up to you which style to use - but always remember that readability is important, and that speed is also important.

While loops

Usage in Python

  • When do I use them?

While loops, like the ForLoop, are used for repeating sections of code - but unlike a for loop, the while loop will not run n times, but until a defined condition is met. As the for loop in Python is so powerful, while is rarely used, except in cases where a user's input is required, for example -

n = raw_input("Please enter 'hello':")
while n.strip() != 'hello':
    n = raw_input("Please enter 'hello':")

However, the problem with the above code is that it's wasteful. In fact, what you will see a lot of in Python is the following -

while True:
    n = raw_input("Please enter 'hello':")
    if n.strip() == 'hello':
        break

As you can see, this compacts the whole thing into a piece of code managed entirely by the while loop. Having True as a condition ensures that the code runs until it's broken by n.strip() equalling 'hello'. The only problem with this is that it slows things down a lot - this is due to first testing the True condition for the while, then again testing the n.strip() value compared to the string 'hello'. Another version you may see of this type of loop uses 1 instead of True - this is exactly the same, though in Python 2.*, using while 1 is minutely faster, due to the ability to reassign True to a different value - the interpretor needs to look up the value of the variable, as opposed to loading a constant.

As a programmer, it is up to you which style to use - but always remember that readability is important, and that speed is also important.

WhileLoop (last edited 2012-09-29 17:53:26 by PaulBoddie)

Unable to edit the page? See the FrontPage for instructions.