Original (English)
Comparisons
A comparison is a special type of expression that delivers either true (nonzero)
or false (zero) as a result. There are special comparison
operators for comparing variables or expressions:
== |
True if the expressions left and right of the operator are equal. |
!= |
True if the expressions left and right of the operator are not equal. |
> |
True if the expression left of the operator is greater than the expression right of the operator. |
>= |
True if the expression left of the operator is greater than or equal to the expression right of the operator. |
< |
True if the expression right of the operator is greater than the expression left of the operator. |
<= |
True if the expression right of the operator is greater than or equal to the expression left of the operator. |
and, && |
True if the expressions left and right of the operator are both true. |
or, || |
True if any one of the expressions left and right of the operator is true. |
not, ! |
True if the expression right of the operator is not true. |
() |
Brackets, for defining the priority of comparisons. Always use brackets when priority matters! |
Remarks:
- The "equals" comparison is done with '==', to differentiate it from the assignment instruction with '='. Wrongly
using '=' instead of '==' causes no error message from the compiler because it's a valid
assignment, but is a frequent bug in scripts.
- Comparing floating point variables
(var, double, float,
DATE)
with '==' returns normally false because they are almost never absolutely identical.
Only exception are simple cases such as comparison with 0.
Otherwise, use only >, >=, <,
<=, or the between function for comparing floating point variables
with each other.
- For comparing the content of structs or arrays, compare their elements.
Strings can be compared with each other
with the strstr or strcmp functions.
For case insensitive comparing strings with string constants, (e.g.,
if(Asset == "EUR/USD") ... ) the operators '==' and '!='
can be also used.
- The precedence of comparison and expression operators follows the C/C++ standard. Use parentheses in case of doubt. For instance, the expressions (x & y == z) and ((x & y) == z) give different results because the & operator has lower precedence than the == operator.
- Unlike C/C++, lite-C evaluates all parts in a &&
or || comparison, even if one of it evaluates to false. Therefore, avoid constructs like if (p && p->data == 1)..; use if (p) if (p->data == 1).. instead.
Examples:
10 < x // true if x is greater than 10
(10 <= x) and (15 => x) // true if x is between 10 and 15
!((10 <= x) and (15 => x)) // true if x is less than 10 or greater than 15 (lite-C only)
See also:
Functions, Variables, Pointers,
Expressions
► latest version online
|
Übersetzung (Deutsch)
Vergleiche
Ein Vergleich ist eine spezielle Art von Ausdruck, der entweder wahr (ungleich null)
oder falsch (null) als Ergebnis liefert. Es gibt spezielle Vergleichsoperatoren zum Vergleichen von Variablen oder Ausdrücken:
== |
Wahr, wenn die Ausdrücke links und rechts vom Operator gleich sind. |
!= |
Wahr, wenn die Ausdrücke links und rechts vom Operator nicht gleich sind. |
> |
Wahr, wenn der Ausdruck links vom Operator größer ist als der Ausdruck rechts vom Operator. |
>= |
Wahr, wenn der Ausdruck links vom Operator größer oder gleich dem Ausdruck rechts vom Operator ist. |
< |
Wahr, wenn der Ausdruck rechts vom Operator größer ist als der Ausdruck links vom Operator. |
<= |
Wahr, wenn der Ausdruck rechts vom Operator größer oder gleich dem Ausdruck links vom Operator ist. |
and, && |
Wahr, wenn die Ausdrücke links und rechts vom Operator beide wahr sind. |
or, || |
Wahr, wenn einer der Ausdrücke links oder rechts vom Operator wahr ist. |
not, ! |
Wahr, wenn der Ausdruck rechts vom Operator nicht wahr ist. |
() |
Klammern zur Definition der Priorität von Vergleichen. Verwende immer Klammern, wenn die Priorität wichtig ist! |
Bemerkungen:
- Der "Gleichheits"-Vergleich wird mit '==' durchgeführt, um ihn von der Zuweisungsanweisung mit '=' zu unterscheiden. Das falsche
Verwenden von '=' statt '==' verursacht keine Fehlermeldung vom Compiler, da es eine gültige
Zuweisung ist, aber ist ein häufiger Fehler in Skripten.
- Vergleich von Fließkommazahlen
(var, double, float,
DATE)
mit '==' ergibt normalerweise false, weil sie fast nie absolut identisch sind.
Nur Ausnahmen sind einfache Fälle wie der Vergleich mit 0.
Ansonsten verwende nur >, >=, <,
<= oder die between Funktion zum Vergleichen von Fließkommazahlen
miteinander.
- Zum Vergleichen des Inhalts von Strukturen oder Arrays, vergleiche deren Elemente.
Strings können mit anderen
mit den strstr oder strcmp Funktionen verglichen werden.
Für fallunabhängige Vergleiche von Strings mit Zeichenkettenkonstanten (z.B.,
if(Asset == "EUR/USD") ... ) können die Operatoren '==' und '!='
ebenfalls verwendet werden.
- Die Priorität der Vergleichs- und Ausdrucksoperatoren folgt dem C/C++ Standard. Verwende Klammern bei Zweifeln. Zum Beispiel ergeben die Ausdrücke (x & y == z) und ((x & y) == z) unterschiedliche Ergebnisse, weil der & Operator eine niedrigere Priorität hat als der == Operator.
- Im Gegensatz zu C/C++ wertet lite-C alle Teile in einer &&
oder || Vergleich aus, selbst wenn einer davon falsch ist. Vermeide daher Konstrukte wie if (p && p->data == 1)..;, verwende stattdessen if (p) if (p->data == 1)...
Beispiele:
10 < x // wahr, wenn x größer als 10 ist
(10 <= x) and (15 => x) // wahr, wenn x zwischen 10 und 15 liegt
!((10 <= x) and (15 => x)) // wahr, wenn x kleiner als 10 oder größer als 15 ist (nur lite-C)
Siehe auch:
Funktionen, Variablen, Pointer,
Ausdrücke
► latest version online
|