This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,8 @@
“For” loops are used to make some block of code be iterated a number of times, setting a variable or parameter to a monotonically increasing integer value for each execution of the block of code. Common extensions of this allow other counting patterns or iterating over abstract structures other than the integers.
For this task, show how two loops may be nested within each other, with the number of iterations performed by the inner for loop being controlled by the outer for loop. Specifically print out the following pattern by using one for loop nested in another:
<pre>*
**
***
****
*****</pre>

View file

@ -0,0 +1,2 @@
---
note: Iteration

View file

@ -0,0 +1,6 @@
FOR i TO 5 DO
TO i DO
print("*")
OD;
print(new line)
OD

View file

@ -0,0 +1,8 @@
BEGIN {
for(i=1; i < 6; i++) {
for(j=1; j <= i; j++ ) {
printf "*"
}
print
}
}

View file

@ -0,0 +1,7 @@
var str:String = "";
for (var i:int = 1; i <= 5; i++) {
for (var j:int = 1; j <= i; j++)
str += "*";
trace(str);
str = "";
}

View file

@ -0,0 +1,6 @@
for I in 1..5 loop
for J in 1..I loop
Put("*");
end loop;
New_Line;
end loop;

View file

@ -0,0 +1,6 @@
for i in 0 to 6
for j in 0 to i
Write('*')
end
WriteLn()
end

View file

@ -0,0 +1,7 @@
PROC main()
DEF i, j
FOR i := 1 TO 5
FOR j := 1 TO i DO WriteF('*')
WriteF('\n')
ENDFOR
ENDPROC

View file

@ -0,0 +1,8 @@
set x to return
repeat with i from 1 to 5
repeat with j from 1 to i
set x to x & "*"
end repeat
set x to x & return
end repeat
return x

View file

@ -0,0 +1,7 @@
"
*
**
***
****
*****
"

View file

@ -0,0 +1,13 @@
Gui, Add, Edit, vOutput r5 w100 -VScroll ; Create an Edit-Control
Gui, Show ; Show the window
Loop, 5 ; loop 5 times
{
Loop, %A_Index% ; A_Index contains the Index of the current loop
{
output .= "*" ; append an "*" to the output var
GuiControl, , Output, %Output% ; update the Edit-Control with the new content
Sleep, 500 ; wait some(500ms) time, [just to show off]
}
Output .= (A_Index = 5) ? "" : "`n" ; append a new line to the output if A_Index is not "5"
}
Return ; End of auto-execution section

View file

@ -0,0 +1,6 @@
for i = 1 to 5
for j = 1 to i
print "*";
next j
print
next i

View file

@ -0,0 +1,6 @@
10 FOR i = 1 TO 5
20 FOR j = 1 TO i
30 PRINT "*";
40 NEXT j
50 PRINT
60 NEXT i

View file

@ -0,0 +1,6 @@
FOR I% = 1 TO 5
FOR J% = 1 TO I%
PRINT"*";
NEXT
PRINT
NEXT

View file

@ -0,0 +1,11 @@
star_triangle: { <- { { "*" << } iter times "\n" << } -> times }
main: { 10 star_triangle }
*
**
***
****
*****
******
*******
********
*********

View file

@ -0,0 +1,12 @@
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
for /l %%i in (1,1,5) do (
SET line=
for /l %%j in (1,1,%%i) do (
SET line=!line!*
)
ECHO !line!
)
ENDLOCAL

View file

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

View file

@ -0,0 +1,9 @@
0:?i
& whl
' ( !i+1:~>5:?i
& 0:?k
& whl'(!k+1:~>!i:?k&put$"*")
& put$\n
)
&
);

View file

@ -0,0 +1,8 @@
>>+++++++[>++++++[>+<-]<-] place * in cell 3
+++++[>++[>>+<<-]<-]<< place \n in cell 4
+++++[ set outer loop count
[>+ increment inner counter
>[-]>[-]<<[->+>+<<]>>[-<<+>>]<< copy inner counter
>[>>.<<-]>>>.<<< print line
<<-] end inner loop
] end outer loop

View file

@ -0,0 +1,6 @@
1.to 5, { i |
1.to i, { j |
print "*"
}
print "\n"
}

View file

@ -0,0 +1,5 @@
for(int i = 1; i <= 5; ++i) {
for(int j = 1; j <= i; j++)
std::cout << "*";
std::cout << std::endl;
}

View file

@ -0,0 +1,6 @@
int i, j;
for (i = 1; i <= 5; i++) {
for (j = 1; j <= i; j++)
putchar('*');
puts("");
}

View file

@ -0,0 +1,29 @@
Asterisks Omelette.
This recipe prints a triangle of asterisks.
Ingredients.
5 eggs
1 onion
1 potato
42 ml water
10 ml olive oil
1 garlic
Method.
Put eggs into the mixing bowl.
Fold onion into the mixing bowl.
Put eggs into the mixing bowl.
Add garlic into the mixing bowl.
Fold eggs into the mixing bowl.
Chop onion.
Put onion into the mixing bowl.
Fold potato into the mixing bowl.
Put olive oil into the mixing bowl.
Mash potato.
Put water into the mixing bowl.
Mash potato until mashed.
Chop onion until choped.
Pour contents of the mixing bowl into the baking dish.
Serves 1.

View file

@ -0,0 +1,3 @@
(doseq [i (range 5), j (range (inc i))]
(print "*")
(if (= i j) (println)))

View file

@ -0,0 +1,6 @@
<cfloop index = "i" from = "1" to = "5">
<cfloop index = "j" from = "1" to = "#i#">
*
</cfloop>
< br />
</cfloop>

View file

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

View file

@ -0,0 +1,4 @@
(loop for i from 1 upto 5 do
(loop for j from 1 upto i do
(write-char #\*))
(write-line ""))

View file

@ -0,0 +1,4 @@
(dotimes (i 5)
(dotimes (j (+ i 1))
(write-char #\*))
(terpri))

View file

@ -0,0 +1,6 @@
(do ((i 1 (+ i 1)))
((> i 5))
(do ((j 1 (+ j 1)))
((> j i))
(write-char #\*))
(terpri))

View file

@ -0,0 +1,16 @@
import std.stdio: write, writeln;
void main() {
for (int i; i < 5; i++) {
for (int j; j <= i; j++)
write("*");
writeln();
}
writeln();
foreach (i; 0 .. 5) {
foreach (j; 0 .. i + 1)
write("*");
writeln();
}
}

View file

@ -0,0 +1,9 @@
number i, j
for (i = 1; i <= 5; i++)
{
for (j = 1; j <= i; j++)
{
Result( "*" )
}
Result( "\n" )
}

View file

@ -0,0 +1,7 @@
var i, j : Integer;
for i := 1 to 5 do begin
for j := 1 to i do
Print('*');
PrintLn('');
end;

View file

@ -0,0 +1,4 @@
for( i = 1 : 5 ){
for( j = 1 : i ) io.write( '*' )
io.writeln()
}

View file

@ -0,0 +1,4 @@
main() {
for (var i = 0; i < 5; i++)
print(i);
}

View file

@ -0,0 +1,14 @@
program LoopFor;
{$APPTYPE CONSOLE}
var
i, j: Integer;
begin
for i := 1 to 5 do
begin
for j := 1 to i do
Write('*');
Writeln;
end;
end.

View file

@ -0,0 +1,6 @@
for width in 1..5 {
for _ in 1..width {
print("*")
}
println()
}

View file

@ -0,0 +1,8 @@
str string;
for ( i int to 5 )
str = "";
for ( j int to i )
str += "*";
end
SysLib.writeStdout(str);
end

View file

@ -0,0 +1,6 @@
open console
loop m n | n < m = loop' n 0 $ writen "" $ loop m (n+1)
| else = ()
where loop' m n | n <= m = write "*" $ loop' m (n+1)
| else = ()

View file

@ -0,0 +1,6 @@
for i = 1 to 5 do
for j = 1 to i do
puts(1, "*") -- Same as "puts(1, {'*'})"
end for
puts(1, "\n") -- Same as "puts(1, {'\n'})"
end for

View file

@ -0,0 +1,2 @@
1[$6-][$[$]["*"1-]#%"
"1+]#%

View file

@ -0,0 +1 @@
5 [1,b] [ [ "*" write ] times nl ] each

View file

@ -0,0 +1,14 @@
class ForLoops
{
public static Void main ()
{
for (Int i := 1; i <= 5; ++i)
{
for (Int j := 1; j <= i; ++j)
{
Env.cur.out.print ("*")
}
Env.cur.out.printLine ("")
}
}
}

View file

@ -0,0 +1,14 @@
class ForLoops
{
public static Void main ()
{
(1..5).each |i|
{
(1..i).each |j|
{
Env.cur.out.print ("*")
}
Env.cur.out.printLine ("")
}
}
}

View file

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

View file

@ -0,0 +1,5 @@
: limit_example
15 1 do r> r@ dup rot >r drop \ Bring limit on stack
. \ And print it
loop ;
\ Gforth and JSForth all work, SP-Forth brakes (different 'for' implementation?)

View file

@ -0,0 +1,33 @@
C WARNING: This program is not valid ANSI FORTRAN 77 code. It uses
C one nonstandard character on the line labelled 5001. Many F77
C compilers should be okay with it, but it is *not* standard.
PROGRAM FORLOOP
INTEGER I, J
DO 20 I = 1, 5
DO 10 J = 1, I
C Print the asterisk.
WRITE (*,5001) '*'
10 CONTINUE
C Print a newline.
WRITE (*,5000) ''
20 CONTINUE
STOP
5000 FORMAT (A)
C Standard FORTRAN 77 is completely incapable of completing a
C WRITE statement without printing a newline. If you wanted to
C write this program in valid F77, you would have to come up with
C a creative way of printing varying numbers of asterisks in a
C single write statement.
C
C The dollar sign at the end of the format is a nonstandard
C character. It tells the compiler not to print a newline. If you
C are actually using FORTRAN 77, you should figure out what your
C particular compiler accepts. If you are actually using Fortran
C 90 or later, you should replace this line with the commented
C line that follows it.
5001 FORMAT (A, $)
C5001 FORMAT (A, ADVANCE='NO')
END

View file

@ -0,0 +1,6 @@
DO i = 1, 5
DO j = 1, i
WRITE(*, "(A)", ADVANCE="NO") "*"
END DO
WRITE(*,*)
END DO

View file

@ -0,0 +1,4 @@
integer :: i
integer, dimension(10) :: v
forall (i=1:size(v)) v(i) = i

View file

@ -0,0 +1,12 @@
for i in [1 .. 5] do
for j in [1 .. i] do
Print("*");
od;
Print("\n");
od;
# *
# **
# ***
# ****
# *****

View file

@ -0,0 +1,10 @@
pattern = ""
for(i = 1; i <= 5; i += 1)
{
for(j = 1; j <= i; j += 1)
{
pattern += "*"
}
pattern += "#"
}
show_message(pattern)

View file

@ -0,0 +1,6 @@
10 FOR I = 1 TO 5
20 FOR J = 1 TO I
30 PRINT "*";
40 NEXT J
50 PRINT
60 NEXT I

View file

@ -0,0 +1,6 @@
for i = 1 to 5
for j = 1 to i
print "*";
next
print
next

View file

@ -0,0 +1,12 @@
package main
import "fmt"
func main() {
for i := 1; i <= 5; i++ {
for j := 1; j <= i; j++ {
fmt.Printf("*")
}
fmt.Printf("\n")
}
}

View file

@ -0,0 +1,6 @@
for(i in (1..6)) {
for(j in (1..i)) {
print '*'
}
println ()
}

View file

@ -0,0 +1,7 @@
import Control.Monad
main = do
forM_ [1..5] $ \i -> do
forM_ [1..i] $ \j -> do
putChar '*'
putChar '\n'

View file

@ -0,0 +1,3 @@
import Data.List (inits)
main = mapM_ putStrLn $ tail $ inits $ replicate 5 '*'

View file

@ -0,0 +1,6 @@
for (i in 1...6) {
for(j in 0...i) {
Sys.print('*');
}
Sys.println('');
}

View file

@ -0,0 +1,6 @@
DO i = 1, 5
DO j = 1, i
WRITE(APPend) "*"
ENDDO
WRITE() ' '
ENDDO

View file

@ -0,0 +1,7 @@
procedure main()
every i := 1 to 5 do {
every 1 to i do
writes("*")
write()
}
end

View file

@ -0,0 +1,4 @@
repeat with length running from 1 to 5:
repeat with N running from 1 to length:
say "*";
say line break;

View file

@ -0,0 +1,13 @@
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
)

View file

@ -0,0 +1,6 @@
for (int i = 0; i < 5; i++) {
for (int j = 0; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}

View file

@ -0,0 +1,7 @@
var i, j;
for (i = 1; i <= 5; i += 1) {
s = '';
for (j = 0; j < i; j += 1)
s += '*';
document.write(s + '<br>');
}

View file

@ -0,0 +1,9 @@
: cr "\n" . ; : dip swap '_ set execute _ ;
: nip swap drop ; : last -1 extract nip ;
: times
swap iota '_ set
do dup 'execute dip _ last 0 == if break then
loop drop ;
: concat "" join ;
'* 1 5 "2dup reshape concat . cr 1 +" times

View file

@ -0,0 +1,6 @@
for i = 1 to 5
for j = 1 to i
print "*";
next
print
next

View file

@ -0,0 +1,6 @@
1.to 5 do { i : INTEGER;
1.to i do { dummy : INTEGER;
'*'.print;
};
'\n'.print;
};

View file

@ -0,0 +1,2 @@
for [i 1 5] [repeat :i [type "*] (print)]
repeat 5 [repeat repcount [type "*] (print)]

View file

@ -0,0 +1,6 @@
for i=1,5 do
for j=1,i do
io.write("*")
end
io.write("\n")
end

View file

@ -0,0 +1,9 @@
define(`for',
`ifelse($#,0,``$0'',
`ifelse(eval($2<=$3),1,
`pushdef(`$1',$2)$5`'popdef(`$1')$0(`$1',eval($2+$4),$3,$4,`$5')')')')dnl
for(`x',`1',`5',`1',
`for(`y',`1',x,`1',
`*')
')

View file

@ -0,0 +1,7 @@
for i = (1:5)
output = [];
for j = (1:i)
output = [output '*'];
end
disp(output);
end

View file

@ -0,0 +1,3 @@
for i = (1:5)
disp(repmat('*',1,i));
end

View file

@ -0,0 +1,9 @@
for i in 1 to 5 do
(
line = ""
for j in 1 to i do
(
line += "*"
)
format "%\n" line
)

View file

@ -0,0 +1,7 @@
for i in [1..5]
s = "";
for j in [1..i]
s += "*";
endfor
player:tell(s);
endfor

View file

@ -0,0 +1,7 @@
FORLOOP
NEW I,J
FOR I=1:1:5 DO
.FOR J=1:1:I DO
..WRITE "*"
.WRITE !
QUIT

View file

@ -0,0 +1 @@
FOR I=1:1:5 FOR J=1:1:I WRITE "*" IF J=I W !

View file

@ -0,0 +1,6 @@
> for i to 5 do to i do printf( "*" ) end; printf( "\n" ) end;
*
**
***
****
*****

View file

@ -0,0 +1,6 @@
n=5;
For[i=1,i<=5,i++,
string="";
For[j=1,j<=i,j++,string=string<>"*"];
Print[string]
]

View file

@ -0,0 +1,5 @@
for i thru 5 do (
s: "",
thru i do s: sconcat(s, "*"),
print(s)
);

View file

@ -0,0 +1,22 @@
:- module loops_for.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
:- import_module int.
main(!IO) :-
int.fold_up(outer_loop_body, 1, 5, !IO).
:- pred outer_loop_body(int::in, io::di, io::uo) is det.
outer_loop_body(I, !IO) :-
int.fold_up(inner_loop_body, 1, I, !IO),
io.nl(!IO).
:- pred inner_loop_body(int::in, io::di, io::uo) is det.
inner_loop_body(_, !IO) :-
io.write_char('*', !IO).

View file

@ -0,0 +1,14 @@
MODULE For;
IMPORT InOut;
VAR
i, j: INTEGER;
BEGIN
FOR i := 1 TO 5 DO
FOR j := 1 TO i DO
InOut.Write('*');
END;
InOut.WriteLn
END
END For.

View file

@ -0,0 +1,12 @@
MODULE Stars EXPORTS Main;
IMPORT IO;
BEGIN
FOR i := 1 TO 5 DO
FOR j := 1 TO i DO
IO.Put("*");
END;
IO.Put("\n");
END;
END Stars.

View file

@ -0,0 +1,6 @@
for ($i = 1; $i <= 5; $i++) {
for ($j = 1; $j <= $i; $j++) {
echo '*';
}
echo "\n";
}

View file

@ -0,0 +1,6 @@
foreach (range(1, 5) as $i) {
foreach (range(1, $i) as $j) {
echo '*';
}
echo "\n";
}

View file

@ -0,0 +1,6 @@
for ($x = 1; $x <= 5; $x++) {
for ($y = 1; $y <= $x; $y++) {
print "*";
}
print "\n";
}

View file

@ -0,0 +1,6 @@
foreach (1..5) {
foreach (1..$_) {
print '*';
}
print "\n";
}

View file

@ -0,0 +1 @@
print ('*' x $_ . "\n") for 1..5

View file

@ -0,0 +1,3 @@
(for N 5
(do N (prin "*"))
(prinl) )

View file

@ -0,0 +1,5 @@
import sys
for i in xrange(5):
for j in xrange(i+1):
sys.stdout.write("*")
print

View file

@ -0,0 +1,2 @@
for i in range(1,6):
print '*' * i

View file

@ -0,0 +1,7 @@
for(i in 0:4) {
s <- ""
for(j in 0:i) {
s <- paste(s, "*", sep="")
}
print(s)
}

View file

@ -0,0 +1,7 @@
do i=1 to 5
s=''
do j=1 to i
s=s || '*'
end
say s
end

View file

@ -0,0 +1,7 @@
do i=1 for 5
s=''
do i
s=s'*'
end
say s
end

View file

@ -0,0 +1,4 @@
(for ([i (in-range 1 6)])
(for ([j i])
(display "*"))
(newline))

View file

@ -0,0 +1,6 @@
for i in 1..5
for j in 1..i
print "*"
end
puts
end

View file

@ -0,0 +1,6 @@
1.upto(5) do |i|
1.upto(i) do |j|
print "*"
end
puts
end

View file

@ -0,0 +1,7 @@
5.times do |i|
# i goes from 0 to 4
(i+1).times do
print "*"
end
puts
end

View file

@ -0,0 +1,10 @@
i = 1
loop do
j = 1
loop do
print "*"
break if (j += 1) > i
end
puts
break if (i += 1) > 5
end

View file

@ -0,0 +1 @@
puts (1..5).map { |i| "*" * i }

View file

@ -0,0 +1,20 @@
class MAIN is
-- from, to, step
for!(once init:INT, once to:INT, once inc:INT):INT is
i ::= init;
loop while!( i <= to );
yield i;
i := i + inc;
end;
end;
main is
i, j :INT;
loop i := for!(1, 5, 1); -- 1.upto!(5)
loop j := for!(1, i, 1); -- 1.upto!(i)
#OUT + "*";
end;
#OUT + "\n";
end;
end;
end;

View file

@ -0,0 +1,5 @@
for (i <- 1 to 5) {
for (j <- 1 to i)
print("*")
println
}

Some files were not shown because too many files have changed in this diff Show more