ISourceCode

Make the frequent cases fast and the rare case correct

While loop execution in C and C# – subtle difference

while(int) would not compile in C# and would throw an error and say that the “Cannot implicitly convert type ‘int’ to ‘bool'” . In C# the while loop must strictly have a boolean expression evaluated or while(true) would work fine.

C# doesn’t allow you to use a number as a boolean, even though it’s allowed in C and C++ (where truth is nonzero and falsehood is zero)

some related discussion on stackoverflow.

#include <stdio.h>

void main()
{
        int i = 10;
        while (i)
        {
                printf("\n%d",i);
                i--;
        }
}

> ./a.out

10
9
8
7
6
5
4
3
2
1>

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: