I have a simple Do While loop that doesn't work. What I am trying to do is to collect an integer and if the number is even, continue looping but if it is not even stop looping.

I can't get it to work with my code:

  int xyz = 0;
            do
            {
                Console.WriteLine(xyz);
                    xyz++;
            }
            while (xyz % 2);

Thanks in advance...

As i am not more familiar with c# programing but on the basis of other programing i am replying to this thread

do while loop is exit control loop , it means that it will check the condition after executing statements and i can see that you didn't specify the condition

use xyz%2==0

hope this helps you to sove the issue

Well, your while condition is incorrect if you want to continue if xyz is even. The expression xyz%2 will return a non-zero value if it is odd, not even. So, try changing the while(xyz%2) condition to while ((xyz%2) == 0).

this is what I have so far, it is improving but I can't get it to continue looping if the number is positive. Any ideas?

int userinput = 0;
            string input = string.Empty;
            Console.WriteLine("Input a number to be manipulated: ");
            input = Console.ReadLine();
            userinput = int.Parse(input);

            do
            {
                Console.WriteLine(userinput);
                    userinput++;
            }
            while (userinput % 2 == 0);

i didn't got you
but i think that your code will print even number only when an even number is inputted. And it will print odd and its just next even number if inpuuted number is odd

Edited 3 Years Ago by rishif2

@rishif2,yes that is what it does. I am not sure how to fix that though.

I want to make sure it continues looping every time the new iteration is even, continue looping. If it is odd, stop looping.

I think I have something going in the right direction, does this seem right?

            int userinput = 0;
            string input = string.Empty;
            Console.WriteLine("Input a number to be manipulated: ");
            input = Console.ReadLine();

            do {Console.WriteLine(userinput); userinput++;}
            while (userinput % 2 > 0);

Don't know exactly what you are trying to achieve here. If an integer value is even and you add 1 to it, it will be odd.

 static void Main(string[] args)
        {
            int userinput = 8;
            do 
            { 
                Console.WriteLine(userinput); 
                userinput++;
            }
            while (userinput % 2 > 0);
            Console.ReadKey();
        }

The above snippet(adapted from your code) will do the following: It will print the value of 8. It will add 1 to 8, so userinput becomes 9. THe while condition detects that 9 % 2 = 1, so > 0, and will continue looping. The number 9 gets printed and userinput becomes 10. The while condition detects that 10 is an even number and exits.

This question has already been answered. Start a new discussion instead.