Difference between revisions of "Do while/ja"

From Second Life Wiki
Jump to navigation Jump to search
m (Do-while/ja moved to Do while/ja: consistency)
m
 
(2 intermediate revisions by 2 users not shown)
Line 5: Line 5:
|statement=do while
|statement=do while
|statement_header
|statement_header
|statement_desc=いくつかのステートメントは空にできます。do...while loop はわずかにwhileもしくはfor loopより速く、while もしくは for loop よりも少ないバイト数でできます
|statement_desc=いくつかのステートメントは空にできます。do...while loop はわずかにwhileもしくはfor loopより速く、while もしくは for loop よりも少ないバイト数でできます(しかし、スクリプトがmonoでコンパイルされた場合には、この実行速度とバイト数の違いはありません)
|statement_title=do {{LSL Param|loop}} while ({{LSL Param|condition}});
|statement_title=do {{LSL Param|loop}} while ({{LSL Param|condition}});
|p1_name=loop
|p1_name=loop
Line 12: Line 12:
|spec
|spec
|caveats
|caveats
|examples=<pre>//1 から 5 までカウント
|examples=<source lang="lsl2">//1 から 5 までカウント
default
{
    state_entry()
    {
        integer olf;  // Without applied value an integer will be defaulted to zero.
        do
            llSay(0, (string) (++olf));      // Increment before the while condition
        while (olf < 5);                    // On the first pass/loop olf = 1
    }
}</source><source lang="lsl2">//0 から 4 までカウント
default
default
{
{
Line 19: Line 29:
         integer olf;
         integer olf;
         do
         do
             llSay(0, (string) (++olf));
             llSay(0, (string)olf);   // olf is still equal to zero at first iteration
         while(olf<5);
         while (++olf < 5);           // Increments then does the while-test
     }
     }
}</pre>
}</source><source lang="lsl2">//0から4まで数え、ループ中にコメントを追加する(ブロック文のデモ)
<pre>//0 から 4 までカウント
default
default
{
{
Line 30: Line 39:
         integer olf;
         integer olf;
         do
         do
             llSay(0, (string)olf);
        {  // Curly brackets are required since there is more than one statement within the do-loop
         while((++olf)<5);
             llSay(0, (string)olf);   
            llSay(0, "looping");
        }
         while (++olf < 5);
    }
}</source><source lang="lsl2">
// do/whileループがwhileループよりも直接役立つ実用的な例:
// センサーイベントには常に入力データが含まれるため、「do」には常に処理するものがあります。
    sensor(integer num)
    { 
        if (num > 12)
            num = 12;
        do
        { 
            // --num decrements num before using it to pick up a detected avatar's name. Thus we retrieve #11 through #0
            gNameList += [llGetSubString(llDetectedName(--num), 0, 23)];    //sometimes avatar names are too long for dialog display
            gKeyList  += [llDetectedKey(num)];    //we will dialog select avatar by name, but still need their key
                                                  //even if their name has not been truncated above
        }  while (num > 0);
        llDialog(llGetOwner(), "Choose an avatar.", gNameList, gDlgChan); //channel is pre-defined when llSensor is triggered
     }
     }
}</pre>
</source>
|helpers
|helpers
|also_header
|also_header
Line 43: Line 71:
|mode
|mode
|deprecated
|deprecated
|cat1=Conditional/ja
|cat1=Conditional
|cat2=Flow Control/ja
|cat2=
|cat3
|cat3
|cat4
|cat4
}}
}}

Latest revision as of 08:56, 26 September 2023

do loop while (condition);

•  loop 一度実行すると、conditionの間実行します。
•  condition もしconditionを実行してtrueなら、再び舞い戻って繰り返しloopを実行します。


いくつかのステートメントは空にできます。do...while loop はわずかにwhileもしくはfor loopより速く、while もしくは for loop よりも少ないバイト数でできます(しかし、スクリプトがmonoでコンパイルされた場合には、この実行速度とバイト数の違いはありません)

詳細

条件の種類
条件
integer 0ではない場合は真。
float 0ではない場合は真。
string 文字列の長さが0ではない場合は真。
key keyが有効でNULL_KEYではない場合のみ真。
vector vectorがZERO_VECTORではない場合は真。
rotation rotationがZERO_ROTATIONではない場合は真。
list listの長さが0ではない場合は真。正しい動作は、Monoでコンパイルされたスクリプトのみで見られ、LSOでコンパイルされたスクリプトは誤って false になります。BUG-230728


//1 から 5 までカウント
default
{
    state_entry()
    {
        integer olf;   // Without applied value an integer will be defaulted to zero.
        do
            llSay(0, (string) (++olf));       // Increment before the while condition 
        while (olf < 5);                    // On the first pass/loop olf = 1
    }
}
//0 から 4 までカウント
default
{
    state_entry()
    {
        integer olf;
        do
            llSay(0, (string)olf);    // olf is still equal to zero at first iteration
        while (++olf < 5);            // Increments then does the while-test
    }
}
//0から4まで数え、ループ中にコメントを追加する(ブロック文のデモ)
default
{
    state_entry()
    {
        integer olf;
        do
        {   // Curly brackets are required since there is more than one statement within the do-loop
            llSay(0, (string)olf);    
            llSay(0, "looping");
        }
        while (++olf < 5);
    }
}
// do/whileループがwhileループよりも直接役立つ実用的な例:
// センサーイベントには常に入力データが含まれるため、「do」には常に処理するものがあります。
    sensor(integer num)
    {   
        if (num > 12)
            num = 12;
        do
        {   
            // --num decrements num before using it to pick up a detected avatar's name. Thus we retrieve #11 through #0
            gNameList += [llGetSubString(llDetectedName(--num), 0, 23)];    //sometimes avatar names are too long for dialog display
            gKeyList  += [llDetectedKey(num)];    //we will dialog select avatar by name, but still need their key
                                                  //even if their name has not been truncated above
        }  while (num > 0);
        llDialog(llGetOwner(), "Choose an avatar.", gNameList, gDlgChan);  //channel is pre-defined when llSensor is triggered
    }