1

I am getting different results for changings order of "d + 20" and "print("d is smaller than 100")" within if-else in R. Please see the code below and help explain the difference.

As far as I know, value of a compound expression is the value of the last expression, but this does not help explain this case:

Code #1:

d <- 12
if (d > 100) {
    print("d is greater than 100")
} else {
    d + 20
    print("d is smaller than 100")
}
Result:
[1] "d is smaller than 100"

Code #2:

d <- 12
if (d > 100) {
    print("d is greater than 100")
} else {
    print("d is smaller than 100")
    d + 20
}
Result:
[1] "d is smaller than 100"
[1] 32
Phung Doan
  • 25
  • 4
  • Nothing in your output looks unexpected to me. – Tim Biegeleisen Aug 21 '23 at 14:09
  • Can you explain please? – Phung Doan Aug 21 '23 at 14:14
  • 3
    "As far as I know, value of a compound expression is the value of the last expression, but this does not help explain this case" That's exactly the explanation. As `help("print")` points out, `print` returns its argument *invisibly*, i.e. without auto-printing it. – Roland Aug 21 '23 at 14:25

1 Answers1

1

If else statements return by default the last expression. All other expressions are also being evaluated but not being returned (or saved) in this example:

if(TRUE){
  1+1
  2+2
  3+3
}

Result:
[1] 6

If you wrap expressions into print() statements they will also all be evaluated (like above) which prints them to the console.

if(TRUE){
  print(1+1)
  print(2+2)
  print(3+3)
}

Result:
[1] 2
[1] 4
[1] 6

Besides printing the value to the console, print() statements also return the argument but in an invisible manner. The primary effect of print() is displaying it to the console, but the function will also return the argument invisibly without printing it again to the console. In your code example 1

d <- 12
if (d > 100) {
    print("d is greater than 100")
} else {
    d + 20
    print("d is smaller than 100")
}
Result:
[1] "d is smaller than 100"

only "d is smaller than 100" is being printed as a result of the execution of the print() statement, since the last expression which is being returned is the invisible return of the print() statement and not the d + 20 expression. The d + 20 expression will be evaluated just like the 1+1 and 2+2 in my first example, but it won't be returned since it is not the last expression in the block. The last expression is the invisible return of print().

I hope this makes sense!

klosinho
  • 85
  • 6
  • Hi Klosinho, so the print() statement will return the argument in the console regardless of its place. On the other hand, operations, such as 3 + 3 or d + 20, must be placed in the last expression of the block to be returned in the console. Can I understand in this way? Anyway, thank you very much for your explanation. It helps a lot! – Phung Doan Aug 21 '23 at 20:41
  • 1
    I would say that the print() statement will print (!) the argument to the console regardless of its place. If a print statement is the last expression in a block it will print the argument but additionally return the argument silently. Since it is silent we only see it once in the console due to the printing and not due to returns. If it would return it non-silently you would see the argument twice in the console, one time from printing and one time from returning. You might want to check the difference between printing and returning. Otherwise I agree with your description. – klosinho Aug 22 '23 at 12:33