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

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


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


==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>


==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
69번째 줄: 62번째 줄:
REM i = 3
REM i = 3
REM i = 4
REM i = 4
</source>
</syntaxhighlight>


==Java==
==Java==
[[category: Java]]
[[분류:Java]]
<source lang='Java'>
<syntaxhighlight lang='Java'>
for(int i=1; i<=10; i++) {
for(int i=1; i<=10; i++) {
sum += i;
sum += i;
}
}
</source>
</syntaxhighlight>


==JavaScript==
==JavaScript==
[[category: JavaScript]]
[[분류:JavaScript]]
<source lang='JavaScript'>
{{참고|자바스크립트 for}}
<syntaxhighlight lang='JavaScript'>
for(var i=1; i<=10; i++) {
for(var i=1; i<=10; i++) {
sum += i;
sum += i;
}
}
</source>
</syntaxhighlight>


==PHP==
==PHP==
[[category: PHP]]
[[분류:PHP]]
<source lang='php'>
{{참고|PHP for}}
<syntaxhighlight lang='php'>
$sum = 0;
$sum = 0;
for($i=1; $i<=10; $i++) {
for($i=1; $i<=10; $i++) {
95번째 줄: 90번째 줄:
}
}
echo $sum;
echo $sum;
</source>
</syntaxhighlight>


==Python==
==Python==
[[category: Python]]
[[분류:Python]]
<source lang='Python'>
{{참고|파이썬 for}}
<syntaxhighlight lang='Python'>
sum = 0
sum = 0
for i in range(10):
for i in range(10):
sum += i
sum += i
print sum
print sum
</source>
</syntaxhighlight>
 
==Perl==
[[category: Perl]]
{{참고|펄 for}}
<syntaxhighlight lang='Perl'>
my $sum = 0;
for $i (1..4) {
$sum += $i;
}
printf("$sum\n");
# 10
</syntaxhighlight>


==Ruby==
==Ruby==
[[category: Ruby]]
[[분류:Ruby]]
<source lang='Ruby'>
<syntaxhighlight lang='Ruby'>
sum = 0
sum = 0
for i in 1..4
for i in 1..4
115번째 줄: 123번째 줄:
puts sum
puts sum
# 10
# 10
</source>
</syntaxhighlight>
<source lang='Ruby'>
<syntaxhighlight lang='Ruby'>
sum = 0
sum = 0
for i in 1...5
for i in 1...5
123번째 줄: 131번째 줄:
puts sum
puts sum
# 10
# 10
</source>
</syntaxhighlight>
<source lang='Ruby'>
<syntaxhighlight lang='Ruby'>
sum = 0
sum = 0
(1..4).each { |i| sum += i }
(1..4).each { |i| sum += i }
puts sum
puts sum
# 10
# 10
</source>
</syntaxhighlight>
<source lang='Ruby'>
<syntaxhighlight lang='Ruby'>
sum = 0
sum = 0
(1..4).each do |i|
(1..4).each do |i|
137번째 줄: 145번째 줄:
puts sum
puts sum
# 10
# 10
</source>
</syntaxhighlight>
<source lang='Ruby'>
<syntaxhighlight lang='Ruby'>
sum = 0
sum = 0
[1, 2, 3, 4].each { |i| sum += i }
[1, 2, 3, 4].each { |i| sum += i }
puts sum
puts sum
# 10
# 10
</source>
</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]]
*[[foreach]]
* [[foreach]]
*[[Iterator 패턴]]
* [[Iterator 패턴]]
*[[자바 키워드]]
* [[자바 키워드]]
*[[str_repeat]]
* [[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 }}