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