Foreach-løkken bruges til hurtigt at iterere over elementerne i en beholder (array, vektorer osv.) uden at udføre initialisering, test eller inkrementering/reduktion. Foreach loops fungerer ved at gøre noget for hvert element i stedet for at gøre noget n gange. Selvom der ikke er nogen foreach loop i C, understøttes den af C++ og Java. Det blev først introduceret i C++ i C++ 11 og i Java i JDK 1.5.0. I både C++ og Java er nøgleordet for hver sløjfe 'for'.
Syntaks
for (data_type variable_name : container_type) { operations using variable_name }
Vi behøver ikke længere at specificere datatypen for variabler i foreach loops takket være introduktionen af auto nøgleordet i C++ og var nøgleordet i Java. Typeinferens registrerer containerens datatype og indstiller den variabel, der bruges til at krydse, til den samme datatype.
Koden nedenfor demonstrerer brugen af en foreach loop til forskellige containere, såvel som auto/var nøgleord i C++/Java.
C++
// C++ program to demonstrate use of foreach for array #include using namespace std; int main() { int arr[] = { 10, 20, 30, 40 }; // Printing elements of an array using // foreach loop // Here, int is the data type, x is the variable name // and arr is the array for which we want to iterate foreach cout<<'traversing the array with foreach using array's data type: '; for (int x : arr) cout<<x<<' type of is set as int cout<<' traversing auto keyword (auto } < pre> <h3>JAVA</h3> <pre> // Java program to demonstrate use of foreach public class Main { public static void main(String[] args) { // Declaring 1-D array with size 4 int arr[] = { 10, 20, 30, 40 }; // Printing elements of an array using // foreach loop // Here, int is the data type, x is the variable name // and arr is the array for which we want to iterate foreach System.out.print('Traversing the array with foreach using array's data type: '); for (int x : arr) System.out.print(x+' '); // data type of x is set as int System.out.print(' Traversing the array with foreach using auto keyword : '); for (var x : arr) System.out.print(x+' '); } } </pre> <p> <strong>Output</strong> </p> <pre> Traversing the array with foreach using array's data type: 10 20 30 40 Traversing the array with foreach using auto keyword : 10 20 30 40 </pre> <h3>Vector C++ programme:</h3> <pre> #include #include using namespace std; int main() { vector value{'This', 'is', 'foreach', 'example', 'using', 'vector.'}; cout<<'traversing the vector with foreach using vector's data type: '; for (string v : value) { cout<<v<<' } cout<<' traversing auto keyword (auto return 0; < pre> <p> <strong>Output</strong> </p> <pre> Traversing the vector with foreach using vector's data type: This is foreach example using vector. Traversing the vector with foreach using auto keyword : This is foreach example using vector. </pre> <h2>C++/Java Set Program:</h2> <h3>C++</h3> <pre> #include #include using namespace std; int main() { set value = {6, 2, 7, 4, 10, 5, 1}; cout<<'traversing the set with foreach using set's data type: '; for (int v : value) { cout<<v<<' } cout<<' traversing auto keyword (auto return 0; < pre> <h3>JAVA</h3> <pre> import java.util.*; public class GFG { public static void main(String[] args) { Set hash_Set = new HashSet(); hash_Set.add('Geeks'); hash_Set.add('For'); hash_Set.add('Geeks'); hash_Set.add('Foreach'); hash_Set.add('Example'); hash_Set.add('Set'); System.out.print('Traversing the set with foreach using set's data type: '); for(String hs : hash_Set) { System.out.print(hs+' '); } System.out.print(' Traversing the set with foreach using auto keyword : '); for (var hs : hash_Set) { System.out.print(hs+' '); } } } </pre> <p> <strong>Output</strong> </p> <pre> Traversing the set with foreach using set's data type: 1 2 4 5 6 7 10 Traversing the set with foreach using auto keyword : 1 2 4 5 6 7 10 </pre> <p>For array, vector, and set, we can use different data types in foreach.</p> <h2>C++/Java Map Program:</h2> <h3>C++</h3> <pre> #include #include using namespace std; int main() { map mapExample; mapExample.insert(pair(1, 'Geeks')); mapExample.insert(pair(2, '4')); mapExample.insert(pair(3, 'Geeks')); mapExample.insert(pair(4, 'Map')); mapExample.insert(pair(5, 'Foreach')); mapExample.insert(pair(6, 'Example')); cout<<'traversing the map with foreach using map's data type '; for (pair mpex : mapexample ) { cout<<mpex.first<<' '<<mpex.second<<endl; } cout<<' traversing auto keyword '; (auto mapexample){ return 0; < pre> <h3>JAVA</h3> <pre> import java.io.*; import java.util.Map; import java.util.HashMap; class GFG { public static void main (String[] args) { Map gfg = new HashMap(); gfg.put(1, 'Geeks'); gfg.put(2, '4'); gfg.put(3, 'Geeks'); gfg.put(4, 'Map'); gfg.put(5, 'Foreach'); gfg.put(6, 'Example'); System.out.println('Traversing the map with foreach using map's data type'); for (Map.Entry entry : gfg.entrySet()) System.out.println(entry.getKey() + ' ' + entry.getValue()); System.out.println(' Traversing the map with foreach using auto keyword'); for (var entry : gfg.entrySet()) System.out.println(entry.getKey() + ' ' + entry.getValue()); } } </pre> <p> <strong>Output</strong> </p> <pre> Traversing the map with foreach using map's data type 1 Geeks 2 4 3 Geeks 4 Map 5 Foreach 6 Example Traversing the map with foreach using auto keyword 1 Geeks 2 4 3 Geeks 4 Map 5 Foreach 6 Example </pre> <h3>Foreach loop has the following advantages:</h3> <ul> <li>This improves the readability of the code.</li> <li>Removes the possibility of data over- or under-running errors.</li> </ul> <h3>Foreach loop has the following disadvantage:</h3> <ul> <li>It is not possible to iterate over the elements in reverse order.</li> <li>Every element will be accessed; no elements in between will be skipped.</li> </ul> <hr></'traversing></pre></'traversing></pre></'traversing></pre></'traversing>
Produktion
Traversing the array with foreach using array's data type: 10 20 30 40 Traversing the array with foreach using auto keyword : 10 20 30 40
Vector C++ program:
#include #include using namespace std; int main() { vector value{'This', 'is', 'foreach', 'example', 'using', 'vector.'}; cout<<\'traversing the vector with foreach using vector\'s data type: \'; for (string v : value) { cout<<v<<\' } cout<<\' traversing auto keyword (auto return 0; < pre> <p> <strong>Output</strong> </p> <pre> Traversing the vector with foreach using vector's data type: This is foreach example using vector. Traversing the vector with foreach using auto keyword : This is foreach example using vector. </pre> <h2>C++/Java Set Program:</h2> <h3>C++</h3> <pre> #include #include using namespace std; int main() { set value = {6, 2, 7, 4, 10, 5, 1}; cout<<\'traversing the set with foreach using set\'s data type: \'; for (int v : value) { cout<<v<<\' } cout<<\' traversing auto keyword (auto return 0; < pre> <h3>JAVA</h3> <pre> import java.util.*; public class GFG { public static void main(String[] args) { Set hash_Set = new HashSet(); hash_Set.add('Geeks'); hash_Set.add('For'); hash_Set.add('Geeks'); hash_Set.add('Foreach'); hash_Set.add('Example'); hash_Set.add('Set'); System.out.print('Traversing the set with foreach using set's data type: '); for(String hs : hash_Set) { System.out.print(hs+' '); } System.out.print(' Traversing the set with foreach using auto keyword : '); for (var hs : hash_Set) { System.out.print(hs+' '); } } } </pre> <p> <strong>Output</strong> </p> <pre> Traversing the set with foreach using set's data type: 1 2 4 5 6 7 10 Traversing the set with foreach using auto keyword : 1 2 4 5 6 7 10 </pre> <p>For array, vector, and set, we can use different data types in foreach.</p> <h2>C++/Java Map Program:</h2> <h3>C++</h3> <pre> #include #include using namespace std; int main() { map mapExample; mapExample.insert(pair(1, 'Geeks')); mapExample.insert(pair(2, '4')); mapExample.insert(pair(3, 'Geeks')); mapExample.insert(pair(4, 'Map')); mapExample.insert(pair(5, 'Foreach')); mapExample.insert(pair(6, 'Example')); cout<<\'traversing the map with foreach using map\'s data type \'; for (pair mpex : mapexample ) { cout<<mpex.first<<\' \'<<mpex.second<<endl; } cout<<\' traversing auto keyword \'; (auto mapexample){ return 0; < pre> <h3>JAVA</h3> <pre> import java.io.*; import java.util.Map; import java.util.HashMap; class GFG { public static void main (String[] args) { Map gfg = new HashMap(); gfg.put(1, 'Geeks'); gfg.put(2, '4'); gfg.put(3, 'Geeks'); gfg.put(4, 'Map'); gfg.put(5, 'Foreach'); gfg.put(6, 'Example'); System.out.println('Traversing the map with foreach using map's data type'); for (Map.Entry entry : gfg.entrySet()) System.out.println(entry.getKey() + ' ' + entry.getValue()); System.out.println(' Traversing the map with foreach using auto keyword'); for (var entry : gfg.entrySet()) System.out.println(entry.getKey() + ' ' + entry.getValue()); } } </pre> <p> <strong>Output</strong> </p> <pre> Traversing the map with foreach using map's data type 1 Geeks 2 4 3 Geeks 4 Map 5 Foreach 6 Example Traversing the map with foreach using auto keyword 1 Geeks 2 4 3 Geeks 4 Map 5 Foreach 6 Example </pre> <h3>Foreach loop has the following advantages:</h3> <ul> <li>This improves the readability of the code.</li> <li>Removes the possibility of data over- or under-running errors.</li> </ul> <h3>Foreach loop has the following disadvantage:</h3> <ul> <li>It is not possible to iterate over the elements in reverse order.</li> <li>Every element will be accessed; no elements in between will be skipped.</li> </ul> <hr></\'traversing></pre></\'traversing></pre></\'traversing>
C++/Java Set Program:
C++
#include #include using namespace std; int main() { set value = {6, 2, 7, 4, 10, 5, 1}; cout<<\'traversing the set with foreach using set\'s data type: \'; for (int v : value) { cout<<v<<\' } cout<<\' traversing auto keyword (auto return 0; < pre> <h3>JAVA</h3> <pre> import java.util.*; public class GFG { public static void main(String[] args) { Set hash_Set = new HashSet(); hash_Set.add('Geeks'); hash_Set.add('For'); hash_Set.add('Geeks'); hash_Set.add('Foreach'); hash_Set.add('Example'); hash_Set.add('Set'); System.out.print('Traversing the set with foreach using set's data type: '); for(String hs : hash_Set) { System.out.print(hs+' '); } System.out.print(' Traversing the set with foreach using auto keyword : '); for (var hs : hash_Set) { System.out.print(hs+' '); } } } </pre> <p> <strong>Output</strong> </p> <pre> Traversing the set with foreach using set's data type: 1 2 4 5 6 7 10 Traversing the set with foreach using auto keyword : 1 2 4 5 6 7 10 </pre> <p>For array, vector, and set, we can use different data types in foreach.</p> <h2>C++/Java Map Program:</h2> <h3>C++</h3> <pre> #include #include using namespace std; int main() { map mapExample; mapExample.insert(pair(1, 'Geeks')); mapExample.insert(pair(2, '4')); mapExample.insert(pair(3, 'Geeks')); mapExample.insert(pair(4, 'Map')); mapExample.insert(pair(5, 'Foreach')); mapExample.insert(pair(6, 'Example')); cout<<\'traversing the map with foreach using map\'s data type \'; for (pair mpex : mapexample ) { cout<<mpex.first<<\' \'<<mpex.second<<endl; } cout<<\' traversing auto keyword \'; (auto mapexample){ return 0; < pre> <h3>JAVA</h3> <pre> import java.io.*; import java.util.Map; import java.util.HashMap; class GFG { public static void main (String[] args) { Map gfg = new HashMap(); gfg.put(1, 'Geeks'); gfg.put(2, '4'); gfg.put(3, 'Geeks'); gfg.put(4, 'Map'); gfg.put(5, 'Foreach'); gfg.put(6, 'Example'); System.out.println('Traversing the map with foreach using map's data type'); for (Map.Entry entry : gfg.entrySet()) System.out.println(entry.getKey() + ' ' + entry.getValue()); System.out.println(' Traversing the map with foreach using auto keyword'); for (var entry : gfg.entrySet()) System.out.println(entry.getKey() + ' ' + entry.getValue()); } } </pre> <p> <strong>Output</strong> </p> <pre> Traversing the map with foreach using map's data type 1 Geeks 2 4 3 Geeks 4 Map 5 Foreach 6 Example Traversing the map with foreach using auto keyword 1 Geeks 2 4 3 Geeks 4 Map 5 Foreach 6 Example </pre> <h3>Foreach loop has the following advantages:</h3> <ul> <li>This improves the readability of the code.</li> <li>Removes the possibility of data over- or under-running errors.</li> </ul> <h3>Foreach loop has the following disadvantage:</h3> <ul> <li>It is not possible to iterate over the elements in reverse order.</li> <li>Every element will be accessed; no elements in between will be skipped.</li> </ul> <hr></\'traversing></pre></\'traversing>
Produktion
Traversing the set with foreach using set's data type: 1 2 4 5 6 7 10 Traversing the set with foreach using auto keyword : 1 2 4 5 6 7 10
For array, vektor og sæt kan vi bruge forskellige datatyper i hver.
C++/Java kortprogram:
C++
#include #include using namespace std; int main() { map mapExample; mapExample.insert(pair(1, 'Geeks')); mapExample.insert(pair(2, '4')); mapExample.insert(pair(3, 'Geeks')); mapExample.insert(pair(4, 'Map')); mapExample.insert(pair(5, 'Foreach')); mapExample.insert(pair(6, 'Example')); cout<<\\'traversing the map with foreach using map\\'s data type \\'; for (pair mpex : mapexample ) { cout<<mpex.first<<\\' \\'<<mpex.second<<endl; } cout<<\\' traversing auto keyword \\'; (auto mapexample){ return 0; < pre> <h3>JAVA</h3> <pre> import java.io.*; import java.util.Map; import java.util.HashMap; class GFG { public static void main (String[] args) { Map gfg = new HashMap(); gfg.put(1, 'Geeks'); gfg.put(2, '4'); gfg.put(3, 'Geeks'); gfg.put(4, 'Map'); gfg.put(5, 'Foreach'); gfg.put(6, 'Example'); System.out.println('Traversing the map with foreach using map's data type'); for (Map.Entry entry : gfg.entrySet()) System.out.println(entry.getKey() + ' ' + entry.getValue()); System.out.println(' Traversing the map with foreach using auto keyword'); for (var entry : gfg.entrySet()) System.out.println(entry.getKey() + ' ' + entry.getValue()); } } </pre> <p> <strong>Output</strong> </p> <pre> Traversing the map with foreach using map's data type 1 Geeks 2 4 3 Geeks 4 Map 5 Foreach 6 Example Traversing the map with foreach using auto keyword 1 Geeks 2 4 3 Geeks 4 Map 5 Foreach 6 Example </pre> <h3>Foreach loop has the following advantages:</h3> <ul> <li>This improves the readability of the code.</li> <li>Removes the possibility of data over- or under-running errors.</li> </ul> <h3>Foreach loop has the following disadvantage:</h3> <ul> <li>It is not possible to iterate over the elements in reverse order.</li> <li>Every element will be accessed; no elements in between will be skipped.</li> </ul> <hr></\\'traversing>
Produktion
Traversing the map with foreach using map's data type 1 Geeks 2 4 3 Geeks 4 Map 5 Foreach 6 Example Traversing the map with foreach using auto keyword 1 Geeks 2 4 3 Geeks 4 Map 5 Foreach 6 Example
For hver sløjfe har følgende fordele:
- Dette forbedrer kodens læsbarhed.
- Fjerner muligheden for dataover- eller underløbsfejl.
Foreach loop har følgende ulempe:
- Det er ikke muligt at iterere over elementerne i omvendt rækkefølge.
- Hvert element vil blive tilgået; ingen elementer imellem vil blive sprunget over.
\\'traversing>\'traversing>\'traversing>'traversing>