english translation started
This commit is contained in:
@@ -9,67 +9,67 @@ engine: julia
|
||||
using InteractiveUtils
|
||||
```
|
||||
|
||||
# Grundlagen der Syntax
|
||||
# Fundamentals of Syntax
|
||||
|
||||
## Namen von Variablen, Funktionen, Typen etc.
|
||||
## Names of Variables, Functions, Types, etc.
|
||||
|
||||
- Namen können Buchstaben, Ziffern, den Unterstrich `_` und das Ausrufezeichen `!` enthalten.
|
||||
- Das erste Zeichen muss ein Buchstabe oder ein Unterstrich sein.
|
||||
- Groß- und Kleinbuchstaben werden unterschieden: `Nmax` und `NMAX` sind verschiedene Variablen.
|
||||
- Als Zeichensatz wird [Unicode](https://home.unicode.org/) verwendet. Damit stehen über 150 Schriften und zahlreiche Symbole zur Verfügung.
|
||||
- Es gibt eine kurze [Liste reservierter Schlüsselwörter](https://docs.julialang.org/en/v1/base/base/#Keywords): `if, then, function, true, false,...`
|
||||
- Names may contain letters, digits, underscores `_`, and exclamation marks `!`.
|
||||
- The first character must be a letter or an underscore.
|
||||
- Case is significant: `Nmax` and `NMAX` are different variables.
|
||||
- The character set used is [Unicode](https://home.unicode.org/). This provides access to over 150 scripts and numerous symbols.
|
||||
- There is a short [list of reserved keywords](https://docs.julialang.org/en/v1/base/base/#Keywords): `if, then, function, true, false,...`
|
||||
|
||||
:::{.callout-tip}
|
||||
## Beispiel
|
||||
## Example
|
||||
|
||||
zulässig: `i, x, Ω, x2, DieUnbekannteZahl, neuer_Wert, 🎷, Zähler, лічильник, einself!!!!,...`
|
||||
permissible: `i, x, Ω, x2, DieUnbekannteZahl, neuer_Wert, 🎷, Zähler, лічильник, einself!!!!,...`
|
||||
|
||||
unzulässig: `Uwe's_Funktion, 3achsen, A#B, $this_is_not_Perl, true,...`
|
||||
impermissible: `Uwe's_Funktion, 3achsen, A#B, $this_is_not_Perl, true,...`
|
||||
:::
|
||||
|
||||
----
|
||||
|
||||
:::{.callout-note }
|
||||
## Anmerkung
|
||||
## Note
|
||||
|
||||
Neben den *reserved keywords* der Kernsprache sind zahlreiche weitere Funktionen und Objekte vordefiniert, wie z.B. die mathematischen Funktionen `sqrt(), log(), sin()`.
|
||||
Diese Definitionen finden sich in dem Modul `Base`, welches Julia beim Start automatisch lädt.
|
||||
Namen aus `Base` können umdefiniert werden, solange sie noch nicht verwendet wurden:
|
||||
In addition to the *reserved keywords* of the core language, numerous additional functions and objects are predefined, such as the mathematical functions `sqrt(), log(), sin()`.
|
||||
These definitions are found in the module `Base`, which Julia loads automatically at startup.
|
||||
Names from `Base` can be redefined as long as they have not yet been used:
|
||||
|
||||
```{julia}
|
||||
#| error: true
|
||||
log = 3
|
||||
1 + log
|
||||
```
|
||||
Jetzt ist natürlich der Logarithmus kaputt:
|
||||
Now of course the logarithm is broken:
|
||||
```{julia}
|
||||
#| error: true
|
||||
x = log(10)
|
||||
```
|
||||
(siehe auch <https://stackoverflow.com/questions/65902105/how-to-reset-any-function-in-julia-to-its-original-state>)
|
||||
(see also <https://stackoverflow.com/questions/65902105/how-to-reset-any-function-in-julia-to-its-original-state>)
|
||||
:::
|
||||
|
||||
## Anweisungen
|
||||
## Statements
|
||||
|
||||
- Im Normalfall enthält eine Zeile eine Anweisung.
|
||||
- Wenn eine Anweisung am Zeilenende als unvollständig erkennbar ist durch
|
||||
- offene Klammern
|
||||
- Operationszeichen,
|
||||
- In normal circumstances, each line contains one statement.
|
||||
- If a statement is recognizable as incomplete at the end of a line through
|
||||
- open parentheses
|
||||
- operators,
|
||||
|
||||
dann wird die nächste Zeile als Fortsetzung aufgefasst.
|
||||
- Mehrere Anweisungen pro Zeile können durch Semikolon getrennt werden.
|
||||
- Im interaktiven Betrieb (REPL oder Notebook) unterdrückt ein Semikolon nach der letzten Anweisung die Ausgabe des Ergebnisses dieser Anweisung.
|
||||
then the next line is regarded as a continuation.
|
||||
- Multiple statements per line can be separated by semicolons.
|
||||
- In interactive mode (REPL or notebook), a semicolon after the last statement suppresses the output of the result of that statement.
|
||||
|
||||
:::{.callout-tip}
|
||||
|
||||
## Beispiel
|
||||
## Example
|
||||
|
||||
Im interaktiven Betrieb wird der Wert der letzten Anweisung auch ohne explizites `print()` ausgegeben:
|
||||
In interactive mode, the value of the last statement is also displayed without explicit `print()`:
|
||||
```{julia}
|
||||
println("Hallo 🌍!")
|
||||
x = sum([i^2 for i=1:10])
|
||||
```
|
||||
Das Semikolon unterdrückt das:
|
||||
The semicolon suppresses this:
|
||||
```{julia}
|
||||
println("Hallo 🌍!")
|
||||
x = sum([i^2 for i=1:10]);
|
||||
@@ -81,14 +81,14 @@ x = sum([i^2 for i=1:10]);
|
||||
|
||||
:::{.callout-warning }
|
||||
|
||||
Bei mehrzeiligen Anweisungen muss die fortzusetzende Zeile mit einer offenen Operation oder Klammer enden:
|
||||
For multi-line statements, the line to be continued must end with an open operation or parenthesis:
|
||||
|
||||
```{julia}
|
||||
x = sin(π/2) +
|
||||
3 * cos(0)
|
||||
```
|
||||
|
||||
Also geht das Folgende schief, aber leider **ohne eine Fehlermeldung**!
|
||||
Therefore, the following goes wrong, but unfortunately **without an error message**!
|
||||
```{julia}
|
||||
#| error: true
|
||||
#| warning: true
|
||||
@@ -96,9 +96,9 @@ x = sin(π/2)
|
||||
+ 3 * cos(0)
|
||||
println(x)
|
||||
```
|
||||
Hier wird das `+` in der zweiten Zeile als Präfix-Operator (Vorzeichen) interpretiert. Damit sind 1. und 2. Zeile jeweils für sich vollständige, korrekte Ausdrücke (auch wenn die 2. Zeile natürlich völlig nutzlos ist) und werden auch so abgearbeitet.
|
||||
Here, the `+` in the second line is interpreted as a prefix operator (sign). Thus, lines 1 and 2 are each complete, correct expressions on their own (even though line 2 is of course completely useless) and are processed as such.
|
||||
|
||||
Moral: Wenn man längere Ausdrücke auf mehrere Zeilen aufteilen will, sollte man immer eine Klammer aufmachen. Dann ist egal, wo der Zeilenumbruch ist:
|
||||
Moral: If you want to split longer expressions across multiple lines, you should always open a parenthesis. Then it doesn't matter where the line break occurs:
|
||||
```{julia}
|
||||
x = ( sin(π/2)
|
||||
+ 3 * cos(0) )
|
||||
@@ -107,26 +107,26 @@ println(x)
|
||||
:::
|
||||
|
||||
|
||||
## Kommentare
|
||||
## Comments
|
||||
|
||||
Julia kennt 2 Arten von Kommentaren im Programmtext:
|
||||
Julia knows two types of comments in program text:
|
||||
|
||||
```{julia}
|
||||
# Einzeilige Kommentare beginnen mit einem Doppelkreuz.
|
||||
# Single-line comments begin with a hash symbol.
|
||||
|
||||
x = 2 # alles vom '#' bis zum Zeilenende ist ein Kommentar und wird ignoriert. x = 3
|
||||
x = 2 # everything from '#' to the end of the line is a comment and is ignored. x = 3
|
||||
```
|
||||
|
||||
```{julia}
|
||||
#=
|
||||
Ein- und mehrzeilige Kommentare können zwischen #= ... =# eingeschlossen werden.
|
||||
Dabei sind verschachtelte Kommentare möglich.
|
||||
Single and multi-line comments can be enclosed between #= ... =#.
|
||||
Nested comments are possible.
|
||||
#=
|
||||
d.h., anders als in C/C++/Java endet der Kommentar nicht mit dem ersten
|
||||
Kommentar-Endezeichen, sondern die #=...=# - Paare wirken wie Klammern.
|
||||
i.e., unlike in C/C++/Java, the comment does not end with the first
|
||||
comment-end character, but the #=...=# pairs act like parentheses.
|
||||
=#
|
||||
Der automatische 'syntax highlighter' weiss das leider noch nicht, wie die wechselnde
|
||||
Graufärbung dieses Kommentars zeigt.
|
||||
The automatic 'syntax highlighter' does not yet know this, as the alternating
|
||||
gray shading of this comment shows.
|
||||
=#
|
||||
|
||||
|
||||
@@ -134,16 +134,16 @@ x #= das ist ein seltener Variablenname! =# = 3
|
||||
```
|
||||
|
||||
|
||||
## Datentypen Teil I
|
||||
## Data Types Part I
|
||||
|
||||
- Julia ist eine [stark typisierte](https://de.wikipedia.org/wiki/Starke_Typisierung) Sprache. Alle Objekte haben einen Typ. Funktionen/Operationen erwarten Argumente mit dem richtigen Typ.
|
||||
- Julia ist eine [dynamisch typisierte](https://de.wikipedia.org/wiki/Dynamische_Typisierung) Sprache. Variablen haben keinen Typ. Sie sind Namen, die durch Zuweisung `x = ...` an Objekte gebunden werden können.
|
||||
- Wenn man vom „Typ einer Variablen“ spricht, meint man den Typ des Objektes, das der Variablen gerade zugewiesen ist.
|
||||
- Funktionen/Operatoren können verschiedene *methods* für verschiedene Argumenttypen implementieren.
|
||||
- Abhängig von den konkreten Argumenttypen wird dann bei Verwendung einer Funktion entschieden, welche Methode benutzt wird ([*dynamic dispatch*](https://en.wikipedia.org/wiki/Dynamic_dispatch)).
|
||||
- Julia is a [strongly typed](https://de.wikipedia.org/wiki/Starke_Typisierung) language. All objects have a type. Functions/operations expect arguments of the correct type.
|
||||
- Julia is a [dynamically typed](https://de.wikipedia.org/wiki/Dynamische_Typisierung) language. Variables have no type. They are names that can be bound to objects via assignment `x = ...`.
|
||||
- When speaking of the "type of a variable", one means the type of the object currently assigned to the variable.
|
||||
- Functions/operators can implement different *methods* for different argument types.
|
||||
- Depending on the concrete argument types, it is decided at function usage which method is used ([*dynamic dispatch*](https://en.wikipedia.org/wiki/Dynamic_dispatch)).
|
||||
|
||||
|
||||
Einfache Basistypen sind z.B.:
|
||||
Simple basic types are, for example:
|
||||
|
||||
```
|
||||
Int64, Float64, String, Char, Bool
|
||||
@@ -176,58 +176,58 @@ x = 3 > π
|
||||
x, typeof(x), sizeof(x)
|
||||
```
|
||||
|
||||
- `sizeof()` liefert die Größe eines Objekts oder Typs in Bytes (1 Byte = 8 Bit)
|
||||
- 64bit Ganzzahlen und 64bit Gleitkommazahlen entsprechen dem Befehlssatz moderner Prozessoren und sind daher die numerischen Standardtypen.
|
||||
- Zeichen/*chars* `'A'` und Zeichenketten/*strings* `"A"` der Länge 1 sind verschiedene Objekte.
|
||||
- `sizeof()` returns the size of an object or type in bytes (1 byte = 8 bits)
|
||||
- 64-bit integers and 64-bit floating-point numbers correspond to the instruction set of modern processors and are therefore the standard numeric types.
|
||||
- Characters/*chars* `'A'` and strings/*strings* `"A"` of length 1 are different objects.
|
||||
|
||||
## Ablaufsteuerung
|
||||
## Control Flow
|
||||
|
||||
### `if`-Blöcke
|
||||
### `if` Blocks
|
||||
|
||||
- Ein `if`-Block *kann* **beliebig viele** `elseif`-Zweige und als letztes maximal **einen** `else`-Zweig enthalten.
|
||||
- Der Block hat einen Wert, den Wert der letzten ausgeführten Anweisung.
|
||||
- An `if` block *can* contain **any number** of `elseif` branches and at the end at most **one** `else` branch.
|
||||
- The block has a value, the value of the last executed statement.
|
||||
|
||||
```{julia}
|
||||
x = 33
|
||||
y = 44
|
||||
z = 34
|
||||
|
||||
if x < y && z != x # elseif- und else-Blöcke sind optional
|
||||
if x < y && z != x # elseif- and else-branches are optional
|
||||
println("yes")
|
||||
x += 10
|
||||
elseif x < z # beliebig viele elseif-Blöcke
|
||||
elseif x < z # any number of elseif branches
|
||||
println(" x is smaller than z")
|
||||
elseif x == z+1
|
||||
println(" x is successor of z")
|
||||
else # maximal ein else-Block
|
||||
else # at most one else block
|
||||
println("Alles falsch")
|
||||
end # Wert des gesamten Blocks ist der Wert der
|
||||
# letzten ausgeführten Auswertung
|
||||
end # value of the entire block is the value of the
|
||||
# last evaluated statement
|
||||
```
|
||||
|
||||
Kurze Blöcke kann man in eine Zeile schreiben:
|
||||
Short blocks can be written in one line:
|
||||
```{julia}
|
||||
if x > 10 println("x is larger than 10") end
|
||||
```
|
||||
|
||||
Der Wert eines `if`-Blocks kann natürlich zugewiesen werden:
|
||||
The value of an `if` block can of course be assigned:
|
||||
```{julia}
|
||||
y = 33
|
||||
z = if y > 10
|
||||
println("y is larger than 10")
|
||||
y += 1
|
||||
end
|
||||
println("y is larger than 10")
|
||||
y += 1
|
||||
end
|
||||
z
|
||||
```
|
||||
|
||||
### Auswahloperator (ternary operator) `test ? exp1 : exp2`
|
||||
### Conditional Operator (ternary operator) `test ? exp1 : exp2`
|
||||
|
||||
```{julia}
|
||||
x = 20
|
||||
y = 15
|
||||
z = x < y ? x+1 : y+1
|
||||
```
|
||||
ist äquivalent zu
|
||||
is equivalent to
|
||||
|
||||
```{julia}
|
||||
z = if x < y
|
||||
@@ -237,9 +237,9 @@ z = if x < y
|
||||
end
|
||||
```
|
||||
|
||||
## Vergleiche, Tests, Logische Operationen
|
||||
## Comparisons, Tests, Logical Operations
|
||||
|
||||
### Arithmetische Vergleiche
|
||||
### Arithmetic Comparisons
|
||||
|
||||
- `==`
|
||||
- `!=`, `≠`
|
||||
@@ -248,56 +248,56 @@ z = if x < y
|
||||
- `<`
|
||||
- `<=`, `≤`
|
||||
|
||||
Wie üblich, ist der Test auf Gleichheit `==` vom Zuweisungsoperator `=` zu unterscheiden. Vergleichen lässt sich so gut wie alles:
|
||||
As usual, the equality test `==` is to be distinguished from the assignment operator `=`. Almost anything can be compared:
|
||||
|
||||
```{julia}
|
||||
"Aachen" < "Leipzig", 10 ≤ 10.01, [3,4,5] < [3,6,2]
|
||||
```
|
||||
|
||||
Nun ja, fast alles:
|
||||
Well, almost anything:
|
||||
|
||||
```{julia}
|
||||
3 < "vier"
|
||||
```
|
||||
|
||||
Die Fehlermeldung zeigt ein paar Grundprinzipien von Julia:
|
||||
The error message shows a few fundamental principles of Julia:
|
||||
|
||||
- Operatoren sind auch nur Funktionen: `x < y` wird zum Funktionsaufruf `isless(x, y)`.
|
||||
- Funktionen (und damit Operatoren) können verschiedene *methods* für verschiedene Argumenttypen implementieren.
|
||||
- Abhängig von den konkreten Argumenttypen wird beim Aufruf der Funktion entschieden, welche Methode benutzt wird ([*dynamic dispatch*](https://en.wikipedia.org/wiki/Dynamic_dispatch)).
|
||||
- Operators are also just functions: `x < y` becomes the function call `isless(x, y)`.
|
||||
- Functions (and thus operators) can implement different *methods* for different argument types.
|
||||
- Depending on the concrete argument types, it is decided at function call which method is used ([*dynamic dispatch*](https://en.wikipedia.org/wiki/Dynamic_dispatch)).
|
||||
|
||||
Man kann sich alle Methoden zu einer Funktion anzeigen lassen. Das gibt einen Einblick in das komplexe Typssystem von Julia:
|
||||
One can display all methods for a function. This provides insight into Julia's complex type system:
|
||||
|
||||
```{julia}
|
||||
methods(<)
|
||||
```
|
||||
|
||||
Zuletzt noch: Vergleiche können gekettet werden.
|
||||
Finally: comparisons can be chained.
|
||||
|
||||
```{julia}
|
||||
10 < x ≤ 100 # das ist äquivalent zu
|
||||
10 < x ≤ 100 # this is equivalent to
|
||||
# 10 < x && x ≤ 100
|
||||
|
||||
```
|
||||
|
||||
|
||||
### Tests
|
||||
Einge Funktionen vom Typ `f(c::Char) -> Bool`
|
||||
Some functions of type `f(c::Char) -> Bool`
|
||||
|
||||
```{julia}
|
||||
isnumeric('a'), isnumeric('7'), isletter('a')
|
||||
```
|
||||
|
||||
|
||||
und vom Typ `f(s1::String, s2::String) -> Bool`
|
||||
and of type `f(s1::String, s2::String) -> Bool`
|
||||
|
||||
```{julia}
|
||||
contains("Lampenschirm", "pensch"), startswith("Lampenschirm", "Lamb"), endswith("Lampenschirm", "rm")
|
||||
```
|
||||
|
||||
- Die Funktion `in(item, collection) -> Bool` testet, ob `item` in `collection` ist.
|
||||
- Sie hat auch das Alias ` ∈(item, collection)` und
|
||||
- sowohl `in` als auch `∈` können auch als Infix-Operatoren geschrieben werden.
|
||||
- The function `in(item, collection) -> Bool` tests whether `item` is in `collection`.
|
||||
- It also has the alias ` ∈(item, collection)` and
|
||||
- both `in` and `∈` can also be written as infix operators.
|
||||
|
||||
```{julia}
|
||||
x = 3
|
||||
@@ -307,23 +307,24 @@ x in [1, 2, 3, 4, 5]
|
||||
x ∈ [1, 2, 33, 4, 5]
|
||||
```
|
||||
|
||||
### Logische Operationen: `&&`, `||`, `!`
|
||||
### Logical Operations: `&&`, `||`, `!`
|
||||
|
||||
|
||||
```{julia}
|
||||
3 < 4 && !(2 > 8) && !contains("aaa", "b")
|
||||
```
|
||||
|
||||
#### Bedingte Auswertung (_short circuit evaluation_)
|
||||
#### Conditional Evaluation (_short circuit evaluation_)
|
||||
|
||||
- in `a && b` wird `b` nur ausgewertet, wenn `a == true`
|
||||
- in `a || b` wird `b` nur ausgewertet, wenn `a == false`
|
||||
- in `a && b` `b` is only evaluated if `a == true`
|
||||
- in `a || b` `b` is only evaluated if `a == false`
|
||||
|
||||
(i) Damit kann `if test statement end` auch als `test && statement` geschrieben werden.
|
||||
(i) Thus, `if test statement end` can also be written as `test && statement`.
|
||||
|
||||
(ii) Damit kann `if !test statement end` als `test || statement` geschrieben werden.
|
||||
(ii) Thus, `if !test statement end` can be written as `test || statement`.
|
||||
|
||||
As an example^[from the [Julia documentation](https://docs.julialang.org/en/v1/manual/control-flow/#Short-Circuit-Evaluation)] here is an implementation of the factorial function:
|
||||
|
||||
Als Beispiel^[aus der [Julia-Dokumentation](https://docs.julialang.org/en/v1/manual/control-flow/#Short-Circuit-Evaluation)] hier eine Implementierung der Fakultätsfunktion *(factorial)*:
|
||||
```{julia}
|
||||
function fact(n::Int)
|
||||
n >= 0 || error("n must be non-negative")
|
||||
@@ -336,23 +337,23 @@ fact(5)
|
||||
|
||||
|
||||
|
||||
Natürlich kann man alle diese Tests auch Variablen vom Typ `Bool` zuordnen und
|
||||
diese Variablen können als Tests in `if`- und `while`-Blöcken verwendet werden:
|
||||
Of course, all these tests can also be assigned to variables of type `Bool` and
|
||||
these variables can be used as tests in `if` and `while` blocks:
|
||||
|
||||
```{julia}
|
||||
x = 3 < 4
|
||||
y = 5 ∈ [1, 2, 5, 7]
|
||||
z = x && y
|
||||
if z # äquivalent zu: if 3 < 4 && 5 in [1,2,5,7]
|
||||
if z # equivalent to: if 3 < 4 && 5 in [1,2,5,7]
|
||||
println("Stimmt alles!")
|
||||
end
|
||||
```
|
||||
|
||||
- In Julia müssen alle Tests in einem logischen Ausdruck vom Typ `Bool` sein.
|
||||
- Es gibt keine implizite Konvertierung à la *"0 is false and 1 (or anything != 0) is true"*
|
||||
- Wenn `x` ein numerischer Typ ist, dann muss daher das C-Idiom `if(x)` als `if x != 0` geschrieben werden.
|
||||
- Es gibt eine Ausnahme zur Unterstützung der _short circuit evaluation_:
|
||||
- bei den Konstrukten `a && b && c...` bzw `a || b || c...` muss der letzte Teilausdruck nicht vom Typ `Bool` sein, wenn diese Konstrukte nicht als Tests in `if` oder `while` verwendet werden:
|
||||
- In Julia, all tests in a logical expression must be of type `Bool`.
|
||||
- There is no implicit conversion such as *"0 is false and 1 (or anything != 0) is true"*
|
||||
- If `x` is a numeric type, then the C idiom `if(x)` must be written as `if x != 0`.
|
||||
- There is an exception to support the _short circuit evaluation_:
|
||||
- in the constructs `a && b && c...` or `a || b || c...` the last subexpression does not need to be of type `Bool` if these constructs are not used as tests in `if` or `while`:
|
||||
|
||||
|
||||
```{julia}
|
||||
@@ -365,9 +366,9 @@ z = 3 < 4 && 10 < 50 && sqrt(3^3)
|
||||
z, typeof(z)
|
||||
```
|
||||
|
||||
## Schleifen *(loops)*
|
||||
## Loops *(loops)*
|
||||
|
||||
### Die `while` ("solange")-Schleife
|
||||
### The `while` ("while") loop
|
||||
|
||||
|
||||
Syntax:
|
||||
@@ -376,19 +377,19 @@ while *condition*
|
||||
*loop body*
|
||||
end
|
||||
```
|
||||
Eine Reihe von Anweisungen (der Schleifenkörper) wird immer wieder abgearbeitet, solange eine Bedingung erfüllt ist.
|
||||
A series of statements (the loop body) is repeatedly executed as long as a condition is satisfied.
|
||||
|
||||
|
||||
```{julia}
|
||||
i = 1 # typischerweise braucht der Test der
|
||||
# while-Schleife eine Vorbereitung ...
|
||||
i = 1 # typically the test of the
|
||||
# while loop needs preparation ...
|
||||
while i < 10
|
||||
println(i)
|
||||
i += 2 # ... und ein update
|
||||
i += 2 # ... and an update
|
||||
end
|
||||
```
|
||||
|
||||
Der Körper einer `while`- und `for`-Schleife kann die Anweisungen `break` und `continue` enthalten. `break` stoppt die Schleife, `continue` überspringt den Rest des Schleifenkörpers und beginnt sofort mit dem nächsten Schleifendurchlauf.
|
||||
The body of a `while` and `for` loop can contain the statements `break` and `continue`. `break` stops the loop, `continue` skips the rest of the loop body and immediately starts the next loop iteration.
|
||||
```{julia}
|
||||
i = 0
|
||||
|
||||
@@ -396,20 +397,20 @@ while i<10
|
||||
i += 1
|
||||
|
||||
if i == 3
|
||||
continue # beginne sofort nächsten Durchlauf,
|
||||
end # überspringe Rest des Schleifenkörpers
|
||||
continue # start next iteration immediately,
|
||||
end # skip rest of loop body
|
||||
|
||||
println("i = $i")
|
||||
|
||||
if i ≥ 5
|
||||
break # breche Schleife ab
|
||||
break # break loop
|
||||
end
|
||||
end
|
||||
|
||||
println("Fertig!")
|
||||
```
|
||||
|
||||
Mit `break` kann man auch Endlosschleifen verlassen:
|
||||
With `break` one can also exit infinite loops:
|
||||
|
||||
```{julia}
|
||||
i = 1
|
||||
@@ -421,7 +422,7 @@ while true
|
||||
end
|
||||
```
|
||||
|
||||
### `for`-Schleifen
|
||||
### `for` Loops
|
||||
|
||||
Syntax:
|
||||
|
||||
@@ -431,9 +432,9 @@ for *var* in *iterable container*
|
||||
end
|
||||
```
|
||||
|
||||
Der Schleifenkörper wird für alle Items aus einem Container durchlaufen.
|
||||
The loop body is executed for all items from a container.
|
||||
|
||||
Statt `in` kann immer auch $\in$ verwendet werden. Im Kopf einer `for`-Schleife kann auch `=` verwendet werden.
|
||||
Instead of `in`, $\in$ can always be used. In the header of a `for` loop, `=` can also be used.
|
||||
|
||||
```{julia}
|
||||
for i ∈ ["Mutter", "Vater", "Tochter"]
|
||||
@@ -442,8 +443,8 @@ end
|
||||
```
|
||||
|
||||
|
||||
Oft benötigt man einen numerischen Schleifenzähler. Dafür gibt es das *range*-Konstrukt. Die einfachsten Formen sind
|
||||
`Start:Ende` und `Start:Schrittweite:Ende`.
|
||||
Often a numerical loop counter is needed. For this purpose, there is the *range* construct. The simplest forms are
|
||||
`Start:End` and `Start:Step:End`.
|
||||
|
||||
```{julia}
|
||||
endwert = 5
|
||||
@@ -469,9 +470,9 @@ for k = 14 : -2.5 : 1 print(" $k") end
|
||||
|
||||
|
||||
|
||||
#### Geschachtelte Schleifen _(nested loops)_
|
||||
#### Nested Loops _(nested loops)_
|
||||
|
||||
Ein `break` beendet die innerste Schleife.
|
||||
A `break` ends the innermost loop.
|
||||
|
||||
```{julia}
|
||||
for i = 1:3
|
||||
@@ -484,22 +485,22 @@ for i = 1:3
|
||||
end
|
||||
```
|
||||
|
||||
Man kann *nested loops* auch in einer `for`-Anweisung zusammenfassen. Dann beendet ein `break` die Gesamtschleife.
|
||||
*Nested loops* can also be combined in a single `for` statement. Then a `break` ends the entire loop.
|
||||
|
||||
```{julia}
|
||||
for i = 1:3, j=1:3 # im Prinzip dasselbe wie oben, aber:
|
||||
for i = 1:3, j=1:3 # essentially the same as above, but:
|
||||
println( (i,j) )
|
||||
if j == 2
|
||||
break # break bricht hier die Gesamtschleife ab
|
||||
break # break ends the entire loop here
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
|
||||
:::{.callout-important .titlenormalxx}
|
||||
## **Wichtig:** Die Semantik ist völlig anders, als bei C-artigen `for`-Schleifen!
|
||||
## **Important:** The semantics are completely different from C-style `for` loops!
|
||||
|
||||
**Bei jedem Schleifendurchlauf wird die Laufvariable neu mit dem nächsten Element aus dem Container initialisiert.**
|
||||
**In each loop iteration, the loop variable is re-initialized with the next element from the container.**
|
||||
|
||||
|
||||
```{julia}
|
||||
@@ -512,11 +513,11 @@ end
|
||||
|
||||
-------
|
||||
|
||||
Die C-Semantik von `for(i=1; i<5; i++)` entspricht der `while`-Schleife:
|
||||
The C semantics of `for(i=1; i<5; i++)` corresponds to the `while` loop:
|
||||
```
|
||||
i = 1
|
||||
while i<5
|
||||
*loop body* # hier kann auch wirksam an i rumgepfuscht werden
|
||||
*loop body* # here one can also mess with i effectively
|
||||
i += 1
|
||||
end
|
||||
```
|
||||
@@ -524,68 +525,68 @@ end
|
||||
|
||||
## Unicode
|
||||
|
||||
Julia verwendet Unicode als Zeichensatz. Damit können für Variablen, Funktionen etc auch Bezeichner aus nicht-lateinischen Schriften (zB Kyrillisch, Koreanisch, Sanskrit, Runen,
|
||||
Emoji,...) verwendet werden. Die Frage, wie man solche Zeichen in seinem Editor eingeben kann und ob der verwendete Bildschirm-Font sie darstellen kann, ist nicht Julias Problem.
|
||||
Julia uses Unicode as its character set. This allows identifiers from non-Latin scripts (e.g., Cyrillic, Korean, Sanskrit, runes,
|
||||
emojis,...) to be used for variables, functions, etc. The question of how one can enter such characters in their editor and whether the used screen font can display them is not Julia's problem.
|
||||
|
||||
- Einige Unicode-Zeichen, z.B. `≤, ≠, ≥, π, ∈, √`, können anstelle von `<=, !=, >=, pi, in, sqrt` verwendet werden.
|
||||
- Some Unicode characters, e.g., `≤, ≠, ≥, π, ∈, √`, can be used instead of `<=, !=, >=, pi, in, sqrt`.
|
||||
|
||||
- über 3000 Unicode-Zeichen können in Julia in einer LaTeX-ähnlichen Weise mit der Tab-Vervollständigung eingegeben werden.
|
||||
- `\alpha<TAB>` wird zu `α`,
|
||||
- `\euler<TAB>` wird zu `ℯ` (Eulersche Zahl `exp(1)`, [spezielles Schreibschrift-e, `U+0212F`](https://www.htmlsymbol.com/unicode-code/212f.html))
|
||||
- `\le<TAB>` wird zu `≤`,
|
||||
- `\in<TAB>` wird zu `∈`,
|
||||
- `\:rainbow:<TAB>` wird zu `🌈`
|
||||
[Hier geht es zur Liste.](https://docs.julialang.org/en/v1/manual/unicode-input/)
|
||||
- Over 3000 Unicode characters can be entered in Julia in a LaTeX-like manner using tab completion.
|
||||
- `\alpha<TAB>` becomes `α`,
|
||||
- `\euler<TAB>` becomes `ℯ` (Euler's number `exp(1)`, [special script e, `U+0212F`](https://www.htmlsymbol.com/unicode-code/212f.html))
|
||||
- `\le<TAB>` becomes `≤`,
|
||||
- `\in<TAB>` becomes `∈`,
|
||||
- `\:rainbow:<TAB>` becomes `🌈`
|
||||
[Here is the list.](https://docs.julialang.org/en/v1/manual/unicode-input/)
|
||||
|
||||
## Eigenheiten und Stolperfallen der Syntax
|
||||
## Idiosyncrasies and Pitfalls of Syntax
|
||||
|
||||
- Man kann den Multiplikationsoperator `*` nach einer numerischen Konstanten weglassen, wenn eine Variable, Funktion oder öffnende Klammer folgt.
|
||||
- After a numeric constant, the multiplication operator `*` can be omitted when a variable, function, or opening parenthesis follows.
|
||||
```
|
||||
z = 3.4x + 2(x+y) + xy
|
||||
```
|
||||
|
||||
ist daher korrektes Julia. Beachte allerdings, dass der Term `xy` als eine Variable mit dem Namen xy interpretiert wird __und nicht__ als Produkt von x und y!
|
||||
is therefore valid Julia. Note, however, that the term `xy` is interpreted as a single variable named xy __and not__ as the product of x and y!
|
||||
|
||||
- Diese Regel hat ein paar Tücken:
|
||||
- This rule has a few pitfalls:
|
||||
|
||||
Das funktioniert wie erwartet:
|
||||
This works as expected:
|
||||
```{julia}
|
||||
e = 7
|
||||
3e
|
||||
```
|
||||
|
||||
Hier wird die Eingabe als Gleitkommazahl interpretiert -- und `3E+2` oder `3f+2` (Float32) ebenso.
|
||||
Here, the input is interpreted as a floating-point number -- and `3E+2` or `3f+2` (Float32) as well.
|
||||
|
||||
```{julia}
|
||||
3e+2
|
||||
```
|
||||
|
||||
Ein Leerzeichen schafft Eindeutigkeit:
|
||||
A space creates clarity:
|
||||
|
||||
```{julia}
|
||||
3e + 2
|
||||
```
|
||||
|
||||
Das funktioniert:
|
||||
This works:
|
||||
|
||||
```{julia}
|
||||
x = 4
|
||||
3x + 3
|
||||
```
|
||||
|
||||
...und das nicht. `0x`, `0o`, `0b` wird als Anfang einer Hexadezimal-, Oktal- bzw. Binärkonstanten interpretiert.
|
||||
...and this does not. `0x`, `0o`, `0b` are interpreted as the beginning of a hexadecimal, octal, or binary constant.
|
||||
|
||||
```{julia}
|
||||
3y + 0x
|
||||
```
|
||||
|
||||
|
||||
- Es gibt noch ein paar andere Fälle, bei denen die sehr kulante Syntax zu Überraschungen führt.
|
||||
- There are a few other cases where the very permissive syntax leads to surprises.
|
||||
|
||||
|
||||
```{julia}
|
||||
Wichtig = 21
|
||||
Wichtig! = 42 # Bezeichner können auch ein ! enthalten
|
||||
Wichtig! = 42 # identifiers can also contain !
|
||||
(Wichtig, Wichtig!)
|
||||
```
|
||||
|
||||
@@ -596,28 +597,24 @@ Wichtig!=88
|
||||
```
|
||||
|
||||
|
||||
Julia interpretiert das als Vergleich `Wichtig != 88`.
|
||||
|
||||
Leerzeichen helfen:
|
||||
Julia interprets this as the comparison `Wichtig != 88`.
|
||||
|
||||
Spaces help:
|
||||
|
||||
```{julia}
|
||||
Wichtig! = 88
|
||||
Wichtig!
|
||||
```
|
||||
|
||||
- Operatoren der Form `.*`, `.+`,... haben in Julia eine spezielle Bedeutung (*broadcasting*, d.h., vektorisierte Operationen).
|
||||
- Operators of the form `.*`, `.+`,... have a special meaning in Julia (*broadcasting*, i.e., vectorized operations).
|
||||
|
||||
```{julia}
|
||||
1.+2.
|
||||
```
|
||||
|
||||
Wieder gilt: Leerzeichen schaffen Klarheit!
|
||||
Again, spaces create clarity!
|
||||
|
||||
```{julia}
|
||||
1. + 2.
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user