Skip to content
Snippets Groups Projects
Commit 52d7c7f6 authored by Chris's avatar Chris
Browse files

Notebook zu WHILE-Programmen

parent 931cbac0
Branches
No related tags found
1 merge request!1Master
%% Cell type:markdown id: tags:
# WHILE-Programme
Um LOOP-Programme zu erweitern führen wir eine WHILE-Schleife ein und erhalten damit die WHILE-Programme.
Die Syntax solcher WHILE-Programme ist definiert durch:
* Jedes LOOP-Programm ist ein WHILE-Programm
* Falls $P_1$ und $P_2$ WHILE-Programme sind ist $P_1; P_2$ ein WHILE-Programm
* Falls $P$ ein WHILE-Programm ist, so ist auch $WHILE\ x_i\neq 0\ DO\ P\ END$ ein WHILE-Programm.
%% Cell type:markdown id: tags:
Die Semantik von WHILE-Programmen ist analog zu der von LOOP-Programmen zu verstehen.
Der einzige Unterschied sind die WHILE-Schleifen.
Das Programm P in einer WHILE-Schleife wird solange wiederholt, wie $x_i \neq 0$ ist.
Dabei ist zu beachten, dass $x_i$ in P vorkommen sollte, da es sonst nicht verändert wird.
Dies ist ein Unterschied zu LOOP-Programmen, da $x_i$ in dem entsprechenden LOOP nicht verwendet werden darf.
%% Cell type:markdown id: tags:
Eine Funktion heißt WHILE-berechenbar, falls es ein WHILE-Programm gibt, das mit $n_1,...,n_k$ in den Variablen $x_1,...,x_k$ gestartet wird und mit $f(n_1,n_2,...,n_k)$ in der Variablen $x_0$ endet, falls $f(n_1,n_2,...,n_k)$ definiert ist. Andernfalls stoppt das WHILE-Programm nicht.
Ein Beispiel dafür ist die 2er-Potenz $f(n) = n^2$:
%% Cell type:code id: tags:
``` python
%run Interpreter/whileinterpreter.py
```
%% Cell type:code id: tags:
``` python
interpret('''
x1:=10;
x0:=1;
WHILE x1 /= 0 DO
x2:=x0+0;
LOOP x2 DO
x0:=x0+1
END;
x1:=x1-1
END''')
```
%% Output
1024
%% Cell type:markdown id: tags:
Ein weiteres Beispiel ist die Funktion f(n) = n mod 2
%% Cell type:code id: tags:
``` python
interpret('''
x1:=16;
x2:=x1-1;
WHILE x2!=0 DO
x1:=x1-2;
x2:=x2-2
END;
x0:=x1+0
''')
```
%% Output
0
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment