Can I have a FOR Loop of in the following manner:[for(int i=0;(s!='\0')|| s!=' ';i++)][/CODE=C++]

where 's' is a character array

Your condition for that loop is:

(s[i] != '\0') || (s[i] != ' ')

Now think, is there any way that expression will ever be false in order to break out the loop? If s is a null-terminator, the loop wont break as s isn't a space, and the same the other way round.

The condition would make sense if instead of having ||, you used &&, but I don't know your intentions.

yes u can .. but m afraid it would be an infinite loop...
your test condition is: s!='\0')|| s!=' '
which means if any of these two conditions is true then overall condition will be true which means loop will be incremented...
Here is the Truth table for OR:
False||False=False
False||True=True
True||False=True
True||True=True

which means condition will only be false when both conditions are false.. i.e loop will break only when s='\0' as well as =' ' at the same time .. which is not actually possible...

but if u want loop to be terminated.. then.. use AND
truth table for AND is:
False&&False=False
False&&True=False
True&&Fasle=False
True&&True=True

which implies condition will fail if any 1 of the condition is false.. i.e if s='/0' or ' ' any of these..
hope u get it..
njjoy!!
- Snehil :)

Comments
Good reply

hey guys...thanks for the replies...I'm sorry to have put tht kinda inexplicable code...actually tht loop is gonna traverse down the array n not up/// anyway i jst wanted to knw whether i can hv a test condition with a '||'...thanks a lot

This article has been dead for over six months. Start a new discussion instead.