klmtsgw
klmtsgw

Reputation: 13

How can I skip loop for first and last element of array and set them to constant value?

I would like the loop to start from the second element and to end before the last one. So that I can set the first and last one to constant value.

for (int i = 0; i < n; i++) {
  for (int j = 0; j < 3; j++) {
    Console.Write($"A[{i},{j}] = ");
    tab[i, j] = double.Parse(Console.ReadLine());
  }
}

Upvotes: 0

Views: 1323

Answers (3)

oded bartov
oded bartov

Reputation: 431

Why don't do something like that?

for (int i = 1; i < n - 1; i++)
{
  //your code
}

Upvotes: 1

Salah Akbari
Salah Akbari

Reputation: 39966

You can use LINQ simply:

var result = yourArray.Skip(1).SkipLast(1).Prepend(theConstValue)
                      .Append(theConstValue).ToArray();

The Prepend, Adds a value to the beginning of the sequence. The Append, Appends a value to the end of the sequence.

Upvotes: 2

Patricia
Patricia

Reputation: 2865

Let the loop start at element 1 and end it one earlier

Upvotes: 0

Related Questions