Condition IF - Good use practice

0

During a coding I had to perform the development as follows:

IF (Condicao1 = True)
  IF (Condicao2 = True)
    IF (Condicao3 = True)
       Imprime(Mensagem);

Is the way I performed best practice?

Would there be a better way to accomplish this?

    
asked by anonymous 20.08.2018 / 14:32

1 answer

2

Apparently the way you structured is not the best. I say this because you do not use keys in ifs , which suggests that they are only for what is being displayed in the code, ie validate if the three conditions are true; being, prints . So the best alternative is to use and , as already said in a comment:

IF (Condicao1 && Condicao2 && Condicao3)
    Imprime(Mensagem);

However, if there are commands to be executed in case some of them are false, you have structured them correctly.

IF (Condicao1 = True) {
  IF (Condicao2 = True){
    IF (Condicao3 = True){
       Imprime(Mensagem);
    } else {
      // codigo
    }
  } else {
    //codigo
  }
} else {
  //código
}

If you do not need any of the "snags" , use the first form.

    
20.08.2018 / 15:00