if (progressBar1.Value == 1) { progressLabel.Text = 1%; }if (progressBar1.Value == 10) { progressLabel.Text = 10%; }if (progressBar1.Value == 20) { progressLabel.Text = 20%; }if (progressBar1.Value == 30) { progressLabel.Text = 30%; }if (progressBar1.Value == 40) { progressLabel.Text = 40%; }if (progressBar1.Value == 50) { progressLabel.Text = 50%; }if (progressBar1.Value == 60) { progressLabel.Text = 60%; }if (progressBar1.Value == 70) { progressLabel.Text = 70%; }if (progressBar1.Value == 80) { progressLabel.Text = 80%; }if (progressBar1.Value == 90) { progressLabel.Text = 90%; }if (progressBar1.Value == 100) { progressLabel.Text = 100%; }
How to write this code without using 11 if-statements?
c#;winforms
Why can't you do:progressLabel.Text = string.Format({0}%, progressBar1.Value)As @MNZ pointed out I potentially didn't answer the question exactly how the OP asked.  So here's a slightly modified alternative option:progressLabel.Text = FormatProgress(progressBar1.Value, progressLabel.Text)private string FormatProgress(int currentProgress, string progressDisplayed){    if (currentProgress < 0 || currentProgress > 100)        throw new ArgumentOutOfRangeException(Current progress is not in a valid percentage range of 0 to 100);    if (currentProgress == 1)         return FormatProgress(currentProgress);    return currentProgress % 10 == 0 ?               FormatProgress(currentProgress) :               progressDisplayed;}private string FormatProgress(int progress){    return string.Format({0}%, progress);}And a unit test to confirm:[TestMethod]public void Test(){    Assert.AreEqual(1%, FormatProgress(1, 0%));    Assert.AreEqual(0%, FormatProgress(0, 5%));    Assert.AreEqual(10%, FormatProgress(10, 6%));    Assert.AreEqual(80%, FormatProgress(85, 80%));    Assert.AreEqual(100%, FormatProgress(100, 90%));    Assert.AreNotEqual(75%, FormatProgress(75, 70%));            }