"For 루프"의 두 판 사이의 차이

잔글 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
 
(사용자 3명의 중간 판 39개는 보이지 않습니다)
1번째 줄: 1번째 줄:
{{소문자}}
[[분류: for 루프]]
{{다른뜻|포문}}
;for
;for
;for문
;for문


==Bash==
==Bash==
[[category: Bash]]
{{참고|Bash for}}
<source lang='bash'>
[[분류:Bash]]
for i in 2 3 5
<syntaxhighlight lang='bash'>
do
for (( i = 1; i < 4; i++ )); do
  echo "i = $i"
echo "i = $i"
done
done
# i = 1
# i = 2
# i = 2
# i = 3
# i = 3
# i = 5
</syntaxhighlight>
</source>
<syntaxhighlight lang='bash'>
<source lang='bash'>
for i in $(seq 1 3); do
for i in {1..3}
echo "i = $i"
do
done
  echo "i = $i"
</syntaxhighlight>
<syntaxhighlight lang='bash'>
for i in `seq 1 3`; do
echo "i = $i"
done
</syntaxhighlight>
<syntaxhighlight lang='bash'>
for i in {1..3}; do
echo "i = $i"
done
</syntaxhighlight>
<syntaxhighlight lang='bash'>
i=1
while [ $i -lt 4 ]; do
echo "i = $i"
i=`expr $i + 1`
done
done
# i = 1
</syntaxhighlight>
# i = 2
<syntaxhighlight lang='bash'>
# i = 3
for i in 2 3 5; do
</source>
echo "i = $i"
<source lang='bash'>
for (( c=1; c<5; c++ ))
do
  echo "i = $c"
done
done
# i = 1
# i = 2
# i = 2
# i = 3
# i = 4
</source>
<source lang='bash'>
for i in {1..5..2}
do
  echo "i = $i"
done
# i = 1
# i = 3
# i = 3
# i = 5
# i = 5
</source>
</syntaxhighlight>


==C==
==C==
[[category: C]]
[[분류:C]]
<source lang='c'>
<syntaxhighlight lang='c'>
for(int i=1; i<=10; i++) {
for(int i=1; i<=10; i++) {
sum += i;
sum += i;
}
}
</source>
</syntaxhighlight>
 
==C++==
*[[#C|same as C]]
 
==C#==
[[category: Csharp]]
*[[#C|same as C]]


==Cmd==
==CMD==
[[category:cmd]]
[[분류:Cmd]]
<source lang='bash'>
<syntaxhighlight lang='bash'>
(@echo off & for /L %i in (1,1,4) do echo i = %i) & echo on
(@echo off & for /L %i in (1,1,4) do echo i = %i) & echo on
REM i = 1
REM i = 1
66번째 줄: 62번째 줄:
REM i = 3
REM i = 3
REM i = 4
REM i = 4
</source>
</syntaxhighlight>
 
==Java==
[[분류:Java]]
<syntaxhighlight lang='Java'>
for(int i=1; i<=10; i++) {
sum += i;
}
</syntaxhighlight>
 
==JavaScript==
[[분류:JavaScript]]
{{참고|자바스크립트 for}}
<syntaxhighlight lang='JavaScript'>
for(var i=1; i<=10; i++) {
sum += i;
}
</syntaxhighlight>


==PHP==
==PHP==
[[category: PHP]]
[[분류:PHP]]
<source lang='php'>
{{참고|PHP for}}
<syntaxhighlight lang='php'>
$sum = 0;
for($i=1; $i<=10; $i++) {
for($i=1; $i<=10; $i++) {
$sum += $i;
$sum += $i;
}
}
</source>
echo $sum;
</syntaxhighlight>
 
==Python==
[[분류:Python]]
{{참고|파이썬 for}}
<syntaxhighlight lang='Python'>
sum = 0
for i in range(10):
sum += i
print sum
</syntaxhighlight>
 
==Perl==
[[category: Perl]]
{{참고|펄 for}}
<syntaxhighlight lang='Perl'>
my $sum = 0;
for $i (1..4) {
$sum += $i;
}
printf("$sum\n");
# 10
</syntaxhighlight>
 
==Ruby==
[[분류:Ruby]]
<syntaxhighlight lang='Ruby'>
sum = 0
for i in 1..4
sum += i
end
puts sum
# 10
</syntaxhighlight>
<syntaxhighlight lang='Ruby'>
sum = 0
for i in 1...5
sum += i
end
puts sum
# 10
</syntaxhighlight>
<syntaxhighlight lang='Ruby'>
sum = 0
(1..4).each { |i| sum += i }
puts sum
# 10
</syntaxhighlight>
<syntaxhighlight lang='Ruby'>
sum = 0
(1..4).each do |i|
    sum += i
end
puts sum
# 10
</syntaxhighlight>
<syntaxhighlight lang='Ruby'>
sum = 0
[1, 2, 3, 4].each { |i| sum += i }
puts sum
# 10
</syntaxhighlight>


==Visual Basic==
==Visual Basic==
[[category: VB]]
[[분류:VB]]
<source lang='vb'>
<syntaxhighlight lang='vb'>
FOR I = 1 TO 10
FOR I = 1 TO 10
SUM = SUM + I
SUM = SUM + I
NEXT I
NEXT I
</source>
</syntaxhighlight>


==같이 보기==
==같이 보기==
*[[while]]
* [[while]]
*[[extended for]]
* [[foreach]]
*[[Iterator 패턴]]
* [[Iterator 패턴]]
*[[자바 키워드]]
* [[자바 키워드]]
 
* [[str_repeat]]
==참고 자료==
*http://eqcode.com/wiki/For
 
[[분류: 프로그래밍]]

2020년 11월 2일 (월) 02:31 기준 최신판

  다른 뜻에 대해서는 포문 문서를 참조하십시오.
for
for문

1 Bash[ | ]

for (( i = 1; i < 4; i++ )); do
	echo "i = $i"
done
# i = 1
# i = 2
# i = 3
for i in $(seq 1 3); do
	echo "i = $i"
done
for i in `seq 1 3`; do
	echo "i = $i"
done
for i in {1..3}; do
	echo "i = $i"
done
i=1
while [ $i -lt 4 ]; do
	echo "i = $i"
	i=`expr $i + 1`
done
for i in 2 3 5; do
	echo "i = $i"
done
# i = 2
# i = 3
# i = 5

2 C[ | ]

for(int i=1; i<=10; i++) {
	sum += i;
}

3 CMD[ | ]

(@echo off & for /L %i in (1,1,4) do echo i = %i) & echo on
REM i = 1
REM i = 2
REM i = 3
REM i = 4

4 Java[ | ]

for(int i=1; i<=10; i++) {
	sum += i;
}

5 JavaScript[ | ]

for(var i=1; i<=10; i++) {
	sum += i;
}

6 PHP[ | ]

$sum = 0;
for($i=1; $i<=10; $i++) {
	$sum += $i;
}
echo $sum;

7 Python[ | ]

sum = 0
for i in range(10):
	sum += i
print sum

8 Perl[ | ]

my $sum = 0;
for $i (1..4) {
	$sum += $i;
}
printf("$sum\n");
# 10

9 Ruby[ | ]

sum = 0
for i in 1..4
	sum += i
end
puts sum
# 10
sum = 0
for i in 1...5
	sum += i
end
puts sum
# 10
sum = 0
(1..4).each { |i| sum += i }
puts sum
# 10
sum = 0
(1..4).each do |i|
    sum += i
end
puts sum
# 10
sum = 0
[1, 2, 3, 4].each { |i| sum += i }
puts sum
# 10

10 Visual Basic[ | ]

FOR I = 1 TO 10
	SUM = SUM + I
NEXT I

11 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}