Jump to content

C# loop help

Flanelman

Hi, so i'm a little stuck with the loops on c#

I am learning about four loops: do while, while, for and foreach.

I know that the do while loop can be used if you are say making a game and want to ask the user to play again, but how else do you know which loop to use?

Thanks in advanced :)

Link to comment
Share on other sites

Link to post
Share on other sites

For the most part, they can be interchanged -- it's just a question of what seems easier to you/the reader to understand. 
 
For example, if I want to iterate through an array, I could use any loop I wanted to, however a for loop looks neatest: 

for(int i=0; i<array.length; i++)     array[i] = 7; 

Alternatively, I could just as easily do: 

int i = 0; while (i < array.length){     array[i] = 7;     i++;}

Or:

int i = 0;do{    array[i] = 7;     i++; }while (i < array.length -1)

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

 

For the most part, they can be interchanged -- it's just a question of what seems easier to you/the reader to understand. 

 

For example, if I want to iterate through an array, I could use any loop I wanted to, however a for loop looks neatest: 

for(int i=0; i<array.length; i++)     array[i] = 7; 

Alternatively, I could just as easily do: 

 

int i = 0; while (i < array.length){     array[i] = 7;     i++;}

Or:

int i = 0;do{    array[i] = 7;     i++; }while (i < array.length -1)

Oh, okay, and also what's the difference between a for, and a for each leap?

I appreciate the help :)

Link to comment
Share on other sites

Link to post
Share on other sites

For is generally the most preferable out of for, while and do-while as all the information is right there at the first line; initialisation, termination condition and iterator. With the other loop structures they are necessarily split up, with do-while being the least preferable since the loop condition is at the end of the loop. There are times when this might make more sense but they are few and far between.

 

One other thing to consider is for vs for-each.

var array = {1, 2, 3, 4};for(int i = 0; i < array.length; i++){    Console.WriteLine(""+array[i]);}foreach(int i in array){    Console.WriteLine(""+i);}

The above two loops do exactly the same thing, but in this case since we don't care about the index we can use a foreach loop which doesn't involve any indexes. This means we are more likely to avoid making mistakes when either initialising an iterator, defining the termination condition or setting the iteration. With a for loop there are at least 3 places where you can make a small mistake that could break the loop whereas we can use a foreach loop to achieve the same effect for only really 1 possible place to mess up; by using the wrong variable.

 

Edit: there is no performance difference between either for and for-each. I can't confirm it right now, but I'm fairly sure that for-each is just "syntactic sugar" for a normal for loop meaning that when the C# code is being compiled, a foreach loop would be translated into a normal for loop by generating a start, end and iterating conditions using the sensible values for the array.

Link to comment
Share on other sites

Link to post
Share on other sites

Oh, okay, and also what's the difference between a for, and a for each leap?

I appreciate the help  :)

int [] num = {1, 2, 3, 4, 5};  //array of numbers for(int i : num)    System.out.print(i); 

would be the same as 

int [] num = {1, 2, 3, 4, 5}; for(int i=0; i<num.length; i++)    System.out.print(i);

So it really comes down to personal taste, and what makes more sense to you (and what you think will make the most sense to the reader). There are times when stylistically one is better, but it really comes down to preference. 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

int [] num = {1, 2, 3, 4, 5};  //array of numbers for(int i : num)    System.out.print(i);
would be the same as
int [] num = {1, 2, 3, 4, 5}; for(int i=0; i<num.length; i++)    System.out.print(i);
So it really comes down to personal taste, and what makes more sense to you (and what you think will make the most sense to the reader). There are times when stylistically one is better, but it really comes down to preference.
Remember, when using a for each loop you can't modify the contents of whatever you are looking at. ie array, list, etc

Just making sure the op is aware :)

Link to comment
Share on other sites

Link to post
Share on other sites

Thanks guys, that wa very helpful :)

 

Actually ... They are not the same. This is some deeper stuff tho. Its a common source of errors in all honesty.

 

Both are said to be the same because for the word "for" used in the nomenclature but its not true. A for each uses an iterator while for does not.

 

In reality a for each is closer to a while loop than a for loop. I am not sure if you want to go on deep level, but I would look around stackoverflow for some explinations.

 

One of the best explanations is the first answer here:

 

http://programmers.stackexchange.com/questions/178218/for-vs-foreach-vs-linq

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×