[translation] for loop

This commit is contained in:
Jan Grewe 2016-10-12 15:52:37 +02:00
parent 11240e18c0
commit 91564a7ef9

View File

@ -1004,27 +1004,24 @@ segment of data of a certain time span (the stimulus was on,
\end{exercise}
\section{Kontrollstrukturen}\label{controlstructsec}
\section{Control flow}\label{controlstructsec}
In der Regel wird ein Programm Zeile f\"ur Zeile von oben nach unten
ausgef\"uhrt. Manchmal muss der Kontrollfluss aber so gesteuert
werden, dass bestimmte Teile wiederholt oder nur
unter bestimmten Bedingungen ausgef\"uhrt werden sollen. Von gro{\ss}er
Bedeutung sind hier zwei Strukturen:
Generally a program is executed line by line from top to
bottom. Sometimes this behavior is not wanted, or the other way round,
it is needed to skip certain parts or execute others
repeatedly. High-level programming languages like \matlab{} offer
statements that allow to manipulate the control flow. There are two
major classes of such statements:
\begin{enumerate}
\item Schleifen.
\item Bedingte Anweisungen und Verzweigungen.
\item loops.
\item conditional expressions
\end{enumerate}
\subsection{Schleifen}
\subsection{Loops}
As the name already suggests loops are used to execute the same parts of the code repeatedly. In one of the earlier exercises the faculty of five has been calculated as depicted in listing~\ref{facultylisting}.
Schleifen werden gebraucht um wiederholte Ausf\"uhrung desselben Codes
zu vereinfachen. In einer \"Ubung wurde die Fakult\"at von 5 wie in
Listing \ref{facultylisting} berechnet:
\begin{lstlisting}[caption={Berechnung der Fakult\"at von 5 in f\"unf
Schritten}, label=facultylisting]
\begin{lstlisting}[caption={Calculation of the faculty of 5 in five steps}, label=facultylisting]
>> x = 1;
>> x = x * 2;
>> x = x * 3;
@ -1035,56 +1032,49 @@ x =
120
\end{lstlisting}
Im Prinzip ist das obige Programm v\"ollig in Ordnung. Es f\"allt
jedoch auf, dass die Zeilen 2 bis 5 sehr \"ahnlich sind; bis auf die
Multiplikation mit einer ansteigenden Zahl \"andert sich nichts. Die
Verwendung von mehr oder weniger exakten Wiederholungen einzelner
Zeilen oder ganzer Abschnitte ist schlechter Prgrammierstil. Dabei
geht es nicht nur um einen \"asthetischen Aspekt sondern vielmehr
darum, dass es schwerwiegende Nachteile gibt:
Basically this kind of program is fine but it is rather
repetitive. The only thing that changes is the increasing factor. The
repetition of such very similar lines of code is bad programming
style. This is not only a matter of esthetics but there are severe
drawbacks to this style:
\begin{enumerate}
\item Fehleranf\"alligkeit: Beim ``Copy-and-paste'' kann leicht
vergessen werden in einzelnen Wiederholungen die entscheidende
\"Anderung auch wirklich vorzunehmen.
\shortquote{Copy and paste is a design error.}{David Parnas}
\item Flexibilit\"at: Das obige Programm ist f\"ur genau einen Zweck,
Berechnung der Fakult\"at von f\"unf, gemacht und kann nichts
anderes.
\item Wartung: Wenn ein Fehler gemacht wurde, dann muss der Fehler in
allen Wiederholungen korrigiert werden (sehr leicht wird dabei etwas
\"ubersehen).
\item Verst\"andlichkeit: Solche Abschnitte sind schwerer zu lesen und
schwer zu verstehen. Das liegt zum Teil daran, dass man dazu neigt
\"uber sich wiederholende Zeilen zu springen (ist ja eh das
gleiche...) und dann den entscheidenden Teil verpasst. Zum Anderen
f\"uhrt Codeduplication zu langen, un\"ubersichtlichen Programmen.
\item Error-proneness: ``Copy-and-paste'' often leads to case that the
essential part of a repetition is not adapted. \shortquote{Copy and
paste is a design error.}{David Parnas}
\item Flexibility: The aforementioned program does exactly one thing,
it cannot be used for any other other purpose (such as the faculty
of 6).
\item Maintenance: If there is an error, it has to be fixed in all
repetitions. It is easy to forget a single change.
\item Readability: repetitive code is terrible to read and to
understand. In parts one tends to skip repetitions (its the same,
anyways) and misses the essential change. Further, the duplication
of code leads to long and hard to parse programs.
\end{enumerate}
Alle Programmiersprachen bieten zur L\"osung dieses Problems die
Schleifen. Eine Schleife wird immer dann eingesetzt, wenn
Abschnitte wiederholt ausgef\"uhrt werden sollen.
\subsubsection{Die \code{for} -- Schleife}
Der am h\"aufigsten benutzte Vertreter der Schleifen ist die
\codeterm{for-Schleife}. Sie besteht aus dem
\codeterm[Schleife!Schleifenkopf]{Schleifenkopf} und dem
\codeterm[Schleife!Schleifenk{\"o}rper]{Schleifenk\"orper}. Der Kopf
regelt, wie h\"aufig der Code im K\"orper ausgef\"uhrt wird. Der
Schleifenkopf beginnt mit dem Schl\"usselwort \code{for} auf welches
folgend die \codeterm{Laufvariable} definiert wird. In \matlab
``l\"auft''/iteriert eine for-Schleife immer(!) \"uber einen
Vektor. Die \codeterm{Laufvariable} nimmt mit jeder Iteration einen
Wert dieses Vektors an. Im Schleifenk\"orper k\"onnen beliebige
Anweisungen ausgef\"uhrt werden. Die Schleife wird durch das
Schl\"usselwort \code{end} beendet. Listing \ref{looplisting} zeigt
das Grundger\"ust einer for-Schleife.
\begin{lstlisting}[caption={Beispiel einer \varcode{for}-Schleife.}, label=looplisting]
>> for x = 1:3
x
All imperative programming languages offer a solution: the loop. It is
used whenever the same commands have to be repeated.
\subsubsection{The \code{for} --- loop}
The most common type of loop is the \codeterm{for-Schleife}. It
consists of a \codeterm[Loop!head]{head} and the
\codeterm[Loop!body]{body}. The head defines how often the code of the
body is executed. In \matlab{} the head begins with the keyword
\code{for} which is followed by the \codeterm{running variable}. In
\matlab{} a for-loop always operates on vectors. With each
\codeterm{iteration} of the loop, the running variable assumes the
next value of this vector. In the body of the loop any code can be
executed which may or may not use the running variable for a certain
purpose. The \code{for} loop is closed with the keyword
\code{end}. Listing~\ref{looplisting} shows a simple version of such a
\code{for} loop.
\begin{lstlisting}[caption={Example of a \varcode{for}-loop.}, label=looplisting]
>> for x = 1:3 % head
disp(x) % body
end
% die Laufvariable x nimmt mit jeder Iteration der Schleife
% einen Wert des Vektors 1:3 an:
% the running variable assumes with each iteration the next value
% of the vector 1:3:
1
2
3
@ -1092,13 +1082,12 @@ das Grundger\"ust einer for-Schleife.
\begin{exercise}{facultyLoop.m}{facultyLoop.out}
Wie k\"onnte Fakult\"at mit einer Schleife implementiert werden?
Implementiere eine \code{for} Schleife, die die Fakul\"at von einer
Zahl \varcode{n} berechnet.
Can we solve the faculty with a for-loop? Implement a for loop that
calculates the faculty of a number \varcode{n}.
\end{exercise}
\subsubsection{Die \varcode{while} -- Schleife}
\subsubsection{The \varcode{while} --- loop}
Eine weiterer Schleifentyp, der weniger h\"aufig eingesetzt wird, ist
die \code{while}-Schleife. Auch sie hat ihre Entsprechungen in fast