48 lines
1.7 KiB
Plaintext
48 lines
1.7 KiB
Plaintext
![]() |
\b;Instruction \c;while\n;
|
||
|
The instruction \c;while () {}\n; is used to repeat a set of instructions several times.
|
||
|
|
||
|
\b;Basic use
|
||
|
The most frequent use of \c;while\n; consists in repeating a set of instructions again and again. In order to achieve this, write \c;while (true) {}\n; and put the instructions to be repeated in braces \c;{}\n;. As an example, here is a program that repeats again and again the following actions:
|
||
|
o look for a spider,
|
||
|
o turn towards it,
|
||
|
o shoot.
|
||
|
\c;
|
||
|
\s; while (true)
|
||
|
\s; {
|
||
|
\s; item = \l;radar\u cbot\radar;(AlienSpider);
|
||
|
\s; \l;turn\u cbot\turn;(direction(item.position));
|
||
|
\s; \l;fire\u cbot\fire;(1);
|
||
|
\s; }
|
||
|
\n;
|
||
|
Just execute this program once, and it will kill all spiders around it.
|
||
|
|
||
|
\b;For specialists
|
||
|
Syntax :
|
||
|
\s;\c;while ( condition )
|
||
|
\s;{
|
||
|
\s; \n;Instructions ...\c;
|
||
|
\s;}
|
||
|
\n;
|
||
|
This instruction allows you to perform the instructions inside the \l;block\u cbot\bloc; several times.
|
||
|
|
||
|
Be careful not to confuse the instruction \c;while( ) { }\n; with the instruction \c;\l;do\u cbot\do; { } while( );\n;; the latter tests the condition only after the instructions in the block have been performed a first time.
|
||
|
|
||
|
\t;\c;condition\n;
|
||
|
The instructions in the block are performed over and over again, as long as the \l;condition\u cbot\cond; is true.
|
||
|
|
||
|
Here is an example :
|
||
|
\s;\c;int i = 0;
|
||
|
\s;while ( i < 10 )
|
||
|
\s;{
|
||
|
\s; \n;Instructions ...\c;
|
||
|
\s; i = i+1;
|
||
|
\s;}
|
||
|
\n;
|
||
|
\t;Attention
|
||
|
Do not put a \l;semicolon\u cbot\term; at the end of the line \c;while ( )\n;.
|
||
|
|
||
|
The instructions \c;\l;break\u cbot\break;\n; and \c;\l;continue\u cbot\continue;\n; can be useful inside a block following the instruction \c;while { }\n;.
|
||
|
|
||
|
\t;See also
|
||
|
\l;Instructions\u cbot;, \l;types\u cbot\type; and \l;categories\u cbot\category;.
|