Home > Net >  for loop not executing/running correctly
for loop not executing/running correctly

Time:11-23

Here's the code can anyone help?

using System;
namespace Mathhero
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            int i;
            for (i = 0; i <= 10; i  )
            {
                Random numgen = new Random();
                int num1 = numgen.Next(1, 11);
                int num2 = numgen.Next(1, 11);
                Console.WriteLine("What is "   num1   " * "   num2   " equal to ???");
                int Answer = Convert.ToInt32(Console.ReadLine());
                if (Answer == num1 * num2)
                {
                    int ran = numgen.Next(1, 4);
                    switch (ran)
                    {
                        case 1:
                            Console.WriteLine("Good work!!");
                            break;
                        case 2:
                            Console.WriteLine("Nice!!!");
                            break;
                        default:
                            Console.WriteLine("Excellent!!");
                            break;
                    }
                    Console.WriteLine();
                }
                else
                {
                    int ran = numgen.Next(1, 4);
                    switch (ran)
                    {
                        case 1:
                            Console.WriteLine("Wrong!!");
                            break;
                        case 2:
                            Console.WriteLine("Try hard!!!");
                            break;
                        default:
                            Console.WriteLine("DO homework!!");
                            break;
                    }
                    Console.WriteLine();
                }
                i=i  1;
            }
            Console.WriteLine("Test Ended!!!");
        }
    }
}

The for loop is exiting after 6 questions while it should after 10.

CodePudding user response:

You are increasing your index within the for loop and thus shortening it's run.

 else
            {
                int ran = numgen.Next(1, 4);
                switch (ran)
                {
                    case 1:
                        Console.WriteLine("Wrong!!");
                        break;
                    case 2:
                        Console.WriteLine("Try hard!!!");
                        break;
                    default:
                        Console.WriteLine("DO homework!!");
                        break;
                }
                Console.WriteLine();
            }
            i=i  1;   //// <------- HERE
        }

Your for loop already takes care of incrementing the index by itself.

CodePudding user response:

Your for loop "header" increments i in the normal way:

for (i = 0; i <= 10; i  )

However, within the loop you also increment i, for no apparent reason:

i=i  1;

So you're incrementing i twice for each iteration of the loop. That makes it reach its end point earlier.

To execute 10 times, you should have:

for (i = 0; i < 10; i  )

(note the use of < rather than <=) and remove the i = i 1; statement entirely.

I'd also recommend the general principle of declaring each variable in as small a scope as you can - in this case, in the for loop:

for (int i = 0; i < 10; i  )

That's more idiomatic and "tidy" IMO than declaring it before the loop.

CodePudding user response:

the loop will run for 11 times as you are initializing i=0 and i<=10 so should check this... Second i don't understand why are you doing i 1 at the end because loop is already incrementing so you should check this also..

  • Related