Loop/Continue
From Rosetta Code
Programming Task
This is a programming task. It lays out a problem which Rosetta Code users are encouraged to solve, using languages they know.
1, 2, 3, 4, 5 6, 7, 8, 9, 10
Contents |
[edit] Ada
Ada has no continue reserved word, nor does it need one. The continue reserved word is only syntactic sugar for operations that can be achieved without it as in the following example.
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;
[edit] C
Translation of: C++
for(int i = 1;i <= 10; i++){ printf("%d", i); if(i % 5 == 0){ printf("\n"); continue; } printf(", "); }
[edit] C++
Translation of: Java
for(int i = 1;i <= 10; i++){ cout << i; if(i % 5 == 0){ cout << endl; continue; } cout << ", "; }
[edit] ColdFusion
Remove the leading space from the line break tag.
<cfscript>
for( i = 1; i <= 10; i++ )
{
writeOutput( i );
if( 0 == i % 5 )
{
writeOutput( "< br />" );
continue;
}
writeOutput( "," );
}
</cfscript>
[edit] D
for(int i = 1;i <= 10; i++){ writef(i); if(i % 5 == 0){ writefln(); continue; } writef(", "); }
[edit] Fortran
Works with: Fortran version 90 and later
DO i = 1, 10
IF (i == 5) THEN
WRITE(*, "(I1)") i
ELSE IF (i == 10) THEN
WRITE(*, "(I2)") i
ELSE
WRITE(*, "(I1,A)", ADVANCE="NO") i, ", "
ENDIF
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:
_2}."1'lq<, >'8!:2>:i.2 5
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 ] 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
)
Though it's rare to see J code like this.
[edit] Java
for(int i = 1;i <= 10; i++){ System.out.print(i); if(i % 5 == 0){ System.out.println(); continue; } System.out.print(", "); }
[edit] MAXScript
for i in 1 to 10 do
(
format "%" i
if mod i 5 == 0 then
(
format "\n"
continue
) continue
format ", "
)
[edit] Perl
foreach (1..10) { print $_; if ($_ % 5 == 0) { print "\n"; next; } print ', '; }
[edit] Pop11
lvars i;
for i from 1 to 10 do
printf(i, '%p');
if i rem 5 = 0 then
printf('\n');
nextloop;
endif;
printf(', ')
endfor;
[edit] Python
line = "" for i in xrange(1,11): line += str(i) if i % 5 == 0: print line line = "" continue line += ", "
[edit] UnixPipes
yes \ | cat -n | head -n 10 | xargs -n 5 echo | tr ' ' ,

