What is the use of true or false?

Asked

Viewed 99 times

0

I still don’t understand the right time to use them. I’m taking the course in EAD and don’t have much text or exercises about them.

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

7

A good part of the operations with an algorithm must perform is to make a decision. In addition to following the normal flow or deviating and doing data manipulations, that’s all you need to do.

The decision needs to be simple, the computer is not a human who can process complex things at once. The decision needs to be yes or no, or does or does not do something. The computer only knows how to deal with zeroes and ones. Everything else is an abstraction by putting together several of these 0s and 1s.

One way to name these basic numbers is to call them false and true.

So false and true are keywords used to denominate the basic values in something that is used as a decision. It is not that value is the decision itself, but it is the possible values of a decision. Whenever you need to say something is false or true you can, or should, use them.

This applies to almost any language (some use only 0 and 1 directly with special meaning, or even any value other than 0 for true, and some may have a slightly different syntax, but they say the same). Even if you are not using a different and weird dialect in Portugol would be falso and verdadeiro.

When a variable only accepts these values it must be declared with the type called logico. So you’re guaranteed to accept nothing other than this.

Then you can only assign a value directly (remembering that these variables are always logical):

variavel := verdadeiro;

You can use an operator that the result is a logical value:

status := x > 0; //ou dá falso ou dá verdadeiro

Or you can call a function whose result is a value of the logical type.

And of course, the logical result can be used in a command that makes a decision. This command always expects a logical value to make the decision and I think it is obvious that it only enters a command or block if the value is verdadeiro. Example:

se idade >= 18 então

or

enquanto nome <> "" faça

So if we take one of the variables above and do

se status então

works as expected because this variable is logical and a se expects a logical value, everything works out. Some people prefer to do:

se status = verdadeiro então

In general when one does this he has not yet understood what serves a logical value. And if she’s learned it wrong, and she does it a lot, she can’t change. Then you’ll have to find an excuse to keep using it, probably saying it’s more readable, but it’s not. If I stayed she would do something like:

status := (x + 0) > 10;

Why did you make the sum with zero? To make it more readable. Does it make sense? It doesn’t. It’s exactly the same thing she did in the previous command. As it is a new concept she thinks it makes sense, but only shows that she still does not understand the idea of logical value.

Portugol is one of the languages that treat any value other than a falso or 0 or "" or '' as true. I find this terrible for a learning language, so I don’t recommend Javascript, PHP, Python and others, like C, as ideal languages to learn programming (I still find C interesting for other reasons). They introduce confusion because they apply weak typing (at this point Python breaks the promise of being strong typing). In fact criticism is over Portugol because it is such an abstract language and decides to be concrete where it should not. But that’s another matter, I won’t go into.

So this performs endlessly:

enquanto 10 faça

10 is automatically a true and because it is a literal it will never change state and therefore it is infinite (it may have something inside the loop that it closes, but it is another story).

Understand that you use the concept of logical values more than they do literally, at least in simple exercises. If you create a variable where you explicitly write that it has the value verdadeiro instead of getting that value for an operation probably the variable there is a flag, and this kind of construction shows that the algorithm was not well thought out.

This changes when it is used in a data structure and the intention is to store for some time in an object in memory or persist a value that is true or not. You could have an object that has a field called pago that could have the value falso as long as it hadn’t been paid off yet, and moved to verdadeiro when someone made the payment. Although this appears more directly with the values, to use right involves the concept of composition of objects that should only see after mastering well the part of algorithms.

Operators that result in a logical value (although all values can be interpreted as logical): ou, e, =, <>, >, >=, <, <=, não.

So you should use when you need a state that can only have two values and the semantics of that value is whether it’s a yes or a no.

Just be careful not to interpret something that happens to have two values and is not really a yes or no. A marital status is better than a married state, yes or no, the same for sex, until a "high-cash sensitive" which is a yes or no can be better to be more specific and say whether it is "high-cash" or "low-cash," then it involves another more advanced concept, also I will not delve into.

Understand that Portugol serves to learn the basics, not to program the right way in real applications.

If you have not found many examples and exercises using this perhaps it is not a good material. It is not the case to address this deficiency here. Even learning in sequence and wrong contexts can bring harm. I always talk about the importance of having good materials.

Browser other questions tagged

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