When should I use if without keys? [duplicate]

3

I usually use with keys because until today I do not know what exactly the operation of if without keys is and if the else can also be without keys. Is there any variation of behavior between javascript and C #? And among other languages?

Some examples that would confuse me if they work or not:

Example 1:

if(true)
    variavel = 55;
else
    variavel = 100;

Example 2:

if(true)
    variavel1 = 55;
    variavel2 = 40;
else
    variavel1 = 100;
    variavel2 = 150;

Example 3

if (true)
   variavel1 = 55;
else{
   variavel1 = 100;
   variavel2 = 200;
}
    
asked by anonymous 29.03.2016 / 18:20

1 answer

6

The IF without keys causes only the next expression to be evaluated.

In a variation of your example 2,

if(variavel == true)
    variavel1 = 55;  //Será executado apenas se variavel == true;
    variavel2 = 40;  // Será executado incondicionalmente.

Incidentally, a better way to visualize the above code would be:

if(variavel == true)
    variavel1 = 55;

variavel2 = 40; 

Notice that expression is different from line . The sequence below will exactly behave in the same way as the variation in example 2:

if(variavel == true)
    variavel1 = 55; variavel2 = 40;
//                     ^- Execução incondicional
//     ^- Execução condicional
    
29.03.2016 / 18:28