Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have a question. How do I convert an IF-statement to a WHILE loop without doing an infinite loop? Every time I tried it, I overloaded the computer and had to turn it off.And when I use the IF condition, it doesn't solve my problem at all, and the code continues. I would like the code to generate two random numbers in my worksheet each time I run it, which will not be the same and will not be the same with the correct result.
This is my code:
private void addToList()
{
    int resultNumber;
    failResult = number.Next(1, maxValue);            
    resultNumber = Int32.Parse(result.Text);
    var rtnlist = new List<int> { resultNumber};
    if (failResult != resultNumber && failResult != wrongResult)
    {
        rtnlist.Add(failResult);
    }
    wrongResult = number.Next(1, maxValue);   
    if (wrongResult != resultNumber && wrongResult != failResult)
    {
        rtnlist.Add(wrongResult);
    }
}


What I have tried:

C#
private void addToList()
{
    int resultNumber;
    failResult = number.Next(1, maxValue);            
    resultNumber = Int32.Parse(result.Text);
    var rtnlist = new List<int> { resultNumber};
    while (failResult != resultNumber && failResult != wrongResult)
    {
        rtnlist.Add(failResult);
    }
    wrongResult = number.Next(1, maxValue);   
    while(wrongResult != resultNumber && wrongResult != failResult)
    {
        rtnlist.Add(wrongResult);
    }
}

EDIT:
C#
private void addToList()
{
    int resultNumber;
    failResult = number.Next(1, maxValue);            
    resultNumber = Int32.Parse(result.Text);
    var rtnlist = new List<int> { resultNumber};
    while (failResult == resultNumber && failResult == wrongResult)
    {
        failResult++;
    }
rtnlist.Add(failResult);
    wrongResult = number.Next(1, maxValue);   
    while(wrongResult == resultNumber && wrongResult == failResult)
    {
        wrongResult++;
    }
rtnlist.Add(wrongResult);
}

I still get two identical values ​​sometimes (the second value is always the same as the result).
I don't know if I should use do-while or I'm just doing it wrong. I will be happy for any advice.

Thank you.
Posted
Updated 15-Jun-21 0:30am
v2

Learn to debug. It is simple, and the time investment pays of immediately. I understand that it might seem like "I have a lot to learn, so I will put that off and learn it later - for now I just want my program working", but that is a REALLY bad idea. Debugging saves time from day one. You should consider 90% or more of your learning time wasted until you spend 15 minutes learning how to use the debugger). Here are the docs from Microsoft, but you can easily google tutorials, youtube videos, or whatever you prefer learning from:
First look at the debugger - Visual Studio | Microsoft Docs[^]

The debugger will allow you to step through your loop line by line, allowing you to see the values of each variable - and even evaluate the entire expression in the while loop. No more guessing what is happening, you can see exactly what happens and why.

In your case, you would see
failResult != resultNumber && failResult != wrongResult
never change value. If it is true the first time the code is executed, it will be true the second time. When using a while loop, you need something inside the loop that changes the variables the loop condition use.

Another small side note: The task manager can stop your program without turning the computer off. And if you use a debugger, you do not even need that. Just stop debugging. :)
 
Share this answer
 
Comments
Member 15170612 15-Jun-21 6:11am    
I stuck the computer so bad that even the task manager couldn't start :D Thank you for your advice.
C#
while (failResult == resultNumber && failResult == wrongResult)

A variable cannot be equal to two different values at the same time.
 
Share this answer
 
Comments
Member 15170612 15-Jun-21 6:36am    
I didn't know that, so I might ask, what if it had to meet both conditions?
Greg Utas 15-Jun-21 7:18am    
Then resultNumber had better be equal to wrongResult!

If something has to meet both conditions, using && is correct, but the overall logic must allow it to happen.
Richard MacCutchan 15-Jun-21 7:21am    
If it has to meet both conditions then both values must be the same; but that makes no sense. You should edit your question and explain in more detail what you are trying to do.
Member 15170612 15-Jun-21 8:15am    
I already figured it out and you were right, I didn't think about it at all and it didn't make sense.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900