Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unknown while while statement

Tags:

c#

.net

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;
}
like image 340
Naor Avatar asked Aug 23 '11 11:08

Naor


People also ask

Why is my while statement not working?

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).

Can a while statement have two conditions?

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.

What is the while statement?

The while statement lets you repeat a statement until a specified expression becomes false.

What is another name of while loop statement?

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.


2 Answers

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.

Edit:

Actually, doing some testing I came to realise that it's actually two loops, like:

while(...) { ... }
while(...);
like image 107
Guffa Avatar answered Oct 14 '22 09:10

Guffa


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.

like image 39
slfan Avatar answered Oct 14 '22 11:10

slfan