Link to home
Start Free TrialLog in
Avatar of jazzIIIlove
jazzIIIloveFlag for Sweden

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.
SOLUTION
Avatar of Paul Jackson
Paul Jackson
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jazzIIIlove

ASKER

Thanks for the explanations,

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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>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.