R Programming
R Dplyr Tutorial: Data Manipulation(Join) & Cleaning(Spread)
Introduction to Data Analysis Data analysis can be divided into three parts Extraction: First, we...
For Each is a Looping command. You should use this statement when you need to repeat a set of statements for every item you have in a collection. You can use it to manipulate all the elements contained in a collection or array.
In this VB. Net tutorial, you will learn,
The for each statement takes the syntax given below:
For Each item [ As data_type ] In group
[ statement(s) ]
[ Continue For ]
[ statement(s) ]
[ Exit For ]
[ statement(s) ]
Next [ item ]
Here,
Let us demonstrate how to use this statement:
Step 1) Begin by creating a new console application.
Step 2) Use the following code:
Module Module1
Sub Main()
Dim myArray() As Integer = {10, 3, 12, 23, 9}
Dim item As Integer
For Each item In myArray
Console.WriteLine(item)
Next
Console.ReadKey()
End Sub
End Module
Step 3) Click the Start button from the top bar to run the code. You should get the following result:
Here is a screenshot of the code:
Explanation of Code:
The For Each loop can be nested. This will occurs when we put one For Each loop inside another For Each loop. Let us demonstrate this using an example.
Step 1) Create a new console application.
Step 2) Use the following code:
Module Module1
Sub Main()
Dim nums() As Integer = {12, 23, 35}
Dim names() As String = {"gtupapers", "alice", "antony"}
For Each n As Integer In nums
For Each st As String In names
Console.Write(n.ToString & st & " ")
Next
Next
Console.ReadKey()
End Sub
End Module
Step 3) Click the Start button from the top bar to execute the code. You should get the following output:
Here is a screenshot of the code:
Explanation of code:
When you use the Exit For statement, the execution will leave the For Each … Next loop and control will be transferred to the statements that come after the Next statement.
When you use the Continue For statement, control will be transferred to the next iteration of your loop. Let us demonstrate this using an example:
Step 1) Begin by creating a new console application.
Step 2) Use the following code:
Module Module1
Sub Main()
Dim nums() As Integer =
{10, 12, 14, 17, 19, 23, 26, 31, 33, 37, 40, 48}
For Each n As Integer In nums
If n >= 17 And n <= 25 Then
Continue For
End If
Console.Write(n.ToString & " ")
If n = 37 Then
Exit For
End If
Next
Console.ReadKey()
End Sub
End Module
Step 3) Run the code by clicking the Start button from the top bar. You should get the following result:
Here is a screenshot of the code:
Explanation of Code:
Introduction to Data Analysis Data analysis can be divided into three parts Extraction: First, we...
CAD software refers to a type of software program used by engineers and designers to create 2D and 3D...
What is Jenkins Pipeline? Jenkins Pipeline is a combination of plugins that supports integration and...
An eCommerce platform is a software application that helps online businesses to manage their...
Slideshow maker software are applications used to develop presentations or videos with different...
Photo viewer is computer software that can display stored pictures. These tools can handle many...