FOR-NEXT/de
(→(**COPIED FROM ENGLISH PAGE**) =) |
(→Example) |
||
(2 intermediate revisions by one user not shown) | |||
Line 15: | Line 15: | ||
<span style="font-family: Arial;"> </span> | <span style="font-family: Arial;"> </span> | ||
− | = <span class="f_Header"> | + | = <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);" | | ||
− | + | 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);" | | ||
− | + | 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);" | | ||
− | + | 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);" | | ||
− | + | 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> | ||
− | | | + | | 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> | ||
− | | | + | | 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> | ||
− | | | + | | 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> | ||
− | | | + | | Die Benutzung von STEP ist optional. Standardmäßig wird für STEP 1 verwendet. |
|} | |} | ||
</div> | </div> | ||
<span style="font-family: Arial;"> </span> | <span style="font-family: Arial;"> </span> | ||
− | + | 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 | Dim x, as byte, st as byte, ed as byte | ||
− | FOR x = st TO ED ' | + | FOR x = st TO ED ' dies ist o.k. weil alle Variablen vom selben Datentyp sind. |
| | ||
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 ' | + | FOR x = st TO ED ' dies ist NICHT o.k. weil die Variablen unterschiedliche Datentypen haben. |
| | ||
− | 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.) | ||
| | ||
− | + | 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. | |
| | ||
Line 112: | Line 115: | ||
| | ||
− | + | Es gibt auch andere Möglichkeiten. Zum Beispiel kann man eine DO..LOOP Schleife benutzen : | |
<span style="font-family: Arial;"> </span> | <span style="font-family: Arial;"> </span> | ||
Line 128: | Line 131: | ||
<span style="font-family: Arial;"> </span> | <span style="font-family: Arial;"> </span> | ||
− | + | Es gibt viele Möglichkeiten, zum gewünschten Resultat zu kommen. | |
− | + | ||
| | ||
<span style="font-family: Arial;"> </span> | <span style="font-family: Arial;"> </span> | ||
− | = <span class="f_Header"> | + | = <span class="f_Header">Siehe auch</span> = |
[[EXIT|EXIT FOR]] | [[EXIT|EXIT FOR]] | ||
Line 144: | Line 146: | ||
<span style="font-family: Arial;"> </span> | <span style="font-family: Arial;"> </span> | ||
− | = <span class="f_Header"> | + | = <span class="f_Header">Beispiel</span> = |
<br/><source lang="bascomavr"> | <br/><source lang="bascomavr"> | ||
Line 156: | Line 158: | ||
'----------------------------------------------------------------------------------------- | '----------------------------------------------------------------------------------------- | ||
− | $regfile = "m48def.dat" ' | + | $regfile = "m48def.dat" ' Benutzter Mikrocontroller |
− | $crystal = 4000000 ' | + | $crystal = 4000000 ' Benutzte Taktfrequenz |
− | $baud = 19200 ' | + | $baud = 19200 ' Baudrate |
$hwstack = 32 ' default use 32 for the hardware stack | $hwstack = 32 ' default use 32 for the hardware stack | ||
$swstack = 10 ' default use 10 for the SW stack | $swstack = 10 ' default use 10 for the SW stack | ||
Line 167: | Line 169: | ||
For A = 1 To 10 Step 2 | For A = 1 To 10 Step 2 | ||
− | Print " | + | Print "Dieses ist A " ; A |
Next A | Next A | ||
− | Print " | + | Print "Herunterzaehlen" |
For C = 10 To -5 Step -1 | For C = 10 To -5 Step -1 | ||
− | Print " | + | Print "Dieses ist C " ; C |
Next | Next | ||
− | Print " | + | Print "Auch Verschachtelungen von FOR..NEXT Konstrukten sind möglich." |
For A = 1 To 10 | For A = 1 To 10 | ||
− | Print " | + | Print "Dieses ist A " ; A |
For B1 = 1 To 10 | For B1 = 1 To 10 | ||
− | Print " | + | Print "Dieses ist B1" ; B1 |
− | Next ' | + | Next ' Beachte: Der Parameter nach NEXT muss nicht spezifiziert werden |
Next A | Next A | ||
End | End |
Latest revision as of 00:27, 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.
Siehe auch
Beispiel
'----------------------------------------------------------------------------------------- '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" ' Benutzter Mikrocontroller $crystal = 4000000 ' Benutzte Taktfrequenz $baud = 19200 ' Baudrate $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 "Dieses ist A " ; A Next A Print "Herunterzaehlen" For C = 10 To -5 Step -1 Print "Dieses ist C " ; C Next Print "Auch Verschachtelungen von FOR..NEXT Konstrukten sind möglich." For A = 1 To 10 Print "Dieses ist A " ; A For B1 = 1 To 10 Print "Dieses ist B1" ; B1 Next ' Beachte: Der Parameter nach NEXT muss nicht spezifiziert werden Next A End
Languages | English • Deutsch |
---|