FOR-NEXT/de

(Difference between revisions)
Jump to: navigation, search
((**COPIED FROM ENGLISH PAGE**) =)
(Remarks)
Line 15: Line 15:
 
<span style="font-family: Arial;">&nbsp;</span>
 
<span style="font-family: Arial;">&nbsp;</span>
  
= <span class="f_Header">Remarks</span> =
+
= <span class="f_Header">Anmerkungen</span> =
 
<div style="padding: 0px; margin: 0px 0px 0px 4px;">
 
<div style="padding: 0px; margin: 0px 0px 0px 4px;">
 
{| width="601" cellspacing="0" cellpadding="1" border="1" style="border: 2px solid rgb(0, 0, 0); border-spacing: 0px; border-collapse: collapse;"
 
{| width="601" cellspacing="0" cellpadding="1" border="1" style="border: 2px solid rgb(0, 0, 0); border-spacing: 0px; border-collapse: collapse;"
Line 23: Line 23:
  
 
| valign="top" width="85%" style="width: 494px; border: 1px solid rgb(0, 0, 0);" |  
 
| valign="top" width="85%" style="width: 494px; border: 1px solid rgb(0, 0, 0);" |  
The variable counter to use
+
Zählervariable
  
 
|- style="vertical-align: top;"
 
|- style="vertical-align: top;"
Line 30: Line 30:
  
 
| valign="top" width="85%" style="width: 494px; border: 1px solid rgb(0, 0, 0);" |  
 
| valign="top" width="85%" style="width: 494px; border: 1px solid rgb(0, 0, 0);" |  
The starting value of the variable var
+
Startwert der Zählervariablen var
  
 
|- style="vertical-align: top;"
 
|- style="vertical-align: top;"
Line 37: Line 37:
  
 
| valign="top" width="85%" style="width: 494px; border: 1px solid rgb(0, 0, 0);" |  
 
| valign="top" width="85%" style="width: 494px; border: 1px solid rgb(0, 0, 0);" |  
The ending value of the variable var
+
Endwert der Zählervariablen var
  
 
|- style="vertical-align: top;"
 
|- style="vertical-align: top;"
Line 44: Line 44:
  
 
| valign="top" width="85%" style="width: 494px; border: 1px solid rgb(0, 0, 0);" |  
 
| valign="top" width="85%" style="width: 494px; border: 1px solid rgb(0, 0, 0);" |  
The value var is increased/decreased with each time NEXT is encountered.
+
Wert, um den die Zählervariable var jedesmal erhöht/vermindert wird wenn NEXT erreicht wird.
  
 
|}
 
|}
Line 53: Line 53:
 
|- valign="baseline" style="vertical-align: baseline;"
 
|- valign="baseline" style="vertical-align: baseline;"
 
| width="13" | <span style="font-size: 10pt; font-family: 'Arial Unicode MS', 'Lucida Sans Unicode', Arial;">•</span>
 
| width="13" | <span style="font-size: 10pt; font-family: 'Arial Unicode MS', 'Lucida Sans Unicode', Arial;">•</span>
| For incremental loops, you must use TO.
+
| Für hochzählende Schleifen muss TO verwendet werden.
 
|}
 
|}
 
</div><div style="padding: 0px; margin: 0px 0px 0px 11px;">
 
</div><div style="padding: 0px; margin: 0px 0px 0px 11px;">
Line 59: Line 59:
 
|- valign="baseline" style="vertical-align: baseline;"
 
|- valign="baseline" style="vertical-align: baseline;"
 
| width="13" | <span style="font-size: 10pt; font-family: 'Arial Unicode MS', 'Lucida Sans Unicode', Arial;">•</span>
 
| width="13" | <span style="font-size: 10pt; font-family: 'Arial Unicode MS', 'Lucida Sans Unicode', Arial;">•</span>
| For decremental loops, you must use a negative step size.
+
| Für herunterzählende Schleifen muss eine negative Schrittweite value angegeben werden.
 
|}
 
|}
 
</div><div style="padding: 0px; margin: 0px 0px 0px 11px;">
 
</div><div style="padding: 0px; margin: 0px 0px 0px 11px;">
Line 65: Line 65:
 
|- valign="baseline" style="vertical-align: baseline;"
 
|- valign="baseline" style="vertical-align: baseline;"
 
| width="13" | <span style="font-size: 10pt; font-family: 'Arial Unicode MS', 'Lucida Sans Unicode', Arial;">•</span>
 
| width="13" | <span style="font-size: 10pt; font-family: 'Arial Unicode MS', 'Lucida Sans Unicode', Arial;">•</span>
| You must end a FOR structure with the NEXT statement.
+
| Ein FOR-Konstrukt muss mit NEXT abgeschlossen.
 
|}
 
|}
 
</div><div style="padding: 0px; margin: 0px 0px 0px 11px;">
 
</div><div style="padding: 0px; margin: 0px 0px 0px 11px;">
Line 71: Line 71:
 
|- valign="baseline" style="vertical-align: baseline;"
 
|- valign="baseline" style="vertical-align: baseline;"
 
| width="13" | <span style="font-size: 10pt; font-family: 'Arial Unicode MS', 'Lucida Sans Unicode', Arial;">•</span>
 
| width="13" | <span style="font-size: 10pt; font-family: 'Arial Unicode MS', 'Lucida Sans Unicode', Arial;">•</span>
| The use of STEP is optional. By default, a value of 1 is used.
+
| Die Benutzung von STEP ist optional. Standardmäßig wird für STEP 1 verwendet.
 
|}
 
|}
 
</div>
 
</div>
 
<span style="font-family: Arial;">&nbsp;</span>
 
<span style="font-family: Arial;">&nbsp;</span>
  
When you know in advance how many times a block of code must be executed, the FOR..NEXT loop is convenient to use.
+
Wenn im Voraus bekannt ist wie oft eine Schleife ausgeführt werden soll dann ist eine FOR..NEXT Schleife geeignet.
  
You can exit a FOR .. NEXT loop with the EXIT FOR statement.
+
Man kann eine FOR..NEXT Schleife mit EXIT FOR (vorzeitig) verlassen.
  
 
&nbsp;
 
&nbsp;
  
It is important that the if you use variables for START and END, that these are of the same data type. So for example:
+
Wenn für Startwert und Endwert Variable verwendet werden dann müssen diese vom selben Datentyp sein.
 +
 
 +
Beispiel:
  
 
Dim x, as byte, st as byte, ed as byte
 
Dim x, as byte, st as byte, ed as byte
  
FOR x = st TO ED ' this is ok since all variables are of the same data type
+
FOR x = st TO ED ' dies ist o.k. weil alle Variablen vom selben Datentyp sind.
  
 
&nbsp;
 
&nbsp;
Line 92: Line 94:
 
Dim x as Byte, st as Word, Ed as Long
 
Dim x as Byte, st as Word, Ed as Long
  
FOR x = st TO ED ' this is NOT ok since all variables are of different data type.
+
FOR x = st TO ED ' dies ist NICHT o.k. weil die Variablen unterschiedliche Datentypen haben.
  
 
&nbsp;
 
&nbsp;
  
The reason is that when the condition is evaluated, it will create a compare on 2 bytes, while you actually want to have a word since the end variable is a word.
+
Der Grund ist, dass bei Auswertung der Bedingung ein 2 Byte Vergleich stattfindet während man eigentlich Long haben möchte weil die End-Variable Long ist.
 +
(Originaltext: The reason is that when the condition is evaluated, it will create a compare on 2 bytes, while you actually want to have a word since the end variable is a word.)
  
 
&nbsp;
 
&nbsp;
  
A for next loop with an integer has an upper limit of 32766 and not 32767, the maximum value that fits into an integer.
+
Eine FOR-NEXT Schleife mit Integer hat einen oberen Wert von 32766 und nicht 32767 - die größte Zahl, die in eine Integer-Variable passt.
  
This is done in order to save code space. Checking an overflow from 32767 to -32768 would cost extra code.
+
Der Grund ist Codeoptimierung. Eine Prüfung auf 32767 bis -32768 würde mehr internen Code benötigen.
  
 
&nbsp;
 
&nbsp;
Line 112: Line 115:
 
&nbsp;
 
&nbsp;
  
There are also other alternatives. You can use a Do.. Loop for example&nbsp;:
+
Es gibt auch andere Möglichkeiten. Zum Beispiel kann man eine DO..LOOP Schleife benutzen&nbsp;:
  
 
<span style="font-family: Arial;">&nbsp;</span>
 
<span style="font-family: Arial;">&nbsp;</span>
Line 128: Line 131:
 
<span style="font-family: Arial;">&nbsp;</span>
 
<span style="font-family: Arial;">&nbsp;</span>
  
There are various way to get the result you need.
+
Es gibt viele Möglichkeiten, zum gewünschten Resultat zu kommen.
 
+
 
&nbsp;
 
&nbsp;
  

Revision as of 00:19, 19 February 2013

Funktion =

Schleife mit einer festgelegten Anzahl an Wiederholungen.

 

 

Contents

Syntax

FOR var = start TO end [STEP value]

 

 

Anmerkungen

var

Zählervariable

start

Startwert der Zählervariablen var

end

Endwert der Zählervariablen var

value

Wert, um den die Zählervariable var jedesmal erhöht/vermindert wird wenn NEXT erreicht wird.

 

Für hochzählende Schleifen muss TO verwendet werden.
Für herunterzählende Schleifen muss eine negative Schrittweite value angegeben werden.
Ein FOR-Konstrukt muss mit NEXT abgeschlossen.
Die Benutzung von STEP ist optional. Standardmäßig wird für STEP 1 verwendet.

 

Wenn im Voraus bekannt ist wie oft eine Schleife ausgeführt werden soll dann ist eine FOR..NEXT Schleife geeignet.

Man kann eine FOR..NEXT Schleife mit EXIT FOR (vorzeitig) verlassen.

 

Wenn für Startwert und Endwert Variable verwendet werden dann müssen diese vom selben Datentyp sein.

Beispiel:

Dim x, as byte, st as byte, ed as byte

FOR x = st TO ED ' dies ist o.k. weil alle Variablen vom selben Datentyp sind.

 

Dim x as Byte, st as Word, Ed as Long

FOR x = st TO ED ' dies ist NICHT o.k. weil die Variablen unterschiedliche Datentypen haben.

 

Der Grund ist, dass bei Auswertung der Bedingung ein 2 Byte Vergleich stattfindet während man eigentlich Long haben möchte weil die End-Variable Long ist. (Originaltext: The reason is that when the condition is evaluated, it will create a compare on 2 bytes, while you actually want to have a word since the end variable is a word.)

 

Eine FOR-NEXT Schleife mit Integer hat einen oberen Wert von 32766 und nicht 32767 - die größte Zahl, die in eine Integer-Variable passt.

Der Grund ist Codeoptimierung. Eine Prüfung auf 32767 bis -32768 würde mehr internen Code benötigen.

 

 

 

 

Es gibt auch andere Möglichkeiten. Zum Beispiel kann man eine DO..LOOP Schleife benutzen :

 

Dim Var As Byte

Do

'code

 Incr Var

Loop Until Var = 10

 

Es gibt viele Möglichkeiten, zum gewünschten Resultat zu kommen.  

 

See also

EXIT FOR

 

 

 

Example


'-----------------------------------------------------------------------------------------
'name : for_next.bas
'copyright : (c) 1995-2005, MCS Electronics
'purpose : demo: FOR, NEXT
'micro : Mega48
'suited for demo : yes
'commercial addon needed : no
'-----------------------------------------------------------------------------------------
 
$regfile = "m48def.dat" ' specify the used micro
$crystal = 4000000 ' used crystal frequency
$baud = 19200 ' use baud rate
$hwstack = 32 ' default use 32 for the hardware stack
$swstack = 10 ' default use 10 for the SW stack
$framesize = 40 ' default use 40 for the frame space
 
Dim A As Byte , B1 As Byte , C As Integer
 
 
For A = 1 To 10 Step 2
Print "This is A " ; A
Next A
 
Print "Now lets count down"
For C = 10 To -5 Step -1
Print "This is C " ; C
Next
 
 
 
Print "You can also nest FOR..NEXT statements."
For A = 1 To 10
Print "This is A " ; A
For B1 = 1 To 10
 Print "This is B1 " ; B1
Next ' note that you do not have to specify the parameter
Next A
End

Languages   English Deutsch  
Personal tools
Namespaces
Variants
Actions
Navigation
In other languages
Language