請問在QBasic能否用Do while loop

2013-07-17 10:08 pm
我找過,QBasic是有do這個指令的,但是這個又是否代表do while loop呢?如果是的話,又怎樣用呢?如果不是的話,又有沒有代表這種指令的語法?有的話,又怎樣用呢?

回答 (1)

2013-07-17 10:28 pm
✔ 最佳答案

圖片參考:http://www.svatopluk.com/qbtutor/next.gif

Loops
圖片參考:http://www.svatopluk.com/qbtutor/star.gif


"Loops" make it easier to do an action multiple times. There are at least four types of loops: IF...GOTO , WHILE...WEND , DO...LOOP , and FOR...NEXT .


IF...GOTO

This program uses IF...GOTO to create a loop:

x = 10

start:
PRINT x

x = x + 1 (This adds 1 to x)

IF x < 15 THEN GOTO start
Output:10
11
12
13
14

WHILE...WEND

The WHILE...WEND commands continue a loop until a specified expression is false.

To use WHILE...WEND :Place an expression after WHILE
Enter a list of commands
Place WEND at the end


Run the following:x = 10

WHILE x < 15

PRINT x

x = x + 1

WEND Output (same as in previous example):10
11
12
13
14

DO...LOOP

DO...LOOP is exactly the same as WHILE...WEND , except it has at least two slight advantages. With DO...LOOP you can:Loop until an expression is true
Loop at least one time regardless of whether the expression is true or not.


To use DO...LOOP :Specify whether the loop continues "while" the expression is true or "until" the expression is true, using the WHILE and UNTIL statements, respectively.
Place an expression after WHILE / UNTIL
Enter a list of commands
Place LOOP at the end


The following uses the WHILE statement:x = 10

DO WHILE x < 15

PRINT x

x = x + 1

LOOP
This program uses the UNTIL statement:x = 10

DO UNTIL x = 15

PRINT x

x = x + 1

LOOP
They both output:10
11
12
13
14

If you place the expression at the end of the loop instead, the program goes through the loop at least once.x = 32

DO

PRINT x

x = x + 1

LOOP WHILE x < 5 This is the output because the loop was only gone through one time:32



收錄日期: 2021-04-13 19:33:55
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20130717000051KK00161

檢視 Wayback Machine 備份