Search code examples
if-statementrascal

Rascal syntax error in if statement (newbie q)


What's wrong with the following?

I'm getting a syntax error on the last line, just after the if

Module mytest

int n = if (3 > 2) 1; else 0;

thx, --Dennis.


Solution

  • Top-level declarations in Rascal (which include declarations in the console) expect an expression on the right-hand side, so you would need to instead say:

    int n = ( 3 > 2 ) ? 1 : 0;
    

    If you are inside a function, there is an inconsistency in what is allowed, so this should work fine:

    n = if (3 > 2) 1; else 0;
    

    but what you have above also won't work in that context.