WHILE...WEND: Difference between revisions
Jump to navigation
Jump to search
Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link
(Created page with "The {{KW|WHILE...WEND}} statement is used to repeat a block of statements while the condition is met. {{PageSyntax}} :{{KW|WHILE}} {{Parameter|condition}} :. :. :. :{{KW|WEND}} {{PageDescription}} * {{Parameter|condition}} is a numeric expression used to determine if the loop will execute. * {{Parameter|statements}} will execute repeatedly while {{Parameter|condition}} is a non-zero value. * EXIT WHILE can be used for emergency exits from the loop in QB64 only. *...") |
No edit summary |
||
(4 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
The | The [[WHILE...WEND]] statement is used to repeat a block of statements while the condition is met. | ||
{{PageSyntax}} | {{PageSyntax}} | ||
: | :[[WHILE]] {{Parameter|condition}} | ||
:. | :. | ||
:. | :. | ||
:. | :. | ||
: | :[[WEND]] | ||
Line 18: | Line 18: | ||
{{ | {{RelationalOperationsPlugin}} | ||
Line 24: | Line 24: | ||
''Example 1:'' Reading an entire file. Example assumes the program has a [[OPEN|file opened]] as #1 | ''Example 1:'' Reading an entire file. Example assumes the program has a [[OPEN|file opened]] as #1 | ||
{{CodeStart}} | {{CodeStart}} | ||
{{Cl|OPEN}} "Readme.txt" FOR {{Cl|INPUT (file mode)|INPUT}} AS #1 | {{Cl|OPEN}} "Readme.txt" FOR {{Cl|INPUT (file mode)|INPUT}} AS #1 | ||
{{Cl|WHILE...WEND|WHILE}} {{Cl|NOT}} {{Cl|EOF}}(1) | {{Cl|WHILE...WEND|WHILE}} {{Cl|NOT}} {{Cl|EOF}}(1) | ||
{{Cl|_LIMIT}} 1 'limit line prints to one per second | {{Cl|_LIMIT}} 1 'limit line prints to one per second | ||
{{Cl|LINE INPUT (file statement)|LINE INPUT #}}1, text$ | {{Cl|LINE INPUT (file statement)|LINE INPUT #}}1, text$ | ||
IF {{Cl|INKEY$}} = {{Cl|CHR$}}(27) THEN {{Cl|EXIT}} {{Cl|WHILE}} 'ESC key exits | IF {{Cl|INKEY$}} = {{Cl|CHR$}}(27) THEN {{Cl|EXIT}} {{Cl|WHILE}} 'ESC key exits | ||
{{Cl|PRINT}} text$ | {{Cl|PRINT}} text$ | ||
{{Cl|WEND}} | {{Cl|WEND}} | ||
{{CodeEnd}} | {{CodeEnd}} | ||
''Example 2:'' Clearing the keyboard buffer. | ''Example 2:'' Clearing the keyboard buffer. | ||
{{CodeStart}} | {{CodeStart}} | ||
{{Cl|WHILE}} {{Cl|INKEY$}} <> "" : {{Cl|WEND}} | {{Cl|WHILE}} {{Cl|INKEY$}} <> "" : {{Cl|WEND}} | ||
{{CodeEnd}} | {{CodeEnd}} | ||
Line 43: | Line 43: | ||
* [[DO...LOOP]] | * [[DO...LOOP]] | ||
* [[FOR...NEXT]] | * [[FOR...NEXT]] | ||
* [[UNTIL]] | * [[UNTIL]] | ||
* [[_CONTINUE]] | * [[_CONTINUE]] | ||
{{PageNavigation}} | {{PageNavigation}} |
Latest revision as of 21:40, 2 February 2023
The WHILE...WEND statement is used to repeat a block of statements while the condition is met.
Syntax
Description
- condition is a numeric expression used to determine if the loop will execute.
- statements will execute repeatedly while condition is a non-zero value.
- EXIT WHILE can be used for emergency exits from the loop in QB64 only.
- A DO...LOOP can use the same DO WHILE condition to get the same results.
- WHILE loops only run if the WHILE condition is True.
Table 3: The relational operations for condition checking. In this table, A and B are the Expressions to compare. Both must represent the same general type, i.e. they must result into either numerical values or STRING values. If a test succeeds, then true (-1) is returned, false (0) if it fails, which both can be used in further Boolean evaluations. ┌─────────────────────────────────────────────────────────────────────────┐ │ Relational Operations │ ├────────────┬───────────────────────────────────────────┬────────────────┤ │ Operation │ Description │ Example usage │ ├────────────┼───────────────────────────────────────────┼────────────────┤ │ A = B │ Tests if A is equal to B. │ IF A = B THEN │ ├────────────┼───────────────────────────────────────────┼────────────────┤ │ A <> B │ Tests if A is not equal to B. │ IF A <> B THEN │ ├────────────┼───────────────────────────────────────────┼────────────────┤ │ A < B │ Tests if A is less than B. │ IF A < B THEN │ ├────────────┼───────────────────────────────────────────┼────────────────┤ │ A > B │ Tests if A is greater than B. │ IF A > B THEN │ ├────────────┼───────────────────────────────────────────┼────────────────┤ │ A <= B │ Tests if A is less than or equal to B. │ IF A <= B THEN │ ├────────────┼───────────────────────────────────────────┼────────────────┤ │ A >= B │ Tests if A is greater than or equal to B. │ IF A >= B THEN │ └────────────┴───────────────────────────────────────────┴────────────────┘ The operations should be very obvious for numerical values. For strings be aware that all checks are done case sensitive (i.e. "Foo" <> "foo"). The equal/not equal check is pretty much straight forward, but for the less/greater checks the ASCII value of the first different character is used for decision making: E.g. "abc" is less than "abd", because in the first difference (the 3rd character) the "c" has a lower ASCII value than the "d". This behavior may give you some subtle results, if you are not aware of the ASCII values and the written case: E.g. "abc" is greater than "abD", because the small letters have higher ASCII values than the capital letters, hence "c" > "D". You may use LCASE$ or UCASE$ to make sure both strings have the same case. |
Examples
Example 1: Reading an entire file. Example assumes the program has a file opened as #1
OPEN "Readme.txt" FOR INPUT AS #1 WHILE NOT EOF(1) _LIMIT 1 'limit line prints to one per second LINE INPUT #1, text$ IF INKEY$ = CHR$(27) THEN EXIT WHILE 'ESC key exits PRINT text$ WEND |
Example 2: Clearing the keyboard buffer.
WHILE INKEY$ <> "" : WEND |
See also