Jump to content

Can someone please explain this line of code to me?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
Demodex

Demodex

    Learning Programmer

  • Members
  • PipPipPip
  • 34 posts
I've been dodging arrays for quite some time in C# but now it's time to ride that horse.

The following code:

        private void button1_Click(object sender, EventArgs e)

        {

1            // create the array

2

3            int[] ml = new int[7] { 5, 3, 7, 9, 2, 6, 8 };

4

5            // loop through the contents of the ml array

6

7            string x = "";

8

9            for (int i = 0; i < ml.Length; i++)

10            {

11                x += Convert.ToInt32(ml[i]) + " ";

12            }

13

14            MessageBox.Show(x, "Contents of Array");        }

Well the 3rd line defines the array, what type of an array and the number of data pieces. We also define which characters.

In the 7th line we create a variable "x" to show the contents of the array later on.

In the 9th line we say that for each time that the variable "i" (which we set equals to 0 at first) is smaller than the number of pieces in the "ml" array (7 pieces) - add 1 to the "i" variable.

In the 11th line - I dont understand what we are doing really...Can anyone have a look and explain to me as bright as he can what's going on?
And if my assumptions on the early lines are wrong - please tell me so I would know i'm making a mistake.

Before I finish this message I wanna point out -

If my assumptions were right about the array statements, it means that once the variable "i" will equal 7 - something will happen. It keeps showing the messagebox, does that still mean that "i" is now equal or larger than 7?
Isn't the loop supposed to stop because of that?

Appreciate the effort for the one who will clarify this for me :)

#2
FlashM

FlashM

    Learning Programmer

  • Members
  • PipPipPip
  • 90 posts
In your third line, where you also populate your array by specifying values in { }, it's also allowed you to write this code which does the same just with few less storkes:

int[] ml = { 5, 3, 7, 9, 2, 6, 8 };


In your 9th line you could use foreach loop insted for loop, which iterates through all the elements in your ml array. If there is no elements no iteration will happen and the program will continue with code after the foreach loop.

foreach (int number in ml)
{

}

In your case where you are using for loop the following happens: For loop iterates from 0 and continues to iterate until i is less then 7 (ml.Length). When i reaches 7, loop stops and code continues with code after the for loop.


Your 11th line is actually something you don't need to to. Type int is by default Int32 and the thing you are trying to do is converting Int32 to Int32, which is really pointless. You could write just the following:

string x = "";
foreach (int number in ml)
{
x += number + " ";
}

In your 11th line the code x += number + " "; is just a short notation for:

x = x + number + " ";

The thing we are doing in your 11th line is:

before the for loop starts: your x equals to empty string.
In first loop: x = x + number + " " is actually: "" = "" + 5 + " " and your x becomes "5 ".
In second loop: x = x + number + " " is actually "5 " = "5 " + 3 + " " and your x becomes "5 3 ".
In your third loop: x = x + number + " " is actually "5 3 " = "5 3 " + 7 + " " and your x becomes "5 3 7 ".
And so on...


Hope the explains and clarifies your code.

#3
Demodex

Demodex

    Learning Programmer

  • Members
  • PipPipPip
  • 34 posts
Thanks alot FlashM!

You really helped me with that, now I understand what's going on.
Last question that will make this clear-
The message box acts only after i would equal 7?
I mean, the loop iterates itself time after time and only when it equals 7 it shows the message box?

#4
FlashM

FlashM

    Learning Programmer

  • Members
  • PipPipPip
  • 90 posts
It depends on where you put your code that shows your message box. If it's inside a for loop, then messagebox will show itself as many times as the loop will iterate.

Example:

Suppose you have 7 items in your ml array, so your message box will show 7 times.
for (int i = 0; i < ml.Length; i++)
{
MessageBox.Show(i);
}

#5
Demodex

Demodex

    Learning Programmer

  • Members
  • PipPipPip
  • 34 posts
Understood!

Thanks a lot my man.