[translation] chapter 1 boolean expressions

This commit is contained in:
Jan Grewe 2016-10-12 10:51:00 +02:00
parent e08b178717
commit b08ff66959

View File

@ -777,122 +777,123 @@ ans =
16 17 25 16 17 25
\end{lstlisting} \end{lstlisting}
\section{Boolean Operations} \section{Boolean expressions}
Boolesche Ausdr\"ucke sind Anweisungen, die zu \codeterm{wahr} oder Boolean expressions are instructions that can be evaluated to
\codeterm{falsch} ausgewertet werden. Man kennt sie z.B. aus der \varcode{true} or \varcode{false}. In the context of programming they
Mengenlehre. In der Programmierung werden sie eingesetzt, um z.B. die are used to test the relations accordingly the programming language
Beziehung zwischen Entit\"aten zu testen. Hierzu werden die defines operators for such instructions. The following
\codeterm{relationalen Operatoren} (\code[Operator!relationaler!>]{>}, \codeterm{relational operators} are defined:
\code[Operator!relationaler!<]{<}, (\code[Operator!relational!>]{>}, \code[Operator!relational!<]{<},
\code[Operator!relationaler!==]{==}, \code[Operator!relational!==]{==}, \code[Operator!relational!"~]{~},
\code[Operator!relationaler!"!]{!}, gr\"o{\ss}er als, kleiner als, greater than, less than, equal to, and not. Via so called
gleich und nicht) eingesetzt. Mehrere Ausdr\"ucke werden mittels der \codeterm[Operator!logical]{logical operators} it is possible to join
\codeterm[Operator!logischer]{logischen Operatoren} single Boolean expressions (\code[Operator!logical!and1@\&]{\&},
(\code[Operator!logischer!and1@\&]{\&}, \code[Operator!logischer!or1@{"|} {}]{|}, \code[Operator!logical!or1@{"|} {}]{|}, AND, OR). These expressions
UND, ODER) verkn\"upft. Sie sind nicht nur wichtig, um are important to control which parts of the code should be evaluated
Codeabschnitte bedingt auszuf\"uhren (Verzweigungen, under a certain condition (conditional statements,
\ref{controlstructsec}) sondern auch um aus Vektoren und Matrizen Section~\ref{controlstructsec}) but also for accessing only certain
bequem Elemente auszuw\"ahlen (logisches Indizieren, elements of a vector or matrix (logical indexing,
\ref{logicalindexingsec}). Section~\ref{logicalindexingsec}).
Die Tabellen \ref{logicalandor} zeigen die Wahrheitstabellen f\"ur das The truth tables (\ref{logicalandor}) are used to visualize the
logische UND, das logische ODER und das logische XOR results of Boolean expressions. The statements A and B can be
(entweder-oder). Es werden die Aussagen A und B mit dem Operator evaluated to True or False. When they are combined with a logical AND
verkn\"upft. Beim logischen UND ist der gesamte Ausdruck nur dann the expression is true only if both statements are true. The logical
wahr, wenn beide Ausdr\"ucke sich zu wahr auswerten lassen. Anders OR, on the other hand, requires that at least one of the statements is
ist das beim logischen ODER. Hier ist der gesamte Ausdruck wahr, wenn true. The exclusive OR (XOR) is true only for cases in which one of
sich der eine \emph{oder} der andere Ausdruck, oder beide Ausdr\"ucke the statements but not both are true. There is no operator for XOR in
zu wahr auswerten lassen. Das auschlie{\ss}ende ODER (XOR) ist nur \matlab{} it is realized via the function \code[xor()]{xor(A,
wahr, wenn entweder der eine oder der andere Ausdruck wahr ist und ist B)}.
in \matlab{} als Funktion \code[xor()]{xor(A, B)} verf\"ugbar.
\begin{table}[tp] \begin{table}[tp]
\titlecaption{Wahrheitstabellen logisches UND, ODER und XOR.}{}\label{logicalandor} \titlecaption{Truth tables for logical AND, OR and XOR.}{}\label{logicalandor}
\begin{tabular}{llll} \begin{tabular}{llll}
\multicolumn{2}{l}{\multirow{2}{*}{}} & \multicolumn{2}{c}{\textbf{B}} \\ \multicolumn{2}{l}{\multirow{2}{*}{}} & \multicolumn{2}{c}{\textbf{B}} \\
& \sffamily{\textbf{und}} & \multicolumn{1}{|c}{wahr} & falsch \\ \cline{2-4} & \sffamily{\textbf{und}} & \multicolumn{1}{|c}{true} & false \\ \cline{2-4}
\multirow{2}{*}{\textbf{A}} & \multicolumn{1}{l|}{wahr} & \multicolumn{1}{c}{\textcolor{mygreen}{wahr}} & \textcolor{red}{falsch} \erb \\ \multirow{2}{*}{\textbf{A}} & \multicolumn{1}{l|}{true} & \multicolumn{1}{c}{\textcolor{mygreen}{true}} & \textcolor{red}{false} \erb \\
& \multicolumn{1}{l|}{falsch} & \multicolumn{1}{l}{\textcolor{red}{falsch}} & \textcolor{red}{falsch} & \multicolumn{1}{l|}{false} & \multicolumn{1}{l}{\textcolor{red}{false}} & \textcolor{red}{false}
\end{tabular} \end{tabular}
\hfill \hfill
\begin{tabular}{llll} \begin{tabular}{llll}
\multicolumn{2}{l}{\multirow{2}{*}{}} & \multicolumn{2}{c}{\textbf{B}} \\ \multicolumn{2}{l}{\multirow{2}{*}{}} & \multicolumn{2}{c}{\textbf{B}} \\
& \sffamily{\textbf{oder}} & \multicolumn{1}{|c}{wahr} & falsch \\ \cline{2-4} & \sffamily{\textbf{oder}} & \multicolumn{1}{|c}{true} & false \\ \cline{2-4}
\multirow{2}{*}{\textbf{A}} & \multicolumn{1}{l|}{wahr} & \multicolumn{1}{c}{\textcolor{mygreen}{wahr}} & \textcolor{mygreen}{wahr} \erb \\ \multirow{2}{*}{\textbf{A}} & \multicolumn{1}{l|}{true} & \multicolumn{1}{c}{\textcolor{mygreen}{true}} & \textcolor{mygreen}{true} \erb \\
& \multicolumn{1}{l|}{falsch} & \multicolumn{1}{l}{\textcolor{mygreen}{wahr}} & \textcolor{red}{falsch} & \multicolumn{1}{l|}{false} & \multicolumn{1}{l}{\textcolor{mygreen}{true}} & \textcolor{red}{false}
\end{tabular} \end{tabular}
\hfill \hfill
\begin{tabular}{llll} \begin{tabular}{llll}
\multicolumn{2}{l}{\multirow{2}{*}{}} & \multicolumn{2}{c}{\textbf{B}} \\ \multicolumn{2}{l}{\multirow{2}{*}{}} & \multicolumn{2}{c}{\textbf{B}} \\
& \sffamily{\textbf{xor}} & \multicolumn{1}{|c}{wahr} & falsch \\ \cline{2-4} & \sffamily{\textbf{xor}} & \multicolumn{1}{|c}{true} & false \\ \cline{2-4}
\multirow{2}{*}{\textbf{A}} & \multicolumn{1}{l|}{wahr} & \multicolumn{1}{c}{\textcolor{red}{falsch}} & \textcolor{mygreen}{wahr} \erb \\ \multirow{2}{*}{\textbf{A}} & \multicolumn{1}{l|}{true} & \multicolumn{1}{c}{\textcolor{red}{false}} & \textcolor{mygreen}{true} \erb \\
& \multicolumn{1}{l|}{falsch} & \multicolumn{1}{l}{\textcolor{mygreen}{wahr}} & \textcolor{red}{falsch} & \multicolumn{1}{l|}{false} & \multicolumn{1}{l}{\textcolor{mygreen}{true}} & \textcolor{red}{false}
\end{tabular} \end{tabular}
\end{table} \end{table}
Tabelle \ref{logicalrelationaloperators} zeigt die logischen Operatoren, die in
\matlab{} definiert sind. Zu bemerken sind hier noch die Table~\ref{logicalrelationaloperators} show the logical and relational
\code[Operator!logischer!and2@\&\&]{\&\&} und operators that are available in \matlab{}. The additional
\code[Operator!logischer!or2@{"|}{"|} {}]{||} Operatoren. Man kann \code[Operator!logical!and2@\&\&]{\&\&} und
beliebige Ausdr\"ucke verkn\"upfen und h\"aufig kann schon anhand des \code[Operator!logical!or2@{"|}{"|} {}]{||} operators are the so
ersten Ausdrucks entschieden werden, ob der gesamte boolesche Ausdruck called `\enterm{shorrt-circuit} operators for the logical OR and
zu wahr oder falsch ausgewertet werden wird. Wenn zwei Aussagen mit AND. Short-circuit means that \matlab{} stopps to evaluate a Boolean
einem UND verkn\"upft werden und der erste zu falsch ausgewertet wird, expresssion as soon as it becomes clear that the whole expression
muss der zweite gar nicht mehr gepr\"uft werden. Die Verwendung der cannot become true. For example assume that the two statements A and B
\enterm{short-circuit} Versionen spart Rechenzeit, da die Ausdr\"ucke are joined using a AND. The whole expression can only be true if A is
nur sowei wie n\"otig ausgewertet werden. already true. This means, that there is no need to evaluate B if A is
false. Since the statements may be arbitrarily elaborated computations
this saves processing time.
\begin{table}[t] \begin{table}[t]
\titlecaption{\label{logicalrelationaloperators} \titlecaption{\label{logicalrelationaloperators}
Logische (links) und relationale (rechts) Operatoren in \matlab.}{} Logical (left) and relational (right) operators in \matlab.}{}
\begin{tabular}{cc} \begin{tabular}{cc}
\hline \hline
\textbf{Operator} & \textbf{Beschreibung} \erh \\ \hline \textbf{operator} & \textbf{description} \erh \\ \hline
\varcode{$\sim$} & logisches NICHT \erb \\ \varcode{$\sim$} & logical NOT \erb \\
\varcode{$\&$} & logisches UND\\ \varcode{$\&$} & logical AND\\
\varcode{$|$} & logisches ODER\\ \varcode{$|$} & logical OR\\
\varcode{$\&\&$} & short-circuit logisches UND\\ \varcode{$\&\&$} & short-circuit logical AND\\
\varcode{$\|$} & short-circuit logisches ODER\\ \varcode{$\|$} & short-circuit logical OR\\
\hline \hline
\end{tabular} \end{tabular}
\hfill \hfill
\begin{tabular}{cc} \begin{tabular}{cc}
\hline \hline
\textbf{Operator} & \textbf{Beschreibung} \erh \\ \hline \textbf{operator} & \textbf{description} \erh \\ \hline
\varcode{$==$} & gleich \erb \\ \varcode{$==$} & equals \erb \\
\varcode{$\sim=$} & ungleich\\ \varcode{$\sim=$} & unequal\\
\varcode{$>$} & gr\"o{\ss}er als \\ \varcode{$>$} & greater than \\
\varcode{$<$} & kleiner als \\ \varcode{$<$} & less than \\
\varcode{$>=$} & gr\"o{\ss}er oder gleich \\ \varcode{$>=$} & greateror equal \\
\varcode{$<=$} & kleiner oder gleich \\ \varcode{$<=$} & less or equal \\
\hline \hline
\end{tabular} \end{tabular}
\end{table} \end{table}
Um Werte miteinander zu vergleichen gibt es die \begin{important}[Assignment and equality operators]
\codeterm[Operator!relationaler]{relationalen Operatoren} (Tabelle The assignment operator \code[Operator!Assignment!=]{=} and the
\ref{logicalrelationaloperators}). Mit ihnen kann man auf Dinge wie logical equality operator \code[Operator!logical!==]{==} are
Gleichheit (\varcode{==}) gr\"o{\ss}er oder kleiner als (\varcode{>}, fundamentally different. Since they are colloquially treated equal
\varcode{<}) testen. they can be easily confused.
\begin{important}[Zuweisungs- und Gleichheitsoperator]
Der Zuweisungsoperator \code[Operator!Zuweisung!=]{=} und der
logische Operator \code[Operator!logischer!==]{==} sind zwei
grundverschiedene Dinge. Da sie umgangsprachlich gleich sind
k\"onnen sie leider leicht verwechselt werden.
\end{important} \end{important}
Das Ergebnis eines booleschen Ausdrucks ist immer vom Datentyp Previously we have introduced the data types for integer or floating
\codeterm{logical}. Jede beliebige Variable zu wahr oder falsch point numbers and discussed that there are instances where it is more
ausgewertet werden indem diese in den Typ \code{logical} umgewandelt efficient to use a integer data type rather than storing floating
wird. Dabei werden von \matlab{} alle Werte, die nicht 0 sind als wahr poing numbers. The result of a Boolean expression can only assume two
eingesch\"atzt. Listing \ref{booleanexpressions} zeigt einige values (true or false). This implies that we need only a single bit to
Beispiele. \matlab{} kennt die Schl\"usselworte \code{true} und store this information as a 0 (false) and 1 (true). In \matlab{} knows
\code{false}. Diese sind Synonyme f\"ur die \code{logical} Werte 1 und a special data type (\codeterm{logical}) to store the result of a
0. Boolean expresssion. Every variable can be evaluated to true or false
by just onverting it to the logical data type. When doing so \matlab{}
\begin{lstlisting}[caption={Boolesche Ausdr\"ucke.}, label=booleanexpressions] interprets all values different form zero to be true. In
listing~\ref{booleanexpressions} we show several examples for such
operations. \matlab{} also knows the keywords \code{true} and
\code{false} which are synonyms for the \codeterm{logical} values 1
and 0.
\begin{lstlisting}[caption={Boolean expressions.}, label=booleanexpressions]
>> true >> true
ans = 1 ans = 1
>> false >> false