Loop/For
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.
* ** *** **** *****
Contents |
[edit] Ada
for I in 1..5 loop for J in 1..I loop Put("*"); end loop; New_Line; end loop;
[edit] BASIC
Works with: QuickBasic version 4.5
FOR i = 1 TO 5 FOR j = 1 TO i PRINT "*"; NEXT j PRINT NEXT i
[edit] C
for (i = 1; i <= 5; i++) { for (j = 1; j <= i; j++) putchar('*'); puts(""); }
[edit] 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>
[edit] Common Lisp
(loop for i from 1 upto 5 do (loop for j from 1 upto i do (write-char #\*)) (write-line ""))
[edit] D
for(int i = 0; i < 5; i++) { for(int j = 0; j <= i; j++) writef("*") ; writefln() ; }
Foreach Range Statement since D2.003
foreach(i ; 0..5) { foreach(j ; 0..i+1) writef("*") ; writefln() ; }
[edit] Forth
: triangle ( n -- )
1+ 1 do
cr i 0 do [char] * emit loop
loop ;
5 triangle
[edit] 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
[edit] Haskell
import Control.Monad
main = do
forM_ [1..5] $ \i -> do
forM_ [1..i] $ \j -> do
putChar '*'
putChar '\n'
[edit] 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.
[edit] Java
for (int i = 0; i < 5; i++) { for (int j = 0; j <= i; j++) { System.out.print("*"); } System.out.println(); }
[edit] JavaScript
for (var i=1; i<=5; i++) {
s = "";
for (var j=0; j<i; j++)
s += '*';
print(s);
}
[edit] Logo
for [i 1 5] [repeat :i [type "*] (print)] repeat 5 [repeat repcount [type "*] (print)]
[edit] MAXScript
for i in 1 to 5 do
(
line = ""
for j in 1 to i do
(
line += "*"
)
format "%\n" line
)
[edit] OCaml
for i = 1 to 5 do for j = 1 to i do print_string "*" done; print_newline () done
[edit] 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.
[edit] Perl
foreach (1..5) { foreach (1..$_) { print '*'; } print "\n"; }
[edit] Pop11
lvars i, j;
for i from 1 to 5 do
for j from 1 to i do
printf('*','%p');
endfor;
printf('\n')
endfor;
[edit] Python
import sys for i in xrange(5): for j in xrange(i+1): sys.stdout.write("*") print
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:
for i in range(1,6): print '*' * i
[edit] Ruby
for i in 1..5 do for j in 1..i do print "*" end puts "" end
[edit] Scheme
(do ((i 1 (+ i 1))) ((> i 5)) (do ((j 1 (+ j 1))) ((> j i)) (display "*")) (newline))
[edit] UnixPipes
yes \ | cat -n | while read n ; do yes \* | head -n $n | xargs -n $n echo done
Categories: Programming Tasks | Iteration | Ada | BASIC | C | ColdFusion | Common Lisp | D | Forth | Fortran | Haskell | J | Java | JavaScript | Logo | MAXScript | OCaml | Pascal | Perl | Pop11 | Python | Ruby | Scheme | UnixPipes

