Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Unexpected behavior with in-line if statements

    I experienced a quirk with Stata. It appears that if statements read (and evaluate) the entire line, even if the condition is false. For example:

    Code:
    local    c=0
    
    forval j=1(1)100{
      /* this below statement is true for 10(10)100 */
      if      mod(`j',10)==0        local `++c'
    }
    
    di     "The count is `c'"
    displays
    Code:
     The count is 100
    Instead of 10, which would be the result of Stata didn't read increment c when the condition is false. Is this an error or should we expect Stata to read (and evaluate) the rest of the line after the if statement?

    Having an if block avoids this problem. Please let me know if there any any other workarounds, or if this is an issue I should contact Stata about.

  • #2
    Originally posted by Quinton Baker View Post
    It appears that if statements read (and evaluate) the entire line, even if the condition is false.
    Not quite. Instead of

    Code:
    if mod(`j',10)==0 local `++c'
    code

    Code:
    if mod(`j',10)==0 local ++c
    It is the single quotes (`') that are causing evaluation regardless of whether the expression is true of false.

    Best
    Daniel

    Comment

    Working...
    X