0

I am very new to C# and while loops. Does anyone know how to stop it? It keeps printing the first if-statement. Here is the code:

   const int Year = 400;
   const int LeapYear = 4;
   const int NoLeapYear = 100;
   int input = 0;
   input = int.Parse(Console.ReadLine());

   while (input != 0)
   {
       Console.WriteLine("Enter a number: ");
       if (input > 0 && input % LeapYear == 0 || input % Year == 0)
       {
           Console.WriteLine($"{input} is a leap year.");
       }
       if (input > 0 && input % NoLeapYear != 0)
       {
            Console.WriteLine($"{input} is not a leap year.");
       }
       if (input < 0)
       {
           Console.WriteLine("Year must be positive!");
       }
       if (input == 0)
       {
           Console.WriteLine("End of program");
       }
   }
4
  • 2
    0 and enter and it will stop. input != 0 means it will run until(while) the input is not zero. You also have to put the input = int.Parse(Console.ReadLine()); line inside the while loop. Commented Sep 28, 2022 at 18:17
  • the while statement literally says "run while input does not equal zero", so the conclusion is that it will stop as soon as input is 0 Commented Sep 28, 2022 at 18:18
  • 2
    Add break; after the 'end of program' printout. Make it a do/while loop, and move input = int.Parse(...) code inside the loop. Commented Sep 28, 2022 at 18:19
  • I don't think this code needs a loop at all. You should also consider elseif for 2nd, 3rd and 4th if. Commented Sep 28, 2022 at 18:38

4 Answers 4

3

You have to read the input inside the while loop:

const int Year = 400;
const int LeapYear = 4;
const int NoLeapYear = 100;
int input = -1; // initialize to something different than zero, to enter the while loop (or use do while loop instead of while loop)
while (input != 0)
{
    Console.WriteLine("Enter a number: ");
    input = int.Parse(Console.ReadLine()); // you were missing this line
    if (input > 0 && input % LeapYear == 0 || input % Year == 0)
    {
        Console.WriteLine($"{input} is a leap year.");
    }
    if (input > 0 && input % NoLeapYear != 0)
    {
        Console.WriteLine($"{input} is not a leap year.");
    }
    if (input < 0)
    {
        Console.WriteLine("Year must be positive!");
    }
    if (input == 0)
    {
        Console.WriteLine("End of program");
    }
}

Consider using a do while loop in this case.

Sign up to request clarification or add additional context in comments.

Comments

0

If you are using loop just and break to true statement It will breaks it

Comments

0
while (input != 0) {
    conditions;
    break; // Breaks the loop
}

Use the break; keyword to stop any loop in C# not just C# in many languages also break is used to stop loops

Or,

Satisfy the condition to opposite of what you have it will break/stop the loop

Comments

0

use break (https://www.w3schools.com/cs/cs_break.php)

int i = 0;
while (i < 10) 
{
  Console.WriteLine(i);
  i++;
  if (i == 4) 
  {
    break;
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.