For loop test condition

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2009
Posts: 2
Reputation: bigsurya is an unknown quantity at this point 
Solved Threads: 0
bigsurya bigsurya is offline Offline
Newbie Poster

For loop test condition

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

where 's' is a character array
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,405
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 114
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso
 
0
  #2
25 Days Ago
Your condition for that loop is:
  1. (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.
I need pageviews! most fun profile ever :)
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 10
Reputation: snehil_khanor is an unknown quantity at this point 
Solved Threads: 0
snehil_khanor snehil_khanor is offline Offline
Newbie Poster
 
2
  #3
25 Days Ago
yes u can .. but m afraid it would be an infinite loop...
your test condition is: s[i]!='\0')|| s[i]!=' '
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[i]='\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[i]='/0' or ' ' any of these..
hope u get it..
njjoy!!
- Snehil
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 2
Reputation: bigsurya is an unknown quantity at this point 
Solved Threads: 0
bigsurya bigsurya is offline Offline
Newbie Poster
 
0
  #4
24 Days Ago
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
Reply With Quote Quick reply to this message  
Reply

Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC