Loops/Continue: Difference between revisions

From Rosetta Code
Content added Content deleted
(added perl and python)
(Ada)
Line 2: Line 2:
1, 2, 3, 4, 5
1, 2, 3, 4, 5
6, 7, 8, 9, 10
6, 7, 8, 9, 10

=={{header|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.

<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;
</ada>


=={{header|C}}==
=={{header|C}}==

Revision as of 00:09, 22 April 2008

Task
Loops/Continue
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.

<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; </ada>

C

Translation of: C++

<c>for(int i = 1;i <= 10; i++){

  printf("%d", i);
  if(i % 5 == 0){
     printf("\n");
     continue;
  }
  printf(", ");

}</c>

C++

Translation of: Java

<cpp>for(int i = 1;i <= 10; i++){

  cout << i;
  if(i % 5 == 0){
     cout << endl;
     continue;
  }
  cout << ", ";

}</cpp>

Java

<java>for(int i = 1;i <= 10; i++){

  System.out.print(i);
  if(i % 5 == 0){
     System.out.println();
     continue;
  }
  System.out.print(", ");

}</java>

Perl

<perl>foreach (1..10) {

   print $_;
   if ($_ % 5 == 0) {
       print "\n";
       next;
   }
   print ', ';

}</perl>

Python

<python>line = "" for i in xrange(1,11):

   line += str(i)
   if i % 5 == 0:
       print line
       line = ""
       continue
   line += ", "</python>

UnixPipes

yes \ | cat -n | head -n 10 | xargs -n 5 echo | tr ' ' ,