problem with a series of if statements

My app has a series of if statements. some of them a executed even when the condition is not met.

 if viewModel.npH > 7.45 {
        diagnos.resultado1 = "alcalemia"
        print ("1")
        }
      if viewModel.npCO2 < 35 {
        diagnos.resultado2 = "alcalosis respiratoria"
        print ("2")
        }
      if viewModel.nHCO3 > 26 {
        diagnos.resultado3 = "alcalosis metabólica"
        print ("3")
      }
       
      if viewModel.npH < 7.35 {
        diagnos.resultado1 = "acidemia"
        print ("4")
      }
      if viewModel.npCO2 > 45 {
        diagnos.resultado2 = "acidosis respiratoria"
        print ("5")
      }
      if viewModel.nHCO3 < 22 {
        diagnos.resultado3 = "acidosis metabólica"
        print ("6")
      }

For instance when i run it with viewModel.nph = 7.4, viewModel.npCO2 = 40 and viewModel.nHCO3 = 23.9 It prints "4", "5", "6" even though none of the conditions are met. I'm obviously doing something wrong but I can't understand what is it I' m doing wrong

Answered by dtos11 in 715278022

Problem solved. Was missing a parenthesis so it was executing the closure due to the || part of the statement

Definitely unexpected based on the limited code you posted. I’d add more sanity checking by also printing the value of the tested value within each block:

if viewModel.npH < 7.35 {
    diagnos.resultado1 = "acidemia"
    print("4", viewModel.npH)
}

Does this turn up any incorrect assumptions?

The errors you shows are just layout errors (constraints), nothing to do with your initial problem.

when i run it with viewModel.nph = 7.4, viewModel.npCO2 = 40 and viewModel.nHCO3 = 23.9 It prints "4", "5", "6" 

Are you sure that's the print you get ? Isn't it 2, 4, 6 instead ?

Are the var initialised as:

func diag() {
      viewModel.npH = Double(Float(viewModel.pH) ?? 0)
      viewModel.npCO2 = Double(Float(viewModel.pCO2) ?? 0)
      viewModel.nHCO3 = Double(Float(viewModel.HCO3) ?? 0)

as defined in a previous post ?

If you get 2, 4, 6 that would mean that all values are 0 at first run.

But we miss some information to be sure of the conclusion.

So please instrument code with print, such as:

      if viewModel.npH > 7.45 {
        diagnos.resultado1 = "alcalemia"
        print ("1", viewModel.npH)
        }
      if viewModel.npCO2 < 35 {
        diagnos.resultado2 = "alcalosis respiratoria"
        print ("2", viewModel.npCO2)
        }
else if viewModel.npH > 7.45 && diagnos.resultado3 == "alcalosis metabólica" && (viewModel.expectALCPCO2 + 0.5) < viewModel.npCO2 || viewModel.npCO2 > 55 {
        diagnos.resultado4 = "acidosis respiratoria asociada"
//        diagnos.resultado2 = ""
        diagnos.valorasociado1 = "PCO2 esperada: \(viewModel.expectALCPCO2)"
        print ("8.2" , viewModel.npH)
      }

it printed 8.2 7.099999904632568

even though nph = 7.0999 it executed the closure

Accepted Answer

Problem solved. Was missing a parenthesis so it was executing the closure due to the || part of the statement

problem with a series of if statements
 
 
Q