Question

Writing a simple console application to learn a few things. The application uses a for loop and and behaves as expected, except when it reaches the while loop with start and end DateTime conditions. However the while loop just keep printing out "Graduation" for all eternity..

I simply want the while loop to run and print out "graduation" to the console for 5 seconds, then continue counting up to 2020. What am I missing?

static void Main(string[] args)
{

    DateTime start = DateTime.Now.AddSeconds(0);

    DateTime end = DateTime.Now.AddSeconds(5);


    for (int i = 1988; i < 2020; i += 2)
    {


        if (i == 2000)
        {
            Console.WriteLine("New Mellenium");

            continue;
        }



        if (i == 2006)
        {


            Console.WriteLine("Age of Majority");

            continue;
        }


        if (i == 2014)
        {

            while (start < end)
            {

                Console.WriteLine("Graduation!!!");

                continue;
            }


        }



        Console.WriteLine(i);
        Console.ReadKey();



    }

Thanks

Was it helpful?

Solution

As my first comment, you never change the start and end dateTime objects, that's the reason!

In this case, you could try checking if the end has hitted by current time.

while (DateTime.Now < end)
   Console.WriteLine("Graduation!!!");

OTHER TIPS

You have forgotten to increment your start variable at the end of your loop.

Put this at the end of your loop, after the Console.ReadKey():

start = DateTime.Now;

This program is going to run longer than 10 seconds, though, because your Console.ReadKey() will delay the program for however long it takes to hit that key for each iteration of the loop.

Change your ´while statement´ to this:

 while (start < end)
            {

                Console.WriteLine("Graduation!!!");
                start =  DateTime.Now;
                continue;
            } 

You never change the start or end date, so their values stay the same for all eternity.

Your intent is not totally clear, but I'm assuming you want "Graduation!" to be output continuously for 5 seconds:

for (int i = 1988; i < 2020; i += 2)
{
    if (i == 2000)
    {
        Console.WriteLine("New Mellenium");
        continue;
    }
    if (i == 2006)
    {
        Console.WriteLine("Age of Majority");
        continue;
    }
    if (i == 2014)
    {
        DateTime now = DateTime.Now;
        DateTime end = now.AddSeconds(5);
        while (now < end)
        {
            Console.WriteLine("Graduation!!!");
            now = DateTime.Now;                // <---- this line is the key
        }
    }
    Console.WriteLine(i);
    Console.ReadKey();
}

In this case, you're not updating start inside the while loop. It will forever have the value of whatever time it was when you called System.DateTime.Now.AddSeconds(0)

Secondly, your logic isn't right. you don't need an AddSeconds call the first time, because you're adding no seconds, which just wastes time. Second, you should set end like this: end=start.AddSeconds(5), because the two calls to System.DateTime.Now will be very slightly different, and can be drastically different under specific circumstances.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top