This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,5 @@
Show the following output using one loop.
1, 2, 3, 4, 5
6, 7, 8, 9, 10
Try to achieve the result by forcing the next iteration within the loop upon a specific condition, if your language allows it.

View file

@ -0,0 +1,2 @@
---
note: Iteration

View file

@ -0,0 +1,9 @@
FOR i FROM 1 TO 10 DO
print ((i,
IF i MOD 5 = 0 THEN
new line
ELSE
","
FI
))
OD

View file

@ -0,0 +1,10 @@
BEGIN {
for(i=1; i <= 10; i++) {
printf("%d", i)
if ( i % 5 == 0 ) {
print
continue
}
printf(", ")
}
}

View file

@ -0,0 +1,13 @@
with Ada.Text_Io; use Ada.Text_Io;
procedure Loop_Continue is
begin
for I in 1..10 loop
Put(Integer'Image(I));
if I mod 5 = 0 then
New_Line;
else
Put(",");
end if;
end loop;
end Loop_Continue;

View file

@ -0,0 +1,8 @@
foreach i 1..10 {
print (i)
if ((i % 5) == 0) {
println()
continue
}
print (", ")
}

View file

@ -0,0 +1,5 @@
Loop, 10 {
Delimiter := (A_Index = 5) || (A_Index = 10) ? "`n":", "
Index .= A_Index . Delimiter
}
MsgBox %Index%

View file

@ -0,0 +1,4 @@
FOR i% = 1 TO 10
PRINT ; i% ;
IF i% MOD 5 = 0 PRINT ELSE PRINT ", ";
NEXT

View file

@ -0,0 +1,8 @@
for(int i = 1;i <= 10; i++){
cout << i;
if(i % 5 == 0){
cout << endl;
continue;
}
cout << ", ";
}

View file

@ -0,0 +1,8 @@
for(int i = 1;i <= 10; i++){
printf("%d", i);
if(i % 5 == 0){
printf("\n");
continue;
}
printf(", ");
}

View file

@ -0,0 +1,5 @@
(doseq
[n (range 1 11)]
(do
(print n)
(if (= (rem n 5) 0) (print "\n") (print ", "))))

View file

@ -0,0 +1,12 @@
<cfscript>
for( i = 1; i <= 10; i++ )
{
writeOutput( i );
if( 0 == i % 5 )
{
writeOutput( "< br />" );
continue;
}
writeOutput( "," );
}
</cfscript>

View file

@ -0,0 +1,16 @@
(do ((i 1 (1+ i))) ((> i 10))
(format t "~a~:[, ~;~%~]" i (zerop (mod i 5))))
(do ((i 1 (1+ i))) ((> i 10))
(write i)
(when (zerop (mod i 5))
(terpri)
(go end))
(write-string ", ")
end)
(do ((i 1 (1+ i))) ((> i 10))
(write i)
(if (zerop (mod i 5))
(terpri)
(write-string ", ")))

View file

@ -0,0 +1,12 @@
(loop for i from 1 to 10
do (write i)
if (zerop (mod i 5)) do (terpri)
else do (write-string ", "))
(loop for i from 1 to 10 do
(block continue
(write i)
(when (zerop (mod i 5))
(terpri)
(return-from continue))
(write-string ", ")))

View file

@ -0,0 +1,12 @@
import std.stdio;
void main() {
foreach (i; 1 .. 11) {
write(i);
if (i % 5 == 0) {
writeln();
continue;
}
write(", ");
}
}

View file

@ -0,0 +1,10 @@
var i : Integer;
for i := 1 to 10 do begin
Print(i);
if i mod 5 = 0 then begin
PrintLn('');
continue;
end;
Print(', ');
end;

View file

@ -0,0 +1,15 @@
program DoLoop(output);
var
i: integer;
begin
for i := 1 to 10 do
begin
write(i);
if i mod 5 = 0 then
begin
writeln;
continue;
end;
write(', ');
end;
end.

View file

@ -0,0 +1,8 @@
open console imperative
loop n | n > 10 = ()
| else = rec write (show n) f `seq` loop (n+1)
where f | n % 5 == 0 = "\r\n"
| else = ", "
loop 1

View file

@ -0,0 +1,8 @@
open console imperative
loop [] = ()
loop (x::xs) = rec (write << show) x c `seq` loop xs
where c | x % 5 == 0 = "\r\n"
| else = ", "
loop [1..10]

View file

@ -0,0 +1,11 @@
include std\console.e --only for any_key to make running command window easier on windows
for i = 1 to 10 do
if remainder(i,5) = 0 then
printf(1, "%d\n", i)
else
printf(1,"%d, ", i)
continue
end if
end for
any_key()

View file

@ -0,0 +1,18 @@
include std\console.e --only for any_key to make running command window easier on windows
for i = 1 to 10 do
if remainder(i,5) = 0 then
switch i do
case 10 then
printf(1,"%d ",i)
break --new to euphoria 4.0.0+
case else
printf(1,"%d\n", i)
end switch
else
printf(1,"%d, ", i)
continue --new to euphoria 4.0.0+
end if
end for
any_key()

View file

@ -0,0 +1,4 @@
1 10 [a,b] [
[ number>string write ]
[ 5 mod 0 = "\n" ", " ? write ] bi
] each

View file

@ -0,0 +1,17 @@
class LoopsContinue
{
public static Void main ()
{
for (Int i := 1; i <= 10; ++i)
{
Env.cur.out.print (i)
if (i % 5 == 0)
{
Env.cur.out.printLine ("")
continue
}
Env.cur.out.print (", ")
}
Env.cur.out.printLine ("")
}
}

View file

@ -0,0 +1,5 @@
: main
11 1 do
i dup 1 r.
5 mod 0= if cr else [char] , emit space then
loop ;

View file

@ -0,0 +1,8 @@
do i = 1, 10
write(*, '(I0)', advance='no') i
if ( mod(i, 5) == 0 ) then
write(*,*)
cycle
end if
write(*, '(A)', advance='no') ', '
end do

View file

@ -0,0 +1,67 @@
C WARNING: This program is not valid ANSI FORTRAN 77 code. It uses
C one nonstandard character on the line labelled 5001. Many F77
C compilers should be okay with it, but it is *not* standard.
C
C It is also worth noting that FORTRAN 77 uses the command CONTINUE,
C but not in the semantic, looping sense of the word. In FORTRAN,
C CONTINUE means "do absolutely nothing." It is a placeholder. If
C anything, it means "continue to the next line."
C
C Python does the same thing with `pass`; C and its family of
C languages, with `{/* do nothing */}`. Write CONTINUE when you need
C to write something but have nothing to write.
C
C This page on Rosetta Code is about a very different "continue"
C statement that tells a loop to go back to the beginning. In
C FORTRAN, we use (you guessed it!) a GOTO to accomplish this.
PROGRAM CONTINUELOOP
INTEGER I
DO 10 I = 1, 10
C Is it five or ten?
IF (MOD(I, 5) .EQ. 0) THEN
C If it is, write a newline and no comma.
WRITE (*,5000) I
C Continue the loop; that is, skip to the end of the loop.
GOTO 10
ENDIF
C Write I with a comma and no newline.
WRITE (*,5001) I
C Again, in this case, CONTINUE is completely unrelated to the
C semantic, looping sense of the word.
10 CONTINUE
STOP
C This will print an integer and a newline (no comma).
5000 FORMAT (I3)
C Standard FORTRAN 77 is completely incapable of completing a
C WRITE statement without printing a newline. If you want to print
C five integers in standard code, you have to do something like
C this:
C
C FORMAT (I3, ',', I3, ',', I3, ',', I3, ',', I3)
C
C Writing `1, 2, 3, 4, 5` and then `6, 7, 8, 9, 10` to that format
C would produce the following two lines:
C
C 1, 2, 3, 4, 5
C 6, 7, 8, 9, 10
C
C However, this code exists to demonstrate continuing a FORTRAN 77
C loop and not to demonstrate how to get around its rigidity about
C newlines.
C
C The dollar sign at the end of the format is a nonstandard
C character. It tells the compiler not to print a newline. If you
C are actually using FORTRAN 77, you should figure out what your
C particular compiler accepts. If you are actually using Fortran
C 90 or later, you should replace this line with the commented
C line that follows it.
5001 FORMAT (I3, ',', $)
C5001 FORMAT (I3, ',', ADVANCE='NO')
END

View file

@ -0,0 +1,10 @@
for i in [1 .. 11] do
if RemInt(i, 5) = 0 then
Print(i, "\n");
continue;
fi;
Print(i, ", ");
od;
# 1, 2, 3, 4, 5
# 6, 7, 8, 9, 10

View file

@ -0,0 +1,7 @@
for(i = 1; i <= 10; i += 1)
{
show_message(string(i))
i += 1
if(i <= 10)
continue
}

View file

@ -0,0 +1,14 @@
package main
import "fmt"
func main() {
for i := 1; i <= 10; i++ {
fmt.Printf("%d", i)
if i%5 == 0 {
fmt.Printf("\n")
continue
}
fmt.Printf(", ")
}
}

View file

@ -0,0 +1,8 @@
for (i in 1..10) {
print i
if (i % 5 == 0) {
println ()
continue
}
print ', '
}

View file

@ -0,0 +1,5 @@
import Control.Monad (forM)
main = forM [1..10] out
where
out x | x `mod` 5 == 0 = print x
| otherwise = (putStr . (++", ") . show) x

View file

@ -0,0 +1,7 @@
DO i = 1, 10
IF( MOD(i, 5) == 1 ) THEN
WRITE(Format="i3") i
ELSE
WRITE(APPend, Format=" ',', i3 ") i
ENDIF
ENDDO

View file

@ -0,0 +1,9 @@
procedure main()
every writes(x := 1 to 10) do {
if x % 5 = 0 then {
write()
next
}
writes(", ")
}
end

View file

@ -0,0 +1 @@
every writes(x := 1 to 10, if x % 5 = 0 then "\n" else ", ")

View file

@ -0,0 +1 @@
_2}."1'lq<, >'8!:2>:i.2 5

View file

@ -0,0 +1,15 @@
3 : 0 ] 10
z=.''
for_i. 1 + i.y do.
z =. z , ": i
if. 0 = 5 | i do.
z 1!:2 ]2
z =. ''
continue.
end.
z =. z , ', '
end.
i.0 0
)

View file

@ -0,0 +1,8 @@
for(int i = 1;i <= 10; i++){
System.out.print(i);
if(i % 5 == 0){
System.out.println();
continue;
}
System.out.print(", ");
}

View file

@ -0,0 +1,10 @@
var output = "";
for (var i = 1; i <= 10; i++) {
output += i;
if (i % 5 == 0) {
print(output);
output = "";
continue;
}
output += ", ";
}

View file

@ -0,0 +1,4 @@
for i =1 to 10
if i mod 5 <>0 then print i; ", "; else print i
next i
end

View file

@ -0,0 +1,4 @@
1.to 10 do { i : INTEGER;
i.print;
(i % 5 = 0).if { '\n'.print; } else { ','.print; };
};

View file

@ -0,0 +1,8 @@
for i = 1, 10 do
io.write( i )
if i % 5 == 0 then
io.write( "\n" )
else
io.write( ", " )
end
end

View file

@ -0,0 +1 @@
disp([1:5; 6:10])

View file

@ -0,0 +1 @@
disp(reshape([1:10],5,2)')

View file

@ -0,0 +1,7 @@
for i = 1:10
printf(' %2d', i);
if ( mod(i, 5) == 0 )
printf('\n');
continue
end
end

View file

@ -0,0 +1,10 @@
for i in 1 to 10 do
(
format "%" i
if mod i 5 == 0 then
(
format "\n"
continue
) continue
format ", "
)

View file

@ -0,0 +1,10 @@
s = "";
for i in [1..10]
s += tostr(i);
if (i % 5 == 0)
player:tell(s);
s = "";
continue;
endif
s += ", ";
endfor

View file

@ -0,0 +1,8 @@
for i from 1 to 10 do
printf( "%d", i );
if irem( i, 5 ) = 0 then
printf( "\n" );
next
end if;
printf( ", " )
end do:

View file

@ -0,0 +1,3 @@
for i to 10 do
printf( "%d%s", i, `if`( irem( i, 5 ) = 0, "\n", ", " ) )
end do:

View file

@ -0,0 +1,10 @@
tmp = "";
For[i = 1, i <= 10, i++,
tmp = tmp <> ToString[i];
If[Mod[i, 5] == 0,
tmp = tmp <> "\n";
,
tmp = tmp <> ", ";
];
];
Print[tmp]

View file

@ -0,0 +1,13 @@
/* There is no "continue" in Maxima, the easiest is using a "if" instead */
block(
[s: ""],
for n thru 10 do (
s: sconcat(s, n),
if mod(n, 5) = 0 then (
ldisp(s),
s: ""
) else (
s: sconcat(s, ", ")
)
)
)$

View file

@ -0,0 +1,9 @@
string s; s := "";
for i = 1 step 1 until 10:
if i mod 5 = 0:
s := s & decimal i & char10;
else:
s := s & decimal i & ", "
fi; endfor
message s;
end

View file

@ -0,0 +1 @@
primarydef x mod y = (x-y*floor(x/y)) enddef;

View file

@ -0,0 +1,8 @@
FOR i := 1 TO 10 DO
IO.PutInt(i);
IF i MOD 5 = 0 THEN
IO.Put("\n");
RETURN;
END;
IO.Put(", ");
END;

View file

@ -0,0 +1,8 @@
for ($i = 1; $i <= 10; $i++) {
echo $i;
if ($i % 5 == 0) {
echo "\n";
continue;
}
echo ', ';
}

View file

@ -0,0 +1,8 @@
foreach (1..10) {
print $_;
if ($_ % 5 == 0) {
print "\n";
next;
}
print ', ';
}

View file

@ -0,0 +1,9 @@
foreach (1..10) {
print $_;
if ($_ % 5 == 0) {
print "\n";
goto MYLABEL;
}
print ', ';
MYLABEL:
}

View file

@ -0,0 +1,5 @@
(for I 10
(print I)
(if (=0 (% I 5))
(prinl)
(prin ", ") ) )

View file

@ -0,0 +1,5 @@
for i in xrange(1,11):
if i % 5 == 0:
print i
continue
print i, ",",

View file

@ -0,0 +1,10 @@
for(i in 1:10)
{
cat(i)
if(i %% 5 == 0)
{
cat("\n")
next
}
cat(", ")
}

View file

@ -0,0 +1,10 @@
/*REXX program to illustrate DO loop with an ITERATE (continue). */
do j=1 to 10
call charout , j", "
if j//5==0 then do
say
iterate
end
end /*j*/

View file

@ -0,0 +1,14 @@
#lang racket
;; Idiomatic way
(for ([i (in-range 1 11)])
(if (= (remainder i 5) 0)
(printf "~a~n" i)
(printf "~a, " i)))
;; Forces a skip, but not idiomatic because
;; the logic is less obvious
(for ([i (in-range 1 11)]
#:unless (and (= (remainder i 5) 0)
(printf "~a~n" i)))
(printf "~a, " i))

View file

@ -0,0 +1,8 @@
for i in 1..10 do
print i
if i % 5 == 0 then
puts
next
end
print ', '
end

View file

@ -0,0 +1,3 @@
(1..10).each do |i| ...
1.upto(10) do |i| ...
10.times do |n| i=n+1; ...

View file

@ -0,0 +1 @@
1.upto(10) {|i| print "%d%s" % [i, i%5==0 ? "\n" : ", "]}

View file

@ -0,0 +1,13 @@
class MAIN is
main is
i:INT;
loop i := 1.upto!(10);
#OUT + i;
if i%5 = 0 then
#OUT + "\n";
else
#OUT + ", ";
end;
end;
end;
end;

View file

@ -0,0 +1,4 @@
for(i <- 1 to 10) {
print(i)
if (i%5==0) println() else print(", ")
}

View file

@ -0,0 +1,8 @@
(define (loop i)
(if (> i 10) 'done
(begin
(display i)
(cond ((zero? (modulo i 5))
(newline) (loop (+ 1 i)))
(else (display ", ")
(loop (+ 1 i)))))))

View file

@ -0,0 +1,8 @@
for {set i 1} {$i <= 10} {incr i} {
puts -nonewline $i
if {$i % 5 == 0} {
puts ""
continue
}
puts -nonewline ", "
}