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

112번째 줄: 112번째 줄:
<source lang='Ruby'>
<source lang='Ruby'>
sum = 0
sum = 0
for i in 1..10
for i in 1..4
sum += i
sum += i
end
end
puts sum
puts sum
# 55
# 10
</source>
</source>
<source lang='Ruby'>
<source lang='Ruby'>
sum = 0
sum = 0
for i in 1...11
for i in 1...5
sum += i
sum += i
end
end
puts sum
puts sum
# 55
# 10
</source>
</source>
<source lang='Ruby'>
<source lang='Ruby'>
sum = 0
sum = 0
(1..10).each { |i| sum += i }
(1..4).each { |i| sum += i }
puts sum
puts sum
# 55
# 10
</source>
</source>
<source lang='Ruby'>
<source lang='Ruby'>
sum = 0
sum = 0
(1..10).each do |i|
(1..4).each do |i|
     sum += i
     sum += i
end
end
puts sum
puts sum
# 55
# 10
</source>
<source lang='Ruby'>
sum = 0
[1, 2, 3, 4].each { |i| sum += i }
puts sum
# 10
</source>
</source>



2014년 5월 31일 (토) 22:16 판

for
for문

1 Bash

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

9 Visual Basic

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

10 같이 보기

11 참고 자료