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

Recommended Answers

All 3 Replies

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

commented: Good reply +1

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.