By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,717 Members | 1,233 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,717 IT Pros & Developers. It's quick & easy.

Why this way and not that way

Claus Mygind
100+
P: 571
I am curious why an if statement would yield the wrong response. In example 1 the two lines StartMiles and EndMiles does not include the strings "&StartMiles" and "&EndMiles". in the qString

Here are two sample code segments

Example 1: gives a result like this "...Tolls=000..."
Expand|Select|Wrap|Line Numbers
  1. qString += "&TOLLS"+i+"="+aDailyTime[i].TOLLS;
  2. qString += "&StartMiles"+i+"="+( aDailyTime[i].STARTMILES == "" ) ? 0 : aDailyTime[i].STARTMILES;
  3. qString += "&EndMiles"+i+"="+( aDailyTime[i].ENDMILES == "" ) ? 0 : aDailyTime[i].ENDMILES;
  4.  
Example 2: gives a result like this "...&Tolls=0&StartMiles=0&EndMiles=0...."
Expand|Select|Wrap|Line Numbers
  1. qString += "&TOLLS"+i+"="+aDailyTime[i].TOLLS;
  2. qString += "&StartMiles"+i+"=";
  3. qString += ( aDailyTime[i].STARTMILES == "" ) ? 0 : aDailyTime[i].STARTMILES;
  4. qString += "&EndMiles"+i+"=";
  5. qString += ( aDailyTime[i].ENDMILES == "" ) ? 0 : aDailyTime[i].ENDMILES;
  6.  
Dec 8 '08 #1
Share this Question
Share on Google+
3 Replies


acoder
Expert Mod 15k+
P: 16,027
The reason why the first example gives an incorrect result is that all of the code until the ? is regarded as part of the if condition. To avoid that, put brackets around the ?: part:
Expand|Select|Wrap|Line Numbers
  1. qString += "&StartMiles"+i+"="+ (( aDailyTime[i].STARTMILES == "" ) ? 0 : aDailyTime[i].STARTMILES);
Dec 8 '08 #2

Claus Mygind
100+
P: 571
Thanks it is great to build one's knowledge base
Dec 8 '08 #3

acoder
Expert Mod 15k+
P: 16,027
It sure is! Glad to help.
Dec 8 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.