Brian Cray
Brian Cray

Reputation: 9

C# how to remove the last comma in my loop using a condition

class Program
{    
    static void Main(string[] args)
    {
        int i = 100;
        while (i >= 50)
        {
            Console.Write(i + ",");
            i--;
        }
    }
}

I am just starting in c# for my class we need to add a condition to the loop so that the last comma does not print, I cannot figure it out. Please help!!!

Upvotes: 0

Views: 1337

Answers (5)

y0_T
y0_T

Reputation: 11

Easy Solution to the problem:

        var result = string.Join(
            separator: ",",
            values: Enumerable.Range(50, 51).Reverse() // Create the needed array
        );
        Console.WriteLine(result);

Upvotes: -2

Soleil
Soleil

Reputation: 7308

You can have a good speedup by avoiding if control in the loop and writing to the console only at the end in one call:

int i = 100;
var values = new List<int>(51);
while (i >= 50)
    values.Add(i--);
var stringResult = string.Join(",", values);
Console.WriteLine(stringResult);

A functional alternative with linq:

var resultList = Enumerable.Range(50, 51).Reverse().ToList();
var resultString = string.Join(",", resultList);
Console.WriteLine(resultString);

Result:

100,99,98,97,96,95,94,93,92,91,90,89,88,87,86,85,84,83,82,81,80,79,78,77,76,75,74,73,72,71,70,69,68,67,66,65,64,63,62,61,60,59,58,57,56,55,54,53,52,51,50

Upvotes: 2

trashr0x
trashr0x

Reputation: 6565

Some ways to do this are outlined below.

1) while loop - add an if statement inline:

int i = 100;
while (i >= 50)
{
    // if i is not equal to 50, print i with a comma,
    // else i without one
    Console.Write(i != 50 ? $"{i}," : $"{i}");

    i--;
}

2) while loop - don't use an if statement at all. Print i with a comma until 51, then print 50 on it's own without a comma, outside of the loop:

int i = 100;
while (i >= 51)
{
    Console.Write($"{i},");

    i--;
}
Console.Write(i);

3) for loop - use a for loop instead of a while loop:

for (var i = 100; i >= 50; i--)
{
    // if i is not equal to 50, print i with a comma, 
    // else i without one
    Console.Write(i != 50 ? $"{i}," : $"{i}");
}

4) for loop - don't use an if statement at all. Print i with a comma until 51, then print 50 on it's own, outside of the loop:

for (var i = 100; i >= 51; i--)
{
    Console.Write($"{i},");
}
Console.Write($"{i}");

5) string.Join - let .NET do the comma work for you using string.Join and create the range using Enumerable.Range:

// this will print numbers from 50 to 100,
// Reverse() reverts the order (i.e. 100 to 50)
var csvString = string.Join(",", Enumerable.Range(50, 51).Reverse());

Console.Write(csvString);

Upvotes: 2

Kai
Kai

Reputation: 216

We call this a fencepost problem (something is different in the first or last iteration). @yellowtail's answer works, but I find it cleaner to do one fence post outside the loop.

int i = 100;
Console.Write(i--);
while (i >= 50)
{
    Console.Write("," + i);
    i--;
}

Upvotes: 1

yellowtail
yellowtail

Reputation: 443

Is this a proper solution for you?

class Program
{
    static void Main(string[] args)
    {
        int i = 100;
        int min = 50;
        while (i >= min)
        {
            if (i == min) Console.Write(i);
            else Console.Write(i + ",");
            i--;
        }
    }
}

Upvotes: 4

Related Questions