- 다른 뜻에 대해서는 jQuery .each() 문서를 참조하십시오.
- extended for
- foreach ... as
- foreach ... in
- for ... in
- each
1 Bash[ | ]
Bash for 문서를 참고하십시오.
Bash
Copy
ARR=("John Smith" "Jane Doe" "Mike Barnes" "Kevin Patterson")
for VALUE in "${ARR[@]}"; do
echo "[$VALUE]"
done
# [Jonh Smith]
# [Jane Doe]
# [Mike Barnes]
# [Kevin Patterson]
Bash
Copy
ARR=("John Smith" "Jane Doe" "Mike Barnes" "Kevin Patterson")
for i in "${!ARR[@]}"; do
echo $i: ${ARR[$i]}
done
# 0: John Smith
# 1: Jane Doe
# 2: Mike Barnes
# 3: Kevin Patterson
Bash
Copy
ARR=("John Smith" "Jane Doe" "Mike Barnes" "Kevin Patterson")
for i in "${!ARR[@]}"; do
echo $((i+1)): ${ARR[$i]}
done
# 1: John Smith
# 2: Jane Doe
# 3: Mike Barnes
# 4: Kevin Patterson
2 C#[ | ]
C#
Copy
// http://msdn.microsoft.com/en-us/library/ttw7t8t6(v=vs.90).aspx
int[] fibarray = new int[] { 0, 1, 2, 3, 5, 8, 13 };
foreach (int i in fibarray)
{
System.Console.WriteLine(i);
}
3 Go[ | ]
Go foreach 문서를 참고하십시오.
Go
Copy
package main
import "fmt"
func main() {
fruits := []string{"Apple", "Banana", "Orange"}
for _, fruit := range fruits {
fmt.Println(fruit)
}
}
4 Java[ | ]
Java Foreach 문서를 참고하십시오.
Java
Copy
String[] strArr = { "John Smith", "Jane Doe", "Mike Barnes", "Kevin Patterson" };
for (String str : strArr){
System.out.println(str);
}
// John Smith
// Jane Doe
// Mike Barnes
// Kevin Patterson
5 JavaScript[ | ]
- i is not value but key!
JavaScript
Copy
var arr = ["John Smith", "Jane Doe", "Mike Barnes", "Kevin Patterson"];
for(var i in arr) {
console.log(arr[i]);
}
// John Smith
// Jane Doe
// Mike Barnes
// Kevin Patterson
JavaScript
Copy
var arr = ["John Smith", "Jane Doe", "Mike Barnes", "Kevin Patterson"];
for(var i in arr) {
console.log(i+": "+arr[i]);
}
// 0: John Smith
// 1: Jane Doe
// 2: Mike Barnes
// 3: Kevin Patterson
6 jQuery[ | ]
JavaScript
Copy
var arr = ["John Smith", "Jane Doe", "Mike Barnes", "Kevin Patterson"];
$.each(arr, function() {
console.log(''+this);
});
// John Smith
// Jane Doe
// Mike Barnes
// Kevin Patterson
JavaScript
Copy
var arr = ["John Smith", "Jane Doe", "Mike Barnes", "Kevin Patterson"];
$.each(arr, function(i, val) {
console.log(i+': '+val);
});
// 0: John Smith
// 1: Jane Doe
// 2: Mike Barnes
// 3: Kevin Patterson
7 Objective-C[ | ]
Objective-C
Copy
NSArray *strArr = [[NSArray alloc] initWithObjects:@"A", @"B", @"C", @"D"];
for(NSString *str in strArr) {
NSLog(@"%@\n", str);
}
8 Perl[ | ]
Perl
Copy
@strArr = ('A', 'B', 'C', 'D');
foreach $str (@strArr) {
print "$str ";
}
# A B C D
foreach (@strArr) {
print "$_ ";
}
# A B C D
for $str (@strArr) {
print "$str ";
}
# A B C D
for (@strArr) {
print "$_ ";
}
# A B C D
9 PHP[ | ]
PHP
Copy
$strArr = array('A', 'B', 'C', 'D');
foreach ($strArr as $str) {
echo $str.' ';
}
// A B C D
PHP
Copy
foreach (range(1,5) as $i) {
echo "$i ";
}
// 1 2 3 4 5
PHP
Copy
$fruits = array('a'=>'apple', 'b'=>'banana', 'c'=>'cranberry');
foreach($fruits as $key => $value) {
echo $key."_".$value."\n";
}
// a_apple
// b_banana
// c_cranberry
10 PowerShell[ | ]
powershell
Copy
$arr = @(1,"Hello",3.5)
foreach ($v in $arr) {$v}
# 1
# Hello
# 3.5
powershell
Copy
$arr = @(1,"Hello",3.5)
$arr
# 1
# Hello
# 3.5
11 Python[ | ]
list
Python
Copy
fruits = ['Banana', 'Orange', 'Apple', 'Mango']
for fruit in fruits:
print( fruit )
# Banana
# Orange
# Apple
# Mango
Python
Copy
for fruit in ('Banana', 'Orange', 'Apple', 'Mango'):
print( fruit )
Python
Copy
for ch in 'ABC':
print( ch )
dictionary
Python
Copy
fruits = {'a':'apple', 'b':'banana', 'c':'cranberry'}
for key in fruits:
print key + '_' + fruits[key]
# a_apple
# c_cranberry
# b_banana
Python
Copy
fruits = {'a':'apple', 'b':'banana', 'c':'cranberry'}
for k, v in fruits.items():
print k + '_' + v
# a_apple
# c_cranberry
# b_banana
12 R[ | ]
R for 문서를 참고하십시오.
R
Copy
v <- c("John Smith", "Jane Doe", "Mike Barnes", "Kevin Patterson")
for( s in v ) {
print( s )
}
## [1] "John Smith"
## [1] "Jane Doe"
## [1] "Mike Barnes"
## [1] "Kevin Patterson"
13 Ruby[ | ]
array
Ruby
Copy
strArr = ['A', 'B', 'C']
strArr.each do |str|
puts str
end
# A
# B
# C
Ruby
Copy
strArr = ['A', 'B', 'C']
strArr.each { |str| puts str }
# A
# B
# C
Ruby
Copy
strArr = ['A', 'B', 'C']
for str in strArr
puts str
end
# A
# B
# C
dictionary
Ruby
Copy
member = { "ID" => 102, "Name" => "YONEZAWA Akinori" }
member.each { |x, y| puts "#{x}: #{y}" }
# ID: 102
# Name: YONEZAWA Akinori
Ruby
Copy
member = { "ID" => 102, "Name" => "YONEZAWA Akinori" }
member.each do |x, y|
puts "#{x}: #{y}"
end
# ID: 102
# Name: YONEZAWA Akinori
14 VB[ | ]
vbnet
Copy
For Each number As Integer In New Long() {45, 3, 987}
Console.Write(number & " ")
Next
' Output: 45 3 987
15 같이 보기[ | ]
- for
- items
- while
- continue
- print_r
- Variable-length argument lists
- array
- dictionary
- array_push
- 함수 count()
- 함수 map()
- 제어문
- C# 키워드
16 참고[ | ]
편집자 Jmnote Jmnote bot 14.32.144.145
로그인하시면 댓글을 쓸 수 있습니다.