Loops/For

From Rosetta Code
Task
Loops/For
You are encouraged to solve this task according to the task description, using any language you may know.

Specifically print out the following pattern by using one for loop nested in another:

*
**
***
****
*****

Ada

<ada>for I in 1..5 loop

  for J in 1..I loop
     Put("*");
  end loop;
  New_Line;

end loop;</ada>

BASIC

Works with: QuickBasic version 4.5

<qbasic>for i = 1 to 5

  for j = 1 to i
     print "*";
  next j
  print

next i</qbasic>

Befunge

1>:5`#@_:>"*",v
         | :-1<
 ^+1,+5+5<

C

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

 for (j = 1; j <= i; j++)
   putchar('*');
 puts("");

}</c>

ColdFusion

Remove the leading space from the line break tag.

With tags:

<cfloop index = "i" from = "1" to = "5">
  <cfloop index = "j" from = "1" to = "#i#">
    *
  </cfloop>
  < br />
</cfloop>

With script:

<cfscript>
  for( i = 1; i <= 5; i++ )
  {
    for( j = 1; j <= i; j++ )
    {
      writeOutput( "*" );
    }
    writeOutput( "< br />" );
  }
</cfscript>

Common Lisp

<lisp>(loop for i from 1 upto 5 do

 (loop for j from 1 upto i do
   (write-char #\*))
 (write-line ""))</lisp>

D

<d>for(int i = 0; i < 5; i++) {

 for(int j = 0; j <= i; j++)
   writef("*") ;
 writefln() ;

}</d> Foreach Range Statement since D2.003 <d>foreach(i ; 0..5) {

 foreach(j ; 0..i+1)
   writef("*") ;
 writefln() ;

}</d>

Forth

: triangle ( n -- )
  1+ 1 do
    cr i 0 do [char] * emit loop
  loop ;
5 triangle

Fortran

Works with: Fortran version 90 and later
DO i = 1, 5
  DO j = 1, i
    WRITE(*, "(A)", ADVANCE="NO") "*"
  END DO
  WRITE(*,*)
END DO

Haskell

import Control.Monad

main = do
  forM_ [1..5] $ \i -> do
    forM_ [1..i] $ \j -> do
      putChar '*'
    putChar '\n'

J

J is array-oriented, so there is very little need for loops. For example, one could satisfy this task this way:

  ]\ '*****'

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
        for_i. 1 + i. y do.
             z =. ''

             for. 1 + i. i do.
                  z=. z,'*'
             end. 

             z 1!:2 ] 2 
         end.

        i.0 0
   )

But you would never see J code like this.

Java

<java>for (int i = 0; i < 5; i++) {

  for (int j = 0; j <= i; j++) {
     System.out.print("*");
  }
  System.out.println();

}</java>

JavaScript

for (var i=1; i<=5; i++) {
  s = "";
  for (var j=0; j<i; j++)
    s += '*';
  print(s);
}

for [i 1 5] [repeat :i [type "*] (print)]
repeat 5 [repeat repcount [type "*] (print)]

MAXScript

for i in 1 to 5 do
(
    line = ""
    for j in 1 to i do
    (
        line += "*"
    )
    format "%\n" line
)

OCaml

<ocaml>for i = 1 to 5 do

 for j = 1 to i do
   print_string "*"
 done;
 print_newline ()

done</ocaml>

Pascal

<pascal> program stars(output);

var

 i, j: integer;

begin

 for i := 1 to 5 do
   begin
     for j := 1 to i do
       write('*');
     writeln
   end

end. </pascal>

Perl

<perl>foreach (1..5) {

 foreach (1..$_) {
   print '*';
 }
 print "\n";

}</perl>

Pop11

lvars i, j;
for i from 1 to 5 do
    for j from 1 to i do
        printf('*','%p');
    endfor;
    printf('\n')
endfor;

Python

<python>import sys for i in xrange(5):

   for j in xrange(i+1):
       sys.stdout.write("*")
   print</python>

Note that we have a constraint to use two for loops, which leads to non-idiomatic Python. If that constraint is dropped we can use the following, more idiomatic Python solution: <python>for i in range(1,6):

   print '*' * i

</python>

Ruby

for i in 1..5 do

  for j in 1..i do
     print "*"
  end
  puts ""

end or 1.upto(5) do |i|

  1.upto(i) do |j|
     print "*"
  end
  puts ""

end

Scheme

<scheme>(do ((i 1 (+ i 1)))

   ((> i 5))
   (do ((j 1 (+ j 1)))
       ((> j i))
       (display "*"))
   (newline))</scheme>

UnixPipes

yes \ | cat -n | while read n ; do
  yes \* | head -n $n | xargs -n $n echo
done

Visual Basic .NET

Works with: Visual Basic version 2002
       For x As Integer = 0 To 4
           For y As Integer = 0 To x
               Console.Write("*")
           Next
           Console.WriteLine()
       Next