What is difference between for and for each loop.

What is difference between for and for each loop.Please explain with a example.

Your Answer

Hello,
The main difference between for and for each loop is to set the number of iteration manually. In for loop you have to set the number of iteration manually while in for each loop the iteration will be counted automatically as per the length of an array. Following the example of both loops respectively.

For Loop
int[] arr = new int[5];
arr[0] = 5;
arr[1] = 7;
arr[2] = 13;
arr[3] = 20;
arr[4] = 25;
for(int i=0;i<5;i++){
     print(arr[i]);
}

Foreach Loop
foreach(var i in arr){
     print(i);
}

Html

Didn't get the answer.
Contact people of Talent-Html directly by clicking here