954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C && Operator

The result of this code is 65535 But I believe that for && both sides must hold true (rather one side being true is enough for ||) so how come the result is 65535 ????? when after 5++ the first condition becomes false ????? Thanks in advance

#include<stdio.h>
#include<conio.h>

void main()
{
 short int i=0;
 clrscr();

 for(i<=5&&i>=-1;++i;i>0)
 printf("%u",i);
 getch();
}
ram619
Light Poster
46 posts since Mar 2010
Reputation Points: 6
Solved Threads: 0
 

The for loop is completely nonsensical. It's like this:

for (initialization; condition; increment)
{
    body
}

Where the roughly analogous while loop is:

initialization

while (condition)
{
    body
    increment
}

So your loop, for(i<=5&&i>=-1;++i;i>0) printf("%u",i); would look something like this while loop:

i <= 5 && i >= 1;

while (++i)
{
    printf("%u", i);
    i > 0;
}

Doesn't make much sense, does it?

deceptikon
Indubitably
Administrator
632 posts since Jan 2012
Reputation Points: 119
Solved Threads: 105
 

Thanks deceptikon got it now......... the && is just initialization.
:)

ram619
Light Poster
46 posts since Mar 2010
Reputation Points: 6
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You