I see some code of some guy that goes like this:
while (!(baseType == typeof(Object)))
{
....
baseType = baseType.BaseType;
if (baseType != null)
continue;
break;
} while (baseType != typeof(Object));
What is while(...) {...} while(...)
statement?
is the following equivalent code?
while (baseType != null && baseType != typeof(Object))
{
....
baseType = baseType.BaseType;
}
The while loop is not run because the condition is not met. After the running the for loop the value of variable i is 5, which is greater than three. To fix this you should reassign the value before running the while loop (simply add var i=1; between the for loop and the while loop).
Using multiple conditions As seen on line 4 the while loop has two conditions, one using the AND operator and the other using the OR operator. Note: The AND condition must be fulfilled for the loop to run. However, if either of the conditions on the OR side of the operator returns true , the loop will run.
The while statement lets you repeat a statement until a specified expression becomes false.
Because the while loop checks the condition/expression before the block is executed, the control structure is often also known as a pre-test loop.
There is no while() ... while();
statement, so it's really two while statements, like:
When they have the same condition, like in your example, the second one is useless.
Actually, doing some testing I came to realise that it's actually two loops, like:
while(...) { ... }
while(...);
You have two while statements in a row. The 2nd could end up as an endless loop, because the first runs until the first condition is true or baseType becomes null. Then the 2nd loop starts:
while (baseType != typeof(Object));
If baseType is not changed by another thread, the loop won't terminate. Because the first loop checks the same condition, the 2nd is never run, except when baseType is null.
Your code is not exactly the same, because the first code breaks the loop if baseType is null and then ends in the endless loop. I would prefer your code, it's a lot clearer. Try to avoid continue and break.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With