Reputation: 1605
how to skip writing the value to the file if it = 0 in my calculator program
Procedure that writes the array into a file
public void SaveArrayToFile()
{
int count;
var writer = new System.IO.StreamWriter("C:/calc/calculations.txt",false);
for (count = 0; count <= Results.Length -1 ; count++)
{
if (Results[count] == 0)
{
// problem
}
writer.Write(Results[count]);
writer.WriteLine();
}
writer.Dispose();
Any help would be valued
Upvotes: 2
Views: 3741
Reputation: 149020
That's exactly what the continue
statement is for.
if (Results[count] == 0)
{
continue;
}
You could also solve this problem using Linq:
foreach (var result in Results.Where(r => r != 0))
{
writer.Write(result);
writer.WriteLine();
}
Upvotes: 6
Reputation: 32481
Use the continue
//your code
if (Results[count] == 0)
{
continue;
}
//your code
More (Jump Statement):
break
keyword.continue
Upvotes: 4
Reputation: 230
if (Results[count] != 0)
{
writer.Write(Results[count]);
writer.WriteLine();
}
Upvotes: 5
Reputation: 283684
Either
if (Results[count] == 0)
{
continue;
}
writer.WriteLine(Results[count]);
or even simpler
if (Results[count] != 0)
{
writer.WriteLine(Results[count]);
}
Upvotes: 12