Loops/Break: Difference between revisions
< Loops
Content added Content deleted
No edit summary |
|||
Line 36: | Line 36: | ||
System.out.println(a); |
System.out.println(a); |
||
if(a == 10) break; |
if(a == 10) break; |
||
int |
int b = rand.nextInt(20); |
||
System.out.println(b); |
System.out.println(b); |
||
}</lang> |
}</lang> |
Revision as of 02:17, 6 June 2009
Loops/Break
You are encouraged to solve this task according to the task description, using any language you may know.
You are encouraged to solve this task according to the task description, using any language you may know.
Show a while loop which prints two random numbers (newly generated each loop) from 0 to 19 (inclusive). If the first number is 10, print it, stop the loop, and do not generate the second. Otherwise, loop forever.
BASIC
<lang qbasic>do
a = int(rnd * 20) print a if a = 10 then exit loop 'EXIT FOR works the same inside FOR loops b = int(rnd * 20) print b
loop</lang>
Forth
<lang forth>include random.fs
- main
begin 20 random dup . 10 <> while 20 random . repeat ;
\ use LEAVE to break out of a counted loop
- main
100 0 do i random dup . 10 = if leave then i random . loop ;
</lang>
Java
<lang java>import java.util.Random;
Random rand = new Random(); while(true){
int a = rand.nextInt(20); System.out.println(a); if(a == 10) break; int b = rand.nextInt(20); System.out.println(b);
}</lang>