Loops/Break: Difference between revisions

From Rosetta Code
Content added Content deleted
(PowerShell)
(add JavaScript)
Line 162: Line 162:
System.out.println(b);
System.out.println(b);
}</lang>
}</lang>

=={{header|JavaScript}}==
<lang javascript>for (;;) {
var a = Math.floor(Math.random() * 20);
print(a);
if (a == 10)
break;
a = Math.floor(Math.random() * 20);
print(a);
}</lang>
The <code>print()</code> function is available in the [[Rhino]] JavaScript shell.


=={{header|Lisaac}}==
=={{header|Lisaac}}==

Revision as of 14:48, 7 October 2009

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

Show a loop which prints random numbers (each number newly generated each loop) from 0 to 19 (inclusive). If a number is 10, stop the loop after printing it, and do not generate any further numbers. Otherwise, generate and print a second random number before restarting the loop. If the number 10 is never generated as the first number in a loop, loop forever.

Ada

<lang Ada> with Ada.Text_IO; use Ada.Text_IO; with Ada.Numerics.Discrete_Random;

procedure Test_Loop_Break is

  type Value_Type is range 1..20;
  package Random_Values is new Ada.Numerics.Discrete_Random (Value_Type);
  use Random_Values;
  Dice : Generator;
  A, B : Value_Type;

begin

  loop
     A := Random (Dice);
     Put_Line (Value_Type'Image (A));
     exit when A = 10;
     B := Random (Dice);
     Put_Line (Value_Type'Image (B));
  end loop;

end Test_Loop_Break; </lang>

AutoHotkey

<lang AutoHotkey> Loop {

 Random, var, 0, 19
 output = %output%`n%var%
 If (var = 10)
   Break
 Random, var, 0, 19
 output = %output%`n%var%

} MsgBox % output </lang>

BASIC

Works with: QuickBasic version 4.5

<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>

C

<lang c>#include <stdlib.h>

  1. include <time.h>
  2. include <stdio.h>

int main() {

   int a, b;
   srand(time(NULL));
   while (1) {
       a = rand() % 20; /* not exactly uniformly distributed, but doesn't matter */
       printf("%d\n", a);
       if (a == 10) break;
       b = rand() % 20; /* not exactly uniformly distributed, but doesn't matter */
       printf("%d\n", b);
   }
   return 0;

}</lang>

Common Lisp

<lang lisp>(loop

   (setq a (random 20))
   (print a)
   (if (= a 10)
       (return))
   (setq b (random 20))
   (print b))</lang>

D

Works with: Tango

Using Mersenne twister;

<lang D>import tango.io.Stdout; import tango.math.random.Twister;

void main() {

   alias Twister.instance r;
   uint x;
   while (~1) {
       x = r.natural(20);
       Stdout (x) (" ");
       if (x == 10) break;
       Stdout (r.natural(20)).newline;
   }

}</lang>

E

<lang e>while (true) {

   def a := entropy.nextInt(20)
   print(a)
   if (a == 10) {
       println()
       break
   }
   println(" ", entropy.nextInt(20))

}</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>

Fortran

Works with: Fortran version 90 and later

<lang fortran>program Example

 implicit none
 real :: r
 integer :: a, b
 do
    call random_number(r)
    a = int(r * 20)
    write(*,*) a
    if (a == 10) exit
    call random_number(r)
    b = int(r * 20)
    write(*,*) b
 end do

end program Example</lang>

J

<lang J>loopexample=: verb define

  while.1 do.
     smoutput n=:?20
     if.10=n do.return.end.
     smoutput ?20
  end.

)</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>

JavaScript

<lang javascript>for (;;) {

 var a = Math.floor(Math.random() * 20);
 print(a);
 if (a == 10) 
   break;
 a = Math.floor(Math.random() * 20);
 print(a);

}</lang> The print() function is available in the Rhino JavaScript shell.

Lisaac

<lang Lisaac> Section Header

+ name := TEST_LOOP_BREAK;

Section Public

- main <- (

 + a, b : INTEGER;
 `srand(time(NULL))`;
 {
   a := `rand()`:INTEGER % 20; // not exactly uniformly distributed, but doesn't matter
   a.print;
   '\n'.print;
   a == 10
 }.until_do {
   b := `rand()`:INTEGER % 20; // not exactly uniformly distributed, but doesn't matter
   b.print;
   '\n'.print;
 }

); </lang>

M4

<lang M4> define(`randSeed',141592653)dnl define(`setRand',

  `define(`randSeed',ifelse(eval($1<10000),1,`eval(20000-$1)',`$1'))')dnl

define(`rand_t',`eval(randSeed^(randSeed>>13))')dnl define(`random',

  `define(`randSeed',eval((rand_t^(rand_t<<18))&0x7fffffff))randSeed')dnl

dnl define(`loopbreak',`define(`a',eval(random%20))`a='a ifelse(a,10,`',`define(`b',eval(random%20))`b='b loopbreak')')dnl dnl loopbreak </lang>

Output:

a=17
b=3
a=0
b=15
a=10

MOO

<lang moo>while (1)

 a = random(20) - 1;
 player:tell(a);
 if (a == 10)
   break;
 endif
 b = random(20) - 1;
 player:tell(b);

endwhile</lang>

OCaml

<lang ocaml># Random.self_init();; - : unit = ()

  1. while true do
   let a = Random.int 20 in
   print_int a;
   print_newline();
   if a = 10 then raise Exit;
   let b = Random.int 20 in
   print_int b;
   print_newline()
 done;;

15 18 2 13 10 Exception: Pervasives.Exit. </lang>

Octave

<lang octave>while(1)

 a = floor(unifrnd(0,20, 1));
 disp(a)
 if ( a == 10 )
   break
 endif
 b = floor(unifrnd(0,20, 1));
 disp(b)

endwhile</lang>

Perl

<lang perl>while (1) {

   my $a = int(rand(20));
   print "$a\n";
   if ($a == 10) {
       last;
   }
   my $b = int(rand(20));
   print "$b\n";

}</lang>

PHP

<lang php>while (true) {

   $a = rand(0,19);
   echo "$a\n";
   if ($a == 10)
       break;
   $b = rand(0,19);
   echo "$b\n";

}</lang>

PowerShell

<lang powershell>$r = New-Object Random for () {

   $n = $r.Next(20)
   Write-Host $n
   if ($n -eq 10) {
       break
   }
   Write-Host $r.Next(20)

}</lang>

Python

<lang python>import random

while True:

   a = random.randrange(20)
   print a
   if a == 10:
       break
   b = random.randrange(20)
   print b</lang>

R

Works with: R version 2.8.1

<lang R> while (FALSE != TRUE)

 {
 result1 <- sample(1:19,1,replace=TRUE)
 if (result1 == 10)
   {
   print(result1)
   break
   }
 result2 <- sample(1:19,1,replace=TRUE)
 cat(result1,result2,"\n")
 }

</lang>

Ruby

<lang ruby>loop do

   a = rand(20)
   puts a
   if a == 10
       break
   end
   b = rand(20)
   puts b

end</lang>

TI-89 BASIC

Local x
Loop
  rand(20)-1 → x
  Disp x                     © new line and text
  If x = 10 Then
    Exit
  EndIf
  Output 64, 50, rand(20)-1  © paint text to the right on same line
EndLoop

Tcl

<lang tcl>while true {

   set a [expr int(20*rand())]
   puts $a
   if {$a == 10} {
       break
   }
   set b [expr int(20*rand())]
   puts $b

}</lang>