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