Seite - 67 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Bild der Seite - 67 -
Text der Seite - 67 -
3.2 ThewhileLoop 67
<code line 2> # 2nd line in loop body
...
...
# This is the first line after the loop
Thefirst linehere is thewhile loopheader. Itcontains the reservedwordwhileand
ends with a colon, both are compulsory. The indented lines that follow the header
(i.e.,<code line 1>,<code line 2>, etc.) constitute a block of statements, the
loop body. Indentation is done as with for loops, i.e., 4 spaces by convention. In
ourexampleabovewith the ball, there was onlya single line in the loopbody(i.e.,
i = i + 1). Aswithfor loops,one run-throughof the loopbody is referred to as
an iteration. Once the indentation is reversed, the loop body has ended. Here, the
first lineafter the loop is# This is the first line after the loop.
Between while and the colon, there is some_condition, which is a boolean
expression that evaluates to eitherTrueorFalse. The boolean expression may be
acompoundexpressionwithand,or, etc.
When a while loop is encountered by the Python interpreter, it evaluates
some_condition the first time. If True, one iteration of the loop body is carried
out. After this first iteration, some_condition is evaluated once again (meaning
that program execution goes back up to the top of the loop). If True again, there
is another iteration, and so on, just like we saw above with ball_time.py. Once
some_conditionevaluates toFalse, the loop is finished and executioncontinues
with the first line after the loop. Note that ifsome_conditionevaluates toFalse
the very first time, the statements inside the loop will not be executed at all, and
executionsimplycontinues immediatelywith thefirst lineafter the loop.
Compared to afor loop, the programmer does not have to specify the number
of iterationswhencodingawhile loop. It simply runsuntil thebooleanexpression
becomesFalse. Remember that if you want to use a variable analogously to the
loop variable of a for loop, you have to explicitly update that variable inside the
while loop (as we did with i in ball_time.py above). This differs from the
automaticupdateofa loopvariable infor loops.
Justas infor loops, theremightbe(arbitrarily)manycodelinesinawhile loop.
Also,nested loopswork just likenestedfor loops.Havingfor loops insidewhile
loops,andviceversa, isstraightforward.Anyfor loopmayalsobeimplementedas
awhile loop,butwhile loopsaremoreflexible,sonotallof themcanbeexpressed
asafor loop.
Infinite Loops It is possible to have a while loop in which the condition never
evaluates to False, meaning that program execution can not escape the loop!
This is referred to as an infinite loop. Sometimes, infinite loops are just what you
need, for example, in surveillance camera systems. More often, however, they are
unintentional,andwhen learningtocode, it isquitecommontounintentionallyend
upwithaninfinite loop(justwaitandsee!). Ifyouaccidentallyenteraninfinite loop
and theprogramjust hangs“forever”,pressCtrl+c to stop the program.
To check that you have gained a basic understanding of the while loop
construction,youare recommendedtodoExercise3.4.
Programming for Computations – Python
A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
- Titel
- Programming for Computations – Python
- Untertitel
- A Gentle Introduction to Numerical Simulations with Python 3.6
- Band
- Second Edition
- Autoren
- Svein Linge
- Hans Petter Langtangen
- Verlag
- Springer Open
- Datum
- 2020
- Sprache
- englisch
- Lizenz
- CC BY 4.0
- ISBN
- 978-3-319-32428-9
- Abmessungen
- 17.8 x 25.4 cm
- Seiten
- 356
- Schlagwörter
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Kategorie
- Informatik