[solved]Correct syntax for an if-clause

Hi, I’m trying to use a nested If-Clause.
Looking around the examples the syntax for the if clause is decribed as:

if (CheckStatement) {
SeveralStatements}

Which is like C or Java
However the only way it works for me is like:

     if (CheckStatement) then
        SeveralStatements
    end if

Which is more like Basic, however when using nested If-clauses only a single one is treated.
For example:

 if (CheckStatementA) then
            if (CheckStatementB) then
                SeveralStatementsB
           end if
           if (CheckStatementC) then
                SeveralStatementsC
           end if
 end if

all CheckStatements (A,B, and C) are true, however only ServeralStatementsB are performend-

???

This is the correct syntax.

I’m not sure how that works at all. That syntax is incorrect.

This is how a nested conditional should look

if(CheckStatementA) {
    if(CheckStatementB) {
        SeveralStatementsB
    }
    if(CheckStatementC) {
        SeveralStatementsC
    }
}

I’m not sure how anything at all is executing using that syntax.

1 Like

I was expecting the same, however in my case even the most outer If doesn’t work if I use the curly brakes.
Maybe its me, since I have long history in using Basic in several versions (last B4Android).

I’m using OH2, but that shouldn’t make such a syntax-change. I’m a bit lost

If it doesn’t work that implies that the CheckStatements are not correct.

Here is my code:

  var String OK = transform("JSONPATH", "$.ok", json)
  logInfo("Benzinpreise1", OK)
    if (OK="true") {
        Status = transform("JSONPATH", "$.prices.a53fff71-133c-48b1-84ba-20a9be6ec538.status", json)
        logInfo("Benzinpreise2", Status)
        if(Status="open") {
            E10 = transform("JSONPATH", "$.prices.a53fff71-133c-48b1-84ba-20a9be6ec538.e10", json)
            logInfo("Benzinpreise3", E10)
         }
        Status = transform("JSONPATH", "$.prices.3e563b17-f3fc-4a3d-b486-60d25e5948d5.status", json)
        logInfo("Benzinpreise4", Status)
        if (Status="open") {
            E10 = transform("JSONPATH", "$.prices.3e563b17-f3fc-4a3d-b486-60d25e5948d5.e10", json)
            logInfo("Benzinpreise5", E10)
        }
        Status = transform("JSONPATH", "$.prices.bc01b1b9-b2b9-4f78-98ef-b7ee8d8a66a4.status", json)
        logInfo("Benzinpreise6", Status)
        if (Status="open") {
            E10 = transform("JSONPATH", "$.prices.bc01b1b9-b2b9-4f78-98ef-b7ee8d8a66a4.e10", json)
            logInfo("Benzinpreise7", E10)
          }
    }
    logInfo("Benzinpreise8", "Exit") 
end

The log shows:
09:25:00.240 [INFO ] [smarthome.model.script.Benzinpreise1] - true
09:25:00.246 [INFO ] [marthome.model.script.Benzinpreise8] - Exit

So OK is “true” but the code inside the clause isn’t performed, no log with “.Benzinpreise2”!

you did type

if(OK="true")

which is incorrect.
it has to be

if(OK=="true")

a=b -> let’s set a = b
a==b -> compare, if a is equal to b

In fact, this is wrong in every if-clause.

In question of the logInfo, better write

logInfo("Benzin","Benzinpreis 1 = {}",OK)

as the first arg is the logger name, the second arg is the log to print. If you want to use more arguments to dynamically change the log line, add curly brackets for every value and add the args as a comma separated list…

1 Like

Thanks for the coding lesson!

Yes, my syntax was crying out loud, I’m a BASIC programmer! There are some more pittfalls when changing to C or JAVA, it’s not only the “writeln” instead of “write” and the required ; at end end of each statement. I totally forgot.
Works like a charm now!

Just to be clear, the Rules programming language is a Domain Specific Language (DSL) that bears the most resemblance to Xtend. While Java classes are available within Xtend and therefore the Rules (DSL), this language bears little resemblance to Java or C in most respects.

Understood!