BasicsTutorial/Loops

Last-modified: 2009-11-27 (金) 20:56:39

Loops

ループ

Loops are handy for repeating code over and over, say if you wanted to keep asking random mathematical questions.
ループは何度もコードを繰り返すことに便利で、あなたがランダムな数学的な質問をし続けたかったかどうか言う。
We’ve already seen one form of a loop, the label with a goto.
我々は、ひとつのループの生まれるのをすでに見た。(gotoによるラベル)

Start:
Print “Hello”
Goto Start

DO...LOOP

But this isn’t really that neat, since you have to say which label you want to go to.
しかし、あなたがあなたがどのラベルへ行きたいかについて言わなければならないから、これは本当にそんなにきちんとしていない。
Try to use the following loops instead where you can (which is pretty much any situation).
あなたがそうすることができる(それは、ほとんどどんな状況ででもある)所で、その代わりに以下のループを使用しようと試みなさい。
This is the simplest loop of all…
これは、最も単純なループである

Do
Loop

DO is the starting point, and LOOP is the end point.
DOが、出発点である、そして、LOOPは終点である。
When the computer passes DO, it makes a note of it, and looks out for the LOOP.
コンピュータ・パスが DOするとき、それはそれを書きとめて、LOOPに気をつける。
When it finds the LOOP it goes back to the DO and this carries on.
それが LOOPを見つける時、それは、DOに行く、そして続行する。
To exit the loop use the EXIT command.
ループを出るために、EXIT命令を使いなさい。

`Main loop
Do
  `Set a random colour
  Ink rgb(rnd(255),rnd(255),rnd(255)),0
  `Get some random coordinates
  boxX=rnd(640)
  boxY=rnd(480)
  `Draw a box of size 20x20 at the coordinates
  Box boxX-10,boxY-10,boxX+10,boxY+10
  `If space is pressed then exit the loop
  If spacekey()=1 then exit
Loop
`Print a message
Set cursor 220,240
Ink rgb(255,255,255),0
Print "Exited, Press any key..."
Wait key

That code just keeps looping and drawing random boxes (you should be able to figure out how I did that by now, I’m going to keep introducing little techniques like that from now on, so watch out for them) until the SPACEKEY() function equals 1.
そのコードはちょうどSPACEKEY()関数が1に等しいまでランダムな箱をループし続ける(あなたは私がどのように今ごろはそうしたかについてわかることができるはずである、私はそれらのためにそのような今後ほとんど技術でない、それで、監視を持ち出すことを中に入れないつもりである)。
SPACEKEY() returns a 1 when space bar is pressed.
SPACEKEY()、スペース・バーが押されるとき、1を返す。
Here are a few more commands that return 1 when a button is pressed…
ここでは、ボタンが押されるとき、もう2、3の命令は 1を返す。

Spacekey() = space bar
Controlkey() = control
Returnkey() = return
Shiftkey() = shift
Escapekey() = escape

The only problem with the escape key is that it is automatically configured to escape the program, so solve this by using…
エスケープ・キーに関する唯一の問題は、それがプログラムを逃れて、それで、using…によってこれを解決するように自動的に構成されるということである

Disable escapekey

The only problem with that though is that there is no way to exit your program if you forget to add in…
それに関する唯一の問題は、しかし、あなたがin…を加えるのを忘れるならば、あなたのプログラムを出る方法が無いということである

End

Somewhere.
どこかで。
END ends the program.
ENDは、プログラムを終了する。
A lot of new commands eh?
多くの新しい命令 えっ?
Don’t worry if you can’t remember them, you will in time.
あなたがそれらを覚えていることができないならば、心配してはいけない、あなたは遅れずにそうする。
Back to the topic…
topicへ戻る

REPEAT...UNTIL

Or you could also use this loop, then you could omit the IF SPACEKEY() = 1 line.
または、あなたはこのループを使用することもできた、それから、あなたは IF SPACEKEY() = 1 を省略することができた。

`Main loop
Repeat
  `Set a random colour
  Ink rgb(rnd(255),rnd(255),rnd(255)),0
  `Get some random coordinates
  boxX=rnd(640)
  boxY=rnd(480)
  `Draw a box of size 20x20 at the coordinates
  Box boxX-10,boxY-10,boxX+10,boxY+10
  `If space is pressed then exit the loop
Until spacekey() = 1
`Print a message
Set cursor 220,240
Ink rgb(255,255,255),0
Print "Exited, Press any key..."
Wait key

The REPEAT_UNTIL_loop is the same as the DO_LOOP_loop, except when the computer reaches the _UNTIL, it checks whether the statement afterwards is equal to one (like the _IF statement) and if it is exits the loop.
DO_LOOPループが、コンピュータが_UNTILに着く時以外は、ループするREPEAT_UNTILループと同じです、ステートメントがその後 1と等しいかどうか調べる( IF命令文のような)、そして、それがループの出口である。

WHILE...ENDWHILE

The WHILE_ENDWHILE loop is the same as the REPEAT_UNTIL loop, except it checks the statement at the top of the loop and only continues if the statement is true.
ステートメントが真であるならば それがループの最上位でステートメントをチェックして、続けるだけであること以外は、WHILE_ENDWHILEループは REPEAT_UNTILループと同じである。

While statement
  Do stuff
Endwhile

Loops can be useful for repeating code infinitely, like that random box code, but it can also be useful for doing things a certain amount of times.
ループは無限にコードを繰り返すことに役立つことがありえて、そのランダムな箱コードを好む、しかし、それはある程度の時することに役立つこともありえる。

Repeat
  Inc x
  Print x
  Wait 100
Until x>=10
Wait key

Another one of those useful things there, printing the numbers 1 to 10 with a loop, rather than writing it all out in 10 print statements.
1から10のプリント命令文においてそれのすべてを書くことよりはむしろそこのそれらの役に立つものの別のもの(ループで番号1~10をプリントする)。
Two new commands there.
そこの2つの新しい命令。
INC increments (increases) the variable specified by 1, or you can specify a different number to increment by.
INCは指定される変数を増加させる(増やす)、あるいは、あなたは増加する異なる数を指定することができる。

Repeat
  Inc x,2
  Print x
  Wait 100
Until x>=10
Wait key

That increments ‘x’ by 2 each time instead.
それは、その代わりにそれぞれ2回『x』を増加させる。
INC is paired with DEC which does the same but decrements (decreases) instead.
DECは、INCと同じことをするが、その代わりにDECは減少する。DECとINCはともに対にされる。

WAIT is the other command.
WAITは、他の命令である。
It basically makes the computer pause at that line for however many milliseconds (1000 in a second, remember TIMER()) and then continues.
それは、基本的にコンピュータをしかし多くのミリ秒(すぐに1/1000秒、タイマー()を覚えている)のためにその行で停止させて、それから、続ける。
In this case it pauses for 100 milliseconds (1/10th of a second) between each print.
この場合、それは各々のプリントの間で100ミリ秒(1/10秒)のために止まる。

FOR...NEXT 

This isn’t the best way of doing this though.
これは、ずっとしかしこうすることで、最良でない。
There is a loop made specially for doing something a certain number of times, the FOR NEXT loop.
特定の数の回数を何かにすることのために特別に行われるループがある、FOR NEXT ループ。

For x = 1 to 10
  Print x
  Wait 100
Next x
Wait key

Four lines instead of five.
5の代わりに4本の行。
This loop as well as only repeating 10 times increases the variable ‘x’ by one each time, just like INC in the last example.
10回を繰り返すだけであることと同様にこのループは、一つずつ回数までに、 変数『x』を増やす。(まるで最後の例のINCのような)
You always have to put the name of the variable after _NEXT at the end of the loop.
あなたは、ループ終了後_NEXTの後に、常に変数の名前を置かなければならない。(省略も可能。)
 

But what about incrementing by more than one?
しかし、1以上で増加することはどうですか?
Well, there’s a command for that too, STEP.
さて、命令がそれのためにもある、STEP。

For x = 1 to 10 step 2
  Print x
  Wait 100
Next x
Wait key

Just stick the STEP command after the FOR command and it increments ‘x’ by the number specified each time.
ちょっと FOR命令の後で STEP命令を入れなさい、そして、それは各時 指定される数によって『x』を増加させる。
You can also use decimals in STEP.
あなたは、STEPに小数を使うこともできる。

For x# = 1 to 10 step 0.1
  Print x#
  Wait 100
Next x#
Wait key

Just remember though, if your variable is going to end up having float values PUT THE # ON THE END OF IT!!!!
あなたの変数がフロート値に#をそれの終わりに置かせることになりそうであるならば、しかしちょっと覚えていなさい!!!!

よくやった!
You have now gone through the basics of DBPro.
あなたは、今はDBProの基本を通り抜けた。
You are well on your way to being able to write some decent programs by yourself.
あなたは、一人で若干の適切なプログラムを記述することができることへ行く途中で健康である。
If there’s something you still don’t get take the time to go back and look at this tutorial again.
あなたがまだ得ない何かがあるならば、戻って、もう一度このチュートリアルを見る時間をかけなさい。

Summary of Commands

命令の概要

· DO and LOOP - When the computer reaches LOOP it jumps back to DO
DO と LOOP - コンピュータがLOOPに達するとき、後ろにDOにジャンプする

· REPEAT and UNTIL statement - When the computer reaches _UNTIL, if the statement is false it loops back to _REPEAT.
REPATとUNTILステートメント-コンピュータが_UNTILに着くとき、もし命令文が間違っているならば、それはREPEATへループになる。

· WHILE statement and ENDWHILE - When the computer reaches ENDWHILE it loops to while and starts again only if the statement is true.
While命令文、そして、ENDWHILE - コンピュータがENDWHILEに着くとき、命令文が真実の場合だけ、それは再び間とスタートにループになる。

· FOR var = num1 to num2 and NEXT var - Var starts off as num1 when the computer reaches FOR and when the computer reaches NEXT it loops back to FOR and increments the variable.
FOR 変数= num2と次のバールまでnum1 NEXT 変数- バールはコンピュータが手を伸ばすnum1として出発する、そして、コンピュータはいつのためにそれが輪になる次に着く、そして、変数を増加させる。
It only continues while var is less or equal to num2.
バールがより少ないかnum2と等しい間、それは続けるだけである。
Use in conjunction with the STEP command.
ステップ命令と連携した使用。

· STEP num - Put after the FOR command to make the increments in the FOR-NEXT loop the size of num.
STEP num- 置くFOR-NEXTの増加を数字のサイズをループしなさいという命令。

· EXIT - Exits any loop.
出口- どんなループでも出る。

· INC var[,num] - Increments by one, or if a num is given, by the num.
INCバール[num]-1による増加、または、数字によって、numが与えられるならば。

· DEC var[,num] - Decrements by one, or if a num is given, by the num.
DECバール[num]-1による減少、または、数字によって、numが与えられるならば。

· WAIT time - Pauses the program for the specified number of milliseconds.
待ち時間-ミリ秒の指定された数のために、プログラムを中断する。

· ESCAPEKEY(), SPACEKEY(), CONTROLKEY(), SHIFTKEY(), RETURNKEY() - They return 1 if the relevant keys are pressed.
ESCAPEKEY()、SPACEKEY()、CONTROLKEY()、SHIFTKEY()、RETURNKEY()-関連したキーが押されるならば、それは1を返す。

· DISABLE ESCAPEKEY - This turns the default function of the escape button off, quitting the program.
ESCAPEKEYを働かなくしなさい-これは脱出ボタンのデフォルト関数をオフにする。そして、プログラムをやめる。脱出する方法を考えなさい、無ければ無限ループになる。

· END - This ends the program.
END - これはプログラムを終える。

End of Section Tasks

セクション終わりの作業

l Write a program that draws strips of colour using the LINE command going from left to right, that gradually change colour from red to blue, using one of the loops.
左からループのうちの1つを用いて赤から青に徐々に色を変える正当へ行っているライン命令を使っている色の帯を引き出すプログラムを記述しなさい。

l Write a program that draws a gradient from red to blue to green using the same method as before.
前の通り同じ方法を使用している緑に赤から青まで勾配を得るプログラムを記述しなさい。

l Expand on the last two tasks to get some really cool effects.
若干の本当にクールな結果を得るために、最後の2つの仕事を詳述しなさい。