Loops/Break

Revision as of 19:12, 5 June 2009 by rosettacode>Mwn3d (Created task with Java and BASIC)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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.

Task
Loops/Break
You are encouraged to solve this task according to the task description, using any language you may know.

BASIC

Works with: QuickBasic version 4.5

<lang qbasic>do

   a = int(rnd * 20)
   print a
   if a = 10 then exit loop
   b = int(rnd * 20)
   print b

loop</lang>


Java

<lang java>while(true){

   int a = (int)(Math.random()*20);
   System.out.println(a);
   if(a == 10) break;
   int b = (int)(Math.random()*20);
   System.out.println(b);

}</lang>