15,667,475 members
See more:
Hello, I'm trying to write a condition in a while-loop and it still doesn't work for me, or rather numbers that don't match the condition always pass. Does anyone have advice on how to write a condition to work? I try to achieve that there is a non - residential division and at the same time that the result is positive and no later than 10...

What I have tried:

EDIT:
C#
```private int maxValueDivision = 100;
private int maxValue = 10;

int secondNumb = number.Next(1, maxValue);
int firstNumb = number.Next(1, maxValueDivision);
while (firstNumb % secondNumb != 0 &&  firstNumb / secondNumb < 10 )
{
secondNumb++;
}
total = firstNumb / secondNumb;```

EDIT2:
C#
```private int maxValueDivision = 100;
private int maxValue = 10;

int secondNumb = number.Next(1, maxValue);
int firstNumb = number.Next(1, maxValueDivision);
while (firstNumb % secondNumb != 0 &&  firstNumb / secondNumb < 10 )
{
firstNumb++;
if(firstNumb>100)
{
firstNumb = number.Next(1, maxValueDivision);
}
}
total = firstNumb / secondNumb;```

How do I ensure that secondNumb is always less than 10 but still maintains a residual division?

Posted
Updated 28-Jun-21 2:21am
v3
Ralf Meier 28-Jun-21 5:14am
Perhaps it is easier if you try to describe what you want to achieve ...
Member 15170612 28-Jun-21 5:16am
You have it in question, look at the last sentence...
Ralf Meier 28-Jun-21 5:21am
I asked you to describe what should happen - as you see by the answer of OriginalGriff your code itself makes no real sense.
So what should happen ?
Member 15170612 28-Jun-21 6:07am
I would like the while-loop to end as soon as the division of firstNumb and secondNumb has no remainder, at the same time the value of the result of their division would not be higher than 10 and at the same time the value of secondNumb is not higher than 10.

## Solution 1

Well, first off, this part will never be true:
C#
`while (... && (firstNumb / secondNumb < 0 || ...))`
Because your random numbers will always be 1 or greater: so 0 isn't possible, much less negative numbers!
So the condition you are evaluating is:
C#
`while (firstNumb % secondNumb != 0 && firstNumb / secondNumb > 10)`
But ... your second condition will rarely match either:
C#
`while (... && firstNumb / secondNumb > 10)`
Because `firstNumb` is 1 .. 99, and `secondNumb` is 1 .. 9.
So unless `firstNumb` is high, and `secondNumb` is low, the result of the division is never going to be greater than 10.
In fact, if `secondNum` is 5 for example, then `firstNumb` has to be 55 or higher for the condition to be true. For 6, that's 66+; for 9 there is only one value of `firstNumb` that will give you true: 99!

You have stacked the odds against you in getting that loop to go round at all!

Quote:
Do you mean, generate a secondNumb and then add firstNumb ++ in a while-loop?

You could, but ... why use a loop at all?
When you use a loop, your code become non-deterministic in that you do not know how long it will take to generate a valid value.
The way I'd do it is to generate secondNumb, and use that as and index into an array of arrays. Each "inner array" contains only the "valid values" for that index:
```1:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99
2:
2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99
3:
3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99
4:
4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99
5:
5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99
6:
6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99
7:
7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99
8:
8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99
9:
9, 18, 27, 36, 45, 54, 63, 72, 81, 90, 99```

Then generate a second random number which selects one of them.

That way, your code has no loops, only generates valid values, and is time deterministic.

v2
Member 15170612 28-Jun-21 5:25am
I added a negative value there because if I add secondNumb ++ it is able to get into negative values, I made a mistake with a small number of the second number which I did not realize but I still don't know how to write it to be just a small multiplier . Thank you.
OriginalGriff 28-Jun-21 5:50am
You are going to have to explain what you expect the code to do, I have no idea from "faulty" code!
Member 15170612 28-Jun-21 5:57am
I would like the while-loop to end as soon as the division of firstNumb and secondNumb has no remainder, at the same time the value of the result of their division would not be higher than 10 and at the same time the value of secondNumb is not higher than 10.
OriginalGriff 28-Jun-21 6:21am
So ...
while (!(c1 && c2 && c3))
then?
Member 15170612 28-Jun-21 6:26am
while (firstNumb % secondNumb != 0 && firstNumb / secondNumb > 10 && secondNumb>10) Make it sense?

## Solution 2

Quote:
How to write a while-loop condition?

Basically, exactly as you did, but anything depend on the values of variables.
C#
```int secondNumb = number.Next(1, maxValue);
int firstNumb = number.Next(1, maxValueDivision);
// here print secondNumb
// here print firstNumb
// here print firstNumb % secondNumb
// here print firstNumb / secondNumb
while (firstNumb % secondNumb != 0 && (firstNumb / secondNumb < 0 || firstNumb / secondNumb > 10))```

And see if values are within range

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

Debugging C# Code in Visual Studio - YouTube[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.