Loops/Continue: Difference between revisions
No edit summary |
|||
Line 6: | Line 6: | ||
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. |
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. |
||
<ada> |
<lang ada> |
||
with Ada.Text_Io; use Ada.Text_Io; |
with Ada.Text_Io; use Ada.Text_Io; |
||
Line 20: | Line 20: | ||
end loop; |
end loop; |
||
end Loop_Continue; |
end Loop_Continue; |
||
</ |
</lang> |
||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
[[ALGOL 68]] 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: |
[[ALGOL 68]] 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: |
||
Line 42: | Line 42: | ||
=={{header|C}}== |
=={{header|C}}== |
||
{{trans|C++}} |
{{trans|C++}} |
||
<c>for(int i = 1;i <= 10; i++){ |
<lang c>for(int i = 1;i <= 10; i++){ |
||
printf("%d", i); |
printf("%d", i); |
||
if(i % 5 == 0){ |
if(i % 5 == 0){ |
||
Line 49: | Line 49: | ||
} |
} |
||
printf(", "); |
printf(", "); |
||
}</ |
}</lang> |
||
=={{header|C++}}== |
=={{header|C++}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
<cpp>for(int i = 1;i <= 10; i++){ |
<lang cpp>for(int i = 1;i <= 10; i++){ |
||
cout << i; |
cout << i; |
||
if(i % 5 == 0){ |
if(i % 5 == 0){ |
||
Line 60: | Line 60: | ||
} |
} |
||
cout << ", "; |
cout << ", "; |
||
}</ |
}</lang> |
||
=={{header|ColdFusion}}== |
=={{header|ColdFusion}}== |
||
Line 78: | Line 78: | ||
=={{header|D}}== |
=={{header|D}}== |
||
<d>for(int i = 1;i <= 10; i++){ |
<lang d>for(int i = 1;i <= 10; i++){ |
||
writef(i); |
writef(i); |
||
if(i % 5 == 0){ |
if(i % 5 == 0){ |
||
Line 85: | Line 85: | ||
} |
} |
||
writef(", "); |
writef(", "); |
||
}</ |
}</lang> |
||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
{{works with|Fortran|90 and later}} |
{{works with|Fortran|90 and later}} |
||
Line 125: | Line 125: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
<java>for(int i = 1;i <= 10; i++){ |
<lang java>for(int i = 1;i <= 10; i++){ |
||
System.out.print(i); |
System.out.print(i); |
||
if(i % 5 == 0){ |
if(i % 5 == 0){ |
||
Line 132: | Line 132: | ||
} |
} |
||
System.out.print(", "); |
System.out.print(", "); |
||
}</ |
}</lang> |
||
=={{header|MAXScript}}== |
=={{header|MAXScript}}== |
||
Line 166: | Line 166: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
<perl>foreach (1..10) { |
<lang perl>foreach (1..10) { |
||
print $_; |
print $_; |
||
if ($_ % 5 == 0) { |
if ($_ % 5 == 0) { |
||
Line 173: | Line 173: | ||
} |
} |
||
print ', '; |
print ', '; |
||
}</ |
}</lang> |
||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
<php>for ($i = 1; $i <= 10; $i++) { |
<lang php>for ($i = 1; $i <= 10; $i++) { |
||
echo $i; |
echo $i; |
||
if ($i % 5 == 0) { |
if ($i % 5 == 0) { |
||
Line 183: | Line 183: | ||
} |
} |
||
echo ', '; |
echo ', '; |
||
}</ |
}</lang> |
||
=={{header|Pop11}}== |
=={{header|Pop11}}== |
||
Line 199: | Line 199: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
<python>for i in xrange(1,11): |
<lang python>for i in xrange(1,11): |
||
if i % 5 == 0: |
if i % 5 == 0: |
||
print i |
print i |
||
continue |
continue |
||
print i, ",",</ |
print i, ",",</lang> |
||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
<ruby>for i in 1..10 do |
<lang ruby>for i in 1..10 do |
||
print i |
print i |
||
if i % 5 == 0 then |
if i % 5 == 0 then |
||
Line 213: | Line 213: | ||
end |
end |
||
print ', ' |
print ', ' |
||
end</ |
end</lang> |
||
=={{header|UnixPipes}}== |
=={{header|UnixPipes}}== |
Revision as of 15:38, 3 February 2009
You are encouraged to solve this task according to the task description, using any language you may know.
Show the following output using one loop.
1, 2, 3, 4, 5 6, 7, 8, 9, 10
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.
<lang ada> 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; </lang>
ALGOL 68
ALGOL 68 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:
FOR i FROM 1 TO 10 DO print ((i, IF i MOD 5 = 0 THEN new line ELSE "," FI )) OD
Output:
+1, +2, +3, +4, +5 +6, +7, +8, +9, +10
C
<lang c>for(int i = 1;i <= 10; i++){
printf("%d", i); if(i % 5 == 0){ printf("\n"); continue; } printf(", ");
}</lang>
C++
<lang cpp>for(int i = 1;i <= 10; i++){
cout << i; if(i % 5 == 0){ cout << endl; continue; } cout << ", ";
}</lang>
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>
D
<lang d>for(int i = 1;i <= 10; i++){
writef(i); if(i % 5 == 0){ writefln(); continue; } writef(", ");
}</lang>
Fortran
DO i = 1, 10 IF (MOD(i, 5) == 0) THEN WRITE(*, "(I0)") i ELSE WRITE(*, "(I0,A)", ADVANCE="NO") i, ", " ENDIF END DO
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.
Java
<lang java>for(int i = 1;i <= 10; i++){
System.out.print(i); if(i % 5 == 0){ System.out.println(); continue; } System.out.print(", ");
}</lang>
MAXScript
for i in 1 to 10 do ( format "%" i if mod i 5 == 0 then ( format "\n" continue ) continue format ", " )
Modula-3
Modula-3 defines the keyword RETURN as an exception, but when it is used with no arguments it works just like continue in C.
Note, however, that RETURN only works inside a procedure or a function procedure; use EXIT otherwise.
Module code and imports are omitted.
FOR i := 1 TO 10 DO IO.PutInt(i); IF i MOD 5 = 0 THEN IO.Put("\n"); RETURN; END; IO.Put(", "); END;
Perl
<lang perl>foreach (1..10) {
print $_; if ($_ % 5 == 0) { print "\n"; next; } print ', ';
}</lang>
PHP
<lang php>for ($i = 1; $i <= 10; $i++) {
echo $i; if ($i % 5 == 0) { echo "\n"; continue; } echo ', ';
}</lang>
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;
Python
<lang python>for i in xrange(1,11):
if i % 5 == 0: print i continue print i, ",",</lang>
Ruby
<lang ruby>for i in 1..10 do
print i if i % 5 == 0 then puts next end print ', '
end</lang>
UnixPipes
yes \ | cat -n | head -n 10 | xargs -n 5 echo | tr ' ' ,
Visual Basic .NET
For i = 1 To 10 Console.Write(i) If i Mod 5 = 0 Then Console.WriteLine() Else Console.Write(", ") End If Next