Does anyone know how to convert int variable to bool?

Asked

Viewed 48 times

0

The L variables would be sides of a triangle, I put them as int and I’m trying to convert to bool, I saw some people saying that this is not possible, so what kind of variable will I use and what will I do now? (I only put a piece of code due to the limitations of the question)

    EQ = (L1 = L2) & (L2 = L3);
    ES = (L1 != L3); (L2 != L3) & (L3 != L1);
    TRI = (L1 < L2 + L3); (L2 < L1 + L3) & (L3 < L1 + L2);
    Console.WriteLine("Pode se formar um triângulo?, " + TRI);
    Console.WriteLine("O triângulo é EQUILÁTERO?, " + EQ);
    Console.WriteLine("O triângulo é ESCALENO?, " + ES);
    Console.Read();

1 answer

3


It seems your error is using attribution syntax L1 = L2 ("L1 receives the value of L2") instead of equality L1 == L2 ("L1 equals L2?"). Note that in the second case are two equal signs together not one.

When it’s a comparison of equality == this operation will result in a bool which can be true or false.

When it is an assignment, it will result in L1, that is, the left operand, which is not a bool and therefore it does not work the check that you are wanting to do because the operator & which is applied then expect something bool on both sides.

Obs.: Technically speaking it is not right to say that it is converting variables here of one kind into the other, in fact it is doing a comparison operation between two integers (with == or !=) and this type of operation returns a bool value. Also compare two bool using the operator & returns a bool result. Finally, assignments using = must be of one type compatible to another, in its case a variable of type bool must receive a value of type bool and a variable of type integer must receive a value of type integer. There are additional complexities involving type conversion but I won’t get into this part here, because C# isn’t my thing.

  • I understood, so what can I do to be able to compare these variables and return a bool value?

  • Just read the answer above.

  • Good, that was a better answer than I was thinking

  • All right, but what will I do with the signs != e <, since it says that only expressions of assignment, call, increment, decrease and new object can be used as an instruction? If anyone can help thanks, but at least one part of the code is already solved(L1==L2) stopped giving int error relative to bool.

  • There is already another problem and therefore another question must be asked, this one is solved and you can accept the answer if you want. Here we try to answer questions in a way useful to everyone, not to meet specific problems. In your case as a general tip you can stop using the ; in the middle of the line because it’s breaking the statement in two, which are being evaluated separately. If you want to combine comparisons can do for example MinhaVariavelBool = (L1 < L2 + L3) & (outra coisa) & (outra coisa); among other combinations. It is also possible to include the operator | or ||.

  • Note also that it is more interesting to use && that & in your case. Between two bools it makes no difference but there is a subtle difference between the two and using wrong hides the intent of your code (ie what your code really wants to do).

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.