How to make so that did not give an error

The index was outside the bounds of the array.

in the absence of zero. That is, what 2 condition is needed?

using System; namespace whileHomeWork { class Program { static void Main() { int[] intArray = {2, 3, 5, 9, 4, 7, 2}; int index = 0; while (intArray[index] != 0) { Console.WriteLine(intArray[index]); index++; } } } } 
  • index <intArray.Length - tym32167
  • tried it! it is necessary that if there is zero then the cycle stopped and, if not, it reached the end and stopped. - j. Atisto 4:06 pm
  • what specifically tried? What code? - tym32167
  • while (intArray [index]! = 0 || index <intArray.Length) - j. Atisto pm
  • one
    And so? while (index < intArray.Length && intArray[index] != 0) - Dmitry D.

1 answer 1

Try to add a loop condition like this:

 while (index < intArray.Length && intArray[index] != 0)