jazzIIIlove
asked on
foreach drawbacks?
Hi there;
I get used to for and while loops. but when it comes to foreach, I couldn't figure out when or how I should use them.
Is there any drawbacks in foreach statements, that, a scenario that they are not applicable and applicable?
Kind regards.
I get used to for and while loops. but when it comes to foreach, I couldn't figure out when or how I should use them.
Is there any drawbacks in foreach statements, that, a scenario that they are not applicable and applicable?
Kind regards.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
i couldn't get the last code snippet which is;
foreach (int i in intList)
{
intList.Remove(i);
}
Is this an equivalent for;
for(int i = 0;i < 100; i++)
?
And what is the type of intList as I assume it's a list, if so, then, that lntList one is not simialar to < 100
Kind regards.
foreach (int i in intList)
{
intList.Remove(i);
}
Is this an equivalent for;
for(int i = 0;i < 100; i++)
?
And what is the type of intList as I assume it's a list, if so, then, that lntList one is not simialar to < 100
Kind regards.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>i couldn't get the last code snippet which is;
>>foreach (int i in intList)
>>{
>>intList.Remove(i);
>>}
intList is a List of Integers
This code is removing each elements/integers from the list.
>>foreach (int i in intList)
>>{
>>intList.Remove(i);
>>}
intList is a List of Integers
This code is removing each elements/integers from the list.
ASKER
so for each int in an array or collection is the phrase for 'foreach'.
So, in practice, I can loop through for the following:
for(int i = 0;i i < 100; i++)
with for each.
Kind regards.