-3

Here is the program code from tutorial:

using System;
namespace ArrayApplication
{
   class MyArray
   {
      static void Main(string[] args)
      {
         int []  n = new int[10]; /* n is an array of 10 integers */

         /* initialize elements of array n */
         for ( int i = 0; i < 10; i++ )
         {
            n[i] = i + 100;
         }

         /* output each array element's value */
         foreach (int j in n )
         {
            int i = j-100;
            Console.WriteLine("Element[{0}] = {1}", i, j);
            i++;
         }
         Console.ReadKey();
      }
   }
}

Please, what tells to the processor what is j and where it appeared from?

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
D Kk
  • 3
  • 3
  • `j` is initialized during `n` foreach. It's a new `j` in its every loop which points to the iterated `n`'s value. – choz Mar 20 '16 at 18:17
  • so, we do not need any more syntax to know that "int j" is nothing but element of the array n? – D Kk Mar 20 '16 at 18:23
  • Geez, if you're not going to bother learning the absolute basic syntax of the code, we can't really help you. `foreach` is a very basic construct that you _must_ understand... we shouldn't have to spell it out for you. – Jeff Mercado Mar 20 '16 at 18:25
  • @DKk The fact that you're not confused with what a for loop does. I gave you an answer of what `j` is in for loop. Hope it helps.. – choz Mar 20 '16 at 18:30

5 Answers5

1

j is(are) the element(s) inside the array n. refer foreach explanation here : How do foreach loops work in C#?

Community
  • 1
  • 1
GorvGoyl
  • 42,508
  • 29
  • 229
  • 225
  • I know it, but how processor knows it? a kind of varialble "j" is just menitoned, nowoere it s "explained" that j are elemensts inside the array..I see that, knowing the result what program does but how does pc konw? – D Kk Mar 20 '16 at 18:17
  • @DKk It's not a kind of variable `j`.It's an `int j`.. – choz Mar 20 '16 at 18:18
  • you only declared "j" as an int right? so foreach loop iterates through the array "n" and subsequently puts the corresponding value in "j" – GorvGoyl Mar 20 '16 at 18:18
  • It is never clearly declared what is j, if it is in n then it is probably elements of array...is that the complier logic? Many thanks – D Kk Mar 20 '16 at 18:19
  • What is that "j"...in that particular case, how do we call it officially, ...sry for stuid question, it is just bothering me... many thanks – D Kk Mar 20 '16 at 18:20
  • compiler logic is to put value which is inside array to "j" at each iteration.that's how it is. – GorvGoyl Mar 20 '16 at 18:22
  • It obviouly looks like that, but for beginners looks a little...transcedent!!! thanks a lot!!! – D Kk Mar 20 '16 at 18:33
  • glad I could help. You may mark the answer as accepted if it clarifies your doubt. – GorvGoyl Mar 20 '16 at 18:36
1

You are inside a for-loop

so every value of j is getting printed and in the string format you are giving as parameter wich position to wich variable must be map to.

enter image description here

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • Hello! That on the pic is the only what is clear for me. I mean, FIRST, we have 10 i-s: – D Kk Mar 20 '16 at 18:27
  • we get ten n[i] values. But only in the foreach line we acually "assign" these valuus to in j, (in loop), do we? I mean, I expected sth like: int j=n[i] but compiller obvioulsy does not need that line? – D Kk Mar 20 '16 at 18:30
  • no you are not, the 10 i-s below to the fitst for loop, here you have defined a new variable int named i, AND THIS IS THE ONE USED IN THAT SCOPE.... – ΦXocę 웃 Пepeúpa ツ Mar 20 '16 at 18:31
0

The foreach statement is used to iterate through the collection to get the information that you want, but can not be used to add or remove items from the source collection to avoid unpredictable side effects.

The element j is iterated as an item in the collection n. Since n is an integer array , the type of j is int.

suppose : n = [1,2,3,4,5] then within foreach (int j in n) , j is the element in each iteration, like 1 in first iteration , 2 in second and goes on till the last element. Thus j contains the array element in each iteration, not the index but the value of consecutive index's at each step

A foreach loop like foreach(int i in obj) {...} is executed like :

var tmp = obj.GetEnumerator();
int i; // up to C# 4.0
while(tmp.MoveNext()) {
    int i; // C# 5.0
    i = tmp.Current;
    {...} // your code
}

But if you need to add or remove items from the source collection, use a for loop.

Tushar Gupta
  • 15,504
  • 1
  • 29
  • 47
0

You're just confused of what foreach does.

foreach (int j in n )
{
  int i = j - 100;
  Console.WriteLine("Element[{0}] = {1}", i, j);
  i++;
}

Equals to,

for (int index = 0; index < n.Length; index++) {
  int j = n[index]; // <---- THIS IS YOUR `j`
  int i = j - 100;
  Console.WriteLine("Element[{0}] = {1}", i, j);
  i++;
}
choz
  • 17,242
  • 4
  • 53
  • 73
  • Hello, many thanks, RIGHT THAT!!! – D Kk Mar 20 '16 at 18:45
  • Please, if I want the array not to be int but double array, how would the same code look like? http://pastebin.com/ZZDcxGzD I want n[0] to be 100.22 , n[1] to be 101.22, n[3] to be 102,22 etc... – D Kk Mar 20 '16 at 18:47
  • @DKk Sorry for just replying now. It'd be `double[] n = {100.22, 101.22, 0, 102.22};`. Note that n[2] is `0` as what you're asking. – choz Mar 22 '16 at 13:57
0

I will try an explain it using a real world example:

Imagine we have a restaurant full of people, and we want to give each person a chocolate bar, we could think of it as:

For each person in the restaurant{give a chocolate bar}

If we take this concept a step closer to code we could say:

foreach(person in restaurant){give chocolate bar}

Now for this statement to make any sense, we need to know which restaurant we are talking about!

So let's give our restaurant a name: TehPizzaPalace

So the result is:

foreach(Person p in TehPizzaPalace){Give chocolate bar}

Basically a restaurant is a collection of people, and Person is just stating the type of object we are expect to extract from TehPizzaPalace.

If we didn't know that the restaurant was a collection of people, we could instead use

foreach(var p in TehPizzaPalace){Give chocolate bar}

In the statement: foreach (int j in n){do something} For each integer, which we will name j, in the array n do something.

Ehren
  • 11
  • 3