Loop/Do While

From Rosetta Code

Jump to: navigation, search

Programming Task
This is a programming task. It lays out a problem which Rosetta Code users are encouraged to solve, using languages they know.

Code examples should be formatted along the lines of one of the existing prototypes.
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.

Contents

[edit] Ada

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

Here is an alternative version:

 
for Value in 0..Integer'Last loop
   Put (Value);
   exit when Value mod 6 = 0;
end loop;
 

[edit] BASIC

Works with: QuickBasic version 4.5

a = 0
DO
  a = a + 1
  PRINT a
LOOP WHILE a MOD 6 <> 0

[edit] C

int val = 0;
do{
   val++;
   printf("%d",val);
}while(val % 6 != 0);

[edit] C++

int val = 0;
do{
   val++;
   cout << val << endl;
}while(val % 6 != 0);

[edit] ColdFusion

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

[edit] D

int val = 0;
do{
  val++;
  writefln(val);
}while(val % 6 != 0);

[edit] Forth

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

[edit] 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

[edit] 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.

[edit] Java

int val = 0;
do{
   val++;
   System.out.println(val);
}while(val % 6 != 0);

[edit] JavaScript

var val = 0;
do {
  print(++val);
} while (val % 6);

[edit] Logo

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

[edit] MAXScript

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

[edit] OCaml

OCaml doesn't have a do-while loop, so we can just make a local loop:

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

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

let do_while f p =
  let rec loop() =
    f();
    if p() then loop()
  in
  loop()
(** val do_while : (unit -> 'a) -> (unit -> bool) -> unit *)
 
let v = ref 0 in
do_while (fun () -> incr v; Printf.printf "%d\n" !v)
         (fun () -> !v mod 6 <> 0)

The example above is the an imperative form, below is its functional counterpart:

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

Or in a very poor OCaml style, we can use an exception to exit a while loop:

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

[edit] Pascal

 
program countto6(output);
 
var
  i: integer;
 
begin
  i := 0;
  repeat
    i := i + 1;
    writeln(i)
  until i mod 6 = 0
end.
 

[edit] Perl

$val = 0;
do {
   $val++;
   print "$val\n";
} while ($val % 7 != 0);

[edit] Pop11

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

[edit] Python

val = 0
while true:
   val +=1
   print val
   if val % 6 == 0: break

or repeat the body of the loop before a standard while.

val = 1
print val
while val % 6 != 0:
   val += 1
   print val
 
Personal tools