[Solved] C# Error In Loop Syntax [closed]


The question is quite vague, because you’re not explaining what you’re expecting in the first place. However, I do see there is something wrong with the for loop if you’re expecting to increment the value of Num1 with the fac variable as a result of the for loop:

  1. You won’t be incrementing Num1 with the fac variable. You’ll need a statement within
    the for loop to increase the value of Num1.

  2. The part with “beg = Num1 + fac” prevents the beg variable from incrementing. For instance, if Num1 has the value of 0.0m (a decimal value) and fac has the value of 1.1m, then beg’s value will always remain 1.1m. This defeats the purpose of the incrementer that the for loop relies on to prevent an infinite loop.

Here’s the updated piece of code. I did remove the input functionality for simplicity’s sake:

decimal Num1 = 0.0m;
decimal Num2 = 2.2m;

char Op = 'I';

if (Op == 'I' || Op == 'i')
{
    decimal fac = 1.1m;

    for (decimal beg = Num1; beg <= Num2; beg += fac)
    {
        Console.WriteLine("Value of beg is: " + beg);

        Num1 = beg;

        Console.WriteLine("Value of Num1 has been updated to: " + Num1);
    }
}

1

solved C# Error In Loop Syntax [closed]