How to stop printing positions = 0 of a vector?

0

In the example below, how would I stop printing the positions of this vector that are equal to 0

int[] caluculo = {50,6,0,59,6,7,0,6,8};

for(int i = 0 ; i < 9 ; i++)
{    
    Console.WriteLine("{0}",calculo[i]);    
}
    
asked by anonymous 24.02.2018 / 15:16

1 answer

5

Whenever you need to filter something you should if :

for (var i = 0 ; i < 9 ; i++) if (calculo[i] != 0) WriteLine(calculo[i]);

Or you can do

foreach (var item in calculo) if (item != 0) WriteLine(item);

You can also use LINQ, but I do not think it compares and is more advanced.

    
24.02.2018 / 15:20