Loops/Do-while

From Rosetta Code
Revision as of 03:13, 27 November 2008 by 128.97.245.4 (talk) (added common lisp and ruby)
Task
Loops/Do-while
You are encouraged to solve this task according to the task description, using any language you may know.

Start with a value at 0. Loop while value mod 6 is not equal to 0. Each time through the loop, add 1 to the value then print it. The loop must execute at least once.

Ada

<ada> Value : Integer := 0;

loop

  Value := Value + 1;
  Put (Value);
  exit when Value mod 6 = 0;

end loop; </ada> Here is an alternative version: <ada> for Value in 0..Integer'Last loop

  Put (Value);
  exit when Value mod 6 = 0;

end loop; </ada>

BASIC

Works with: QuickBasic version 4.5

<qbasic>a = 0 do

 a = a + 1
 print a

loop while a mod 6 <> 0</qbasic>

Befunge

0>1+:.v
 |%6: <
 @

C

<c>int val = 0; do{

  val++;
  printf("%d",val);

}while(val % 6 != 0);</c>

C++

<cpp>int val = 0; do{

  val++;
  cout << val << endl;

}while(val % 6 != 0);</cpp>

ColdFusion

<cfscript>
  value = 0;
  do
  {
    value += 1;
    writeOutput( value );
  } while( value % 6 != 0 );			
</cfscript>

Common Lisp

<lisp>(setq val 0) (loop do

 (incf val)
 (print val)
while (/= 0 (mod val 6)))</lisp>

D

<d>int val = 0; do{

 val++;
 writefln(val);

}while(val % 6 != 0);</d>

Forth

: do-until
  0
  begin 1+
        dup .
        dup 6 mod 0=
  until
  drop ;

Fortran

Works with: Fortran version 90 and later
INTEGER :: i = 0
DO 
  i = i + 1
  WRITE(*, *) i
  IF (MOD(i, 6) == 0) EXIT
END DO

J

J is array-oriented, so there is very little need for loops. For example, one could satisfy this task this way:

  ,. ([^:(0=6|])>:)^:a: 0

J does support loops for those times they can't be avoided (just like many languages support gotos for those time they can't be avoided).

   3 : 0 ] 0

         NB.  The 'st' in 'whilst' stands for 'skip test'

         whilst. 0 ~: 6 | y do.  
             y 1!:2 ]2 
             y =. y+1
         end.

        i.0 0
   )

Though it's rare to see J code like this.

Java

<java>int val = 0; do{

  val++;
  System.out.println(val);

}while(val % 6 != 0);</java>

JavaScript

<javascript>var val = 0; do {

 print(++val);

} while (val % 6);</javascript>

make "val 0
do.while [make "val :val + 1  print :val] [notequal? 0 modulo :val 6]
do.until [make "val :val + 1  print :val] [equal? 0 modulo :val 6]
to my.loop :n
  make "n :n + 1
  print :n
  if notequal? 0 modulo :n 6 [my.loop :n]
end
my.loop 0

MAXScript

a = 0
do
(
    print a
    a += 1
)
while mod a 6 != 0

OCaml

OCaml doesn't have a do-while loop, so we can just make a local loop: <ocaml>let rec loop i =

 let i = succ i in
 Printf.printf "%d\n" i;
 if i mod 6 <> 0 then
   loop i
 in
 loop 0</ocaml>

or implementing a generic do-while iterator with higher order function:

<ocaml>let do_while f p =

 let rec loop() =
   f();
   if p() then loop()
 in
 loop()

(** val do_while : (unit -> 'a) -> (unit -> bool) -> unit *) </ocaml>

<ocaml>let v = ref 0 in do_while (fun () -> incr v; Printf.printf "%d\n" !v)

        (fun () -> !v mod 6 <> 0)</ocaml>

The example above is the an imperative form, below is its functional counterpart: <ocaml>let do_while f p ~init =

 let rec loop v =
   let v = f v in
   if p v then loop v
 in
 loop init

do_while (fun v ->

           let v = succ v in
           Printf.printf "%d\n" v;
           (v))
        (fun v -> v mod 6 <> 0)
        ~init:0</ocaml>

Or in a very poor OCaml style, we can use an exception to exit a while loop: <ocaml>let v = ref 0 exception Exit_loop try while true do

 incr v;
 Printf.printf "%d\n" !v;
 if not(!v mod 6 <> 0) then
   raise Exit_loop;

done with Exit_loop -> ()</ocaml>

Pascal

<pascal> program countto6(output);

var

 i: integer;

begin

 i := 0;
 repeat
   i := i + 1;
   writeln(i)
 until i mod 6 = 0

end. </pascal>

Perl

<perl>$val = 0; do {

  $val++;
  print "$val\n";

} while ($val % 7 != 0);</perl>

Pop11

lvars val = 0;
while true do
   val + 1 -> val;
   printf(val, '%p\n');
   quitif(val rem 6 = 0);
endwhile;

Python

<python>val = 0 while true:

  val +=1
  print val
  if val % 6 == 0: break</python>

or repeat the body of the loop before a standard while. <python>val = 1 print val while val % 6 != 0:

  val += 1
  print val

</python>

Ruby

val = 0 begin

  val += 1
  puts val

end while val % 6 != 0