C# Loop Through Arrays
Loop Through an Array You can loop through the array elements with the for loop, and use the Length property to specify how many times the loop should run. The following example outputs all elements in the cars array: Example…
Loop Through an Array You can loop through the array elements with the for loop, and use the Length property to specify how many times the loop should run. The following example outputs all elements in the cars array: Example…
C# For Loop When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop: Syntax for (statement 1; statement 2; statement 3) { // code block…
Real Life Example To demonstrate a practical example of using arrays, let’s create a program that calculates the average of different ages: Example // An array storing different ages int ages[8] = {20, 22, 18, 35, 48, 26, 87, 70};…
The foreach Loop There is also a “for-each loop” (also known as ranged-based for loop), which is used exclusively to loop through elements in an array (or other data structures): Syntax for (type variableName : arrayName) { // code…
C++ For Loop When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop: Syntax for (statement 1; statement 2; statement 3) { // code…
The do…while loop – Loops through a block of code once, and then repeats the loop as long as the specified condition is true. The PHP do…while Loop The do…while loop will always execute the block of code at least…
In the following chapters you will learn how to repeat code by using loops in PHP. PHP Loops Often when you write code, you want the same block of code to run over and over again a certain number of…
Loop Through an Enum The enum type has a values() method, which returns an array of all enum constants. This method is useful when you want to loop through the constants of an enum: Example enum Level { LOW, MEDIUM,…
Loop Through a HashMap Loop through the items of a HashMap with a for-each loop. Note: Use the keySet() method if you only want the keys, and use the values() method if you only want the values: