Operadores Lógicos e Relacionais - Curso de Algoritmos #04 - Gustavo Guanabara

1.49M views4245 WordsCopy TextShare
Curso em Vídeo
Aula do Curso de HTML5 criado pelo professor Gustavo Guanabara para o portal CursoemVideo.com. Dow...
Video Transcript:
♫ ♫ Opening Music Hello! Welcome to another lesson of their course algorithm, my name is Gustavo Guanabara, I am your teacher, and now we're going to continue the study of operators seeing the two operators missing: relational operators and logical operators. Let's go to class for us to understand the difference between them and what each will do.
So, the last lesson we saw the entry of commands and arithmetic operators. Now, we'll start seeing the Relational Operators. And as the name suggests, relational operators will create a relationship between variables or expressions.
Then you will be able to compare the variables and expressions and generate logical results, as true or false. Then the relational operators will generate as a result, always, a logical value. Logical operators that exist in all programming languages, including, ambiento in the visualg are: So, it is noteworthy that there is no way to do the greater than or equal, with the same symbol under the larger, as we do in mathematics, by keyboard typing issues.
So we will put the biggest sign followed the same space without any sign between them. The same will occur pro or less, and we have the last two relational operators are: equal to and different from And worth noting that there as well as or greater and less than or equal, It is difficult to type "different" which is an equal sign "cut" in the middle. So pro difference operator, I'll use a smaller signal and a higher signal, typed with no space between them.
and then, we will go, then by a party that practice is to compare numeric values ​​using the VisuAlg and once again, as always, I will recommend. Open visuAlg and Assita this class with the application open no use getting you watch me do something And you find that you learned then the best way to learn is to practice because when we practice, we make mistakes and when we make mistakes, we try to fix and it is trying to fix what you learn, you never forget it then I created an algorithm and I will create three variables here variable A variable B variable and C, all three integer declaration of variables we have seen in previous lessons what I will do here is to make a simple assignment "A" gets 2 "B" receives 3 and "C" receives 5 and now we will make some logical comparisons I post here. type "A" greater than "B" Then he will show me the result of If "A" is greater than "B" Running the program we have "false" this means .
I sent write the logical result of "A" is greater than "B" and here, I can see the values ​​of the variables "A" is worth 2 and "B" is worth 3 "A" is not greater than "B", "A" is actually smaller than "B" as a result of the "A" greater than "B". I have the logical value "false" Get the idea? We will here little we see the logical operators.
But the relational operators will always lead me a logical value That is, will always return me either true or false Let's see some more example So I'll put here for example "A" Like "B" "A" is 2 and "B" is 3 then "A" is not equal to "B". So I will return "false" value. This is "A" is not equal to "B" Similarly I can test if "A" is different from "B" "A" is 2 and "B" being 3 course "A" is different from "B" Running it returns me the value "TRUE" This is the answer to: "A" is different from "B"?
Yes "A is different from" B " And as I said earlier relational operators will test the relationship enters variables, values ​​or expressions So I can make some comparisons using values For example here. "A" is greater than 2? He will return me "FALSE" Because "A" is not greater than 2, if you check here.
"A" is equal to 2 But if I do the logical test: "A" is greater than or equal to 2? Running, he has returned me "TRUE" Because "A" is not greater than 2, but it is the same Then one of the two operators is going to have to lead to true Thus, in this manner. "A" greater than or equal to 2 results in the logical value "TRUE" We do now, some logical expressions tests Nothing prevents you do it here I'll test if "C" is equal to "A" + "B" (A plus B) We've seen the last class the arithmetic operators, and we know that this operator will perform addition Then it will test if "C" is equal to "A" + "B" "C" is 3, "A is 2, and" B "is 3.
2 plus 3 equals 5, it will lead me to the value "TRUE" Let's run and see what the end result, it really was true Let's do another example here, let's see if "C" is greater than or equal to "B ^ A" (B raised to A) That is, it will make a test seeing if "C" is greater or equal than the result of "B ^ A" (B raised to A) "B" is 3, the high 'A'. "3" squared, 9. "C" is equal to 5.
"C" is not greater than or equal to 9. So. We will run and it will return me "False.
" Because "B" high "A" Of Nine (9) "C" is not greater than or equal to "B" high "A" But if I put here If "C" is "<= B ^ A" (less than or equal to a high B to A) Now he will return me The true value I think I already gave you understand! And it is clear that I can use Comparisons between variables and values Between variables and expressions And among variables, expressions and values I can mix everything we've seen so far In a single expression, we test: I can put here for example Test whether "B" It is less than or equal to "C" rest of the division "%" by Two The "C" is Five The rest of the Five Division by Two "5/2" "B" is equal to three Three logo is not less than or equal "<=" to A (1) So should return the false value I do like this: A equals the "C" 2% (1% C = 2) "C" 2%, is the rest of the division "C" worth 5 per 2 5 divided by 2 equals two and the rest of the division is 1 He will test me here, if 1 is equal to 1 He will return me now, the true value. Why is the result of 1 "C" 2% And I have no need to put "C" 2% on the left side of the expression Of course, this way, it is a little harder to see.
But that is possible using the relational operator equal. And with that, we tested several ways of using the relational operators. When we have more advanced, and we see the conditional structures We will, even better use the relational operators.
But for now, I think we've done enough exercises And you can understand, the comparison between the logical results. Let's go now, pro next type of operator. The next operators that we will see are the logical operators.
In the same way that the relational operators the logical operators also return a final value logical. But they do not serve to compare results expressions or numbers. They only serve to compare other logical results.
In the case of VisualG, we have three types of logical operators: The Operator First, is the operator "and" Let's consider two premises the premise "P" and the premise "Q" each with its own set of values, true and false Using the operator "and" we will have the true result Only in cases where the premises "P" and "Q" are true! In all other cases, the logical result of "P" and "Q" will be false The second operator is the operator "OR" Let us consider the same premises "P" and "Q" and we will see how it would carry the "OR" operator! In this case, the operator "OR" will only return false, where the two premises are false!
In all other cases, the logical result will be true! It was complicated? He was a little confused?
quiet is no problem How often have you come back to watch again What do I have spoken! What did I just said, it is WHAT a teacher usually speaks in the classroom That's the scientific way, this is the formal way to teach operators But I'll show the difference between the operators "AND" and "OR" to you, you will understand for sure! So waiting a little bit that we have another logical operator to see, this is simple to understand you'll learn right away!
The last logical operator we will see, is the operator "NOT" For that we consider only a premise! Being the "P 'true' NOT p" premise is false, that is there will be a reversal of the logical result If we have "P" as true "NOT p" obviously something that is not true, is false. and one thing that is not false, it becomes true the operator "NO", it is easy to understand the big problem is the comparison of "AND" and "OR" for those who ta starting the program!
If you are already an experienced programmer or with little experience, you know the difference between them But if you are just starting out, and agree with me that a little hard to understand? I have a technique to show you that I use in the classroom, it becomes much easier to understand Let me and for sure you will understand now! Let's imagine the situation Next I have two friends, "Paula" and "Keziah" and I would love to see them happy!
So I want "Paula" to be happy and that "Keziah" be happy If "Paula" is happy and "Keziah" is happy, of course I am very happy! If by chance the "Paula" is happy, but the "Keziah" is not happy, and I want "Paula" and "Keziah" are happy I will not be happy! Understand the use of the word "And" here in my sentence, I want the "Paula '' and '' Keziah" be happy!
does not help one to be happy, I want the two are So Similar, if "Paula 'is not happy," Keziah "is Obviously I'm not going to be happy I want the two to be happy! and in the latter case, if neither of the two are happy, for sure I will not be happy! You get the idea?
With the operator "and" I want one thing and another. No use come with only one for me, because I want both. We will follow this same line of reasoning to the "OR" operator.
And there is even easier for you to understand, is not it? Come on. We will follow the same line of reasoning.
"Paula" and "Keziah," and now I want Paula to be happy OR Keziah be happy. If the two are happy, of course I'm happy. If only Paula is happy and Keziah not, I end up being satisfied, because I want Paula OR Keziah be happy.
Any one or the other. Similarly, if Paula is not happy and is Keziah, yet still happy. The only case that is different in the case of the OR, It is when Paula is not happy and Keziah also is not happy, In this case, as I want Paula OR Keziah be happy, and neither is, I do not get happy.
Now given to understand the difference between the operators AND and OR right? In the case of And I'm only happy when Paula and Keziah stay happy. Otherwise I'm not satisfied.
In the case of the OR, I'm happy in either case, except in the case where the two are unhappy. The NOT operator is simply a logical reversal If Paula is HAPPY The NOT HAPPY SAD is Thinking in the same way If it is SAD, the SAD is NOT happy So, for you generate the truth table What are these tables we're doing E and OR, is easy All happyfaces You will turn into true All the sad faces, you will turn into false So it's easy, is not it? So whenever you have difficulty understanding the truth table E and OR Just draw smiley faces And then replace All HAPPY by V And all the SAD by F And so You can have the two tables TRUTHS The logical operators: AND and OR Let's do some logical tests Using that our same example We were creating in Visualg So we do here This "Type" I have to give two logical tests So if I put here "(True and false)" He will return me The value FALSE Why a REAL thing and one thing FALSE Give in FALSE But I can also use logical results generated by relational operators and test We will test for example, if A is equal to B (A = B) or C is greater than a (C> A) Let's see the result.
In this case A is equal to B? 2 equals 3? No!
So this one will return me false. C is greater than A? 5 is greater than 2?
This one would return me true True or false, comes in that case. Paola is not happy, but quesia is happy as I have a "OR" my logical result will be: a happy or true face! So here we have the logical result of all ((A = B) or (C> A)) is true!
If by chance we put a parenthesis here, and we put a non front: (not ((A = B) or (C> A))) it will reverse the logical result So; if this here: (A = B) or (C> A) is true, not this here: (A = B) or (C> A) will return me the False value! So what he did was the following: A is equal to B? , no!
C is higher qua A ? , yes! False or true is: true and not true is: false.
And again, you will be able to do your logical and relational tests, using: numbers, variables or expressions. Remembering that to use logical operators, you can only test logical values. And remember, too, that all expressions using relational operators generate logical results.
What I recommend is that you make several logical tests like the one I just used. then do tests and try to find out what is the logical result at the end. if you prefer to pause the video at that point back to your visual g and do some testing, and do not forget to put all your relational comparisons between parenthesis because they are going to generate the logical results at the end and speaking in parenthesis we have to be concerned enough with the order of precedence we saw in the previous lesson that there is an order of precedence between the arithmetic operators now that we have seen the two other operators, relational and logical we will see a general table of how the order of precedence in expressions containing arithmetic, logical and relational operations therefore, we understand how to work the general order of precedence an expression that has arithmetic, relational and logical operators at the same time First, the arithmetic will be made giving in order of precedence, we have seen previously the parentheses, then exponentiation, soon after, multiplication and division, and finally, the additions and subtractions after all arithmetic expressions are resolved They will be held all relational operations within the relational operators, which are "higher," "lower," "greater than or equal", "less than or equal", "equal" and different Everyone has the same order of precedence, and will always run from left to right Finally, after being solved the arithmetic expressions and relational execute all logical operations starting with 'E', soon after the 'OR' and finally the 'NO' save this table in your mind, this is the execution order of all the operators that can be used within an algorithm Now it's easy right ?
! Now we have gathered all the information about operators, you can now go for something bigger which is a more complex exercise using relational and logical arithmetic operators along with the input commands and output that we have seen previously let's think then in an exercise that uses all that we've seen so far, including the variables let's make an example that later will be more complete but for now since the work for us I'm going to do is this, surely you know triangles what I will do here is to read three sides of a triangle, which I'll call L1, L2 and L3 These values ​​will be real ( 'L1, L2, L3: Real'). And I'll make a logical test to see if this triangle can be equilateral or scalene.
WHAT will lead me into a logical test ( 'logical') if you remember the triangles study equilateral triangle is one that has three equal sides the scalene triangle is one that has three different sides for now I will not worry if the three values ​​that were actually typed form a triangle because there is a mathematical rule for this. But with us 'ok' at first, we will not see this mathematical rule yet. We will see later on in a more complex exercise.
in fact, we read three values ​​and see if they are totally equal to each other, or completely different. we do the testing here and understand how the relational and logical operators work so here, vamomos start asking the user to enter the three sides Write ( "enter the first side") Read (L1) I will do the same for the other two sides the procedure is exactly the same only the read will play the value in another variable Finally, I will do so with the third. Read (L3) now I've done three of reading what I will do is to check if the triangle is equilateral EQ will receive the logical outcome of the tests of the three sides ( 'EQ <-') If iqual L1 is L2 and L2 to L3 is iqual [ '(L1 = L2) and (L3 = L2)'] He is an equilateral triangle I might even put another and here L1 L3 is equal to But that It is already a logical imposition If L1 is equal to L2 if L2 is equal to L3 Obviamnete L1 L3 is equal to Is I do not need the end of this expression If you leave no problem, will work the same way But that It is a logical redundancy is I will eleiminar To test whether it is scalene I will check If L1 is other than L2 and L2 is different from L3 and L1 is different from L3 In mamneira scalene three logical tests are needed For L1 is different from L2 and L2 is different from L3 does not imply L1 Is different from L3 So I have to make this logical terceirot Lastly I will write on the screen the result The triangle and equilateral?
I will show the logical value of EQ will write true or false I will use the escreval instead of write So he skip the line At the end we will see the triangle and scalene So that my algorithm I have reading the three values tests including it's the result it is checking whether the triangle will be equilateral or scalene we will do some tests running First side 3 second side two third side 4 three two four are three different values ​​each other soon would form a scalene triangle let's check the equilateral triangle can not be but it can be scalene let's go back. rerun and we put 4, 4:04 which form an equilateral triangle The triangle is equilateral? true.
The triangle is scalene? false. Let's do another test it will be four (4) four (4) and three (3) Here, we have two equal sides and a different What would lead me an isosceles triangle.
We are not testing in this case But it will be neither equilateral nor scalene . . The logical result: The triangle is equilateral?
False The triangle is scalene? False And just remember what I said There is a mathematical rule To verify that the three sides can form a triangle And this logical test is simple Each side must be less than the sum of the other two sides So if I have side L1, L2 and L3 L1 must be less than the sum of the other two L2 must be smaller than the sum of the other two And L3 must also be less than the sum of the other two. And note that I used the logical operator "and" in my sentence So let's create another logical variable to see if it can be a triangle.
Then I'll create a new logical value called "TRI". That will determine whether it can be a triangle My variable "TRI" will receive the logical result L1, is less than the sum of L2 to L3 Note here that this expression the arithmetic operators have higher precedence, according to the table that we saw, then he will do first L2 + L3, and test whether the sum between the two is greater than L1 I also have to take the tests between L2 is greater than L1 + L3 And also you have to know if L3 is smaller than L1 + L2. A triangle can only exist if all these operations result in a true logical value.
We will show here Escreval "can form a Tiângulo? " we'll see whether or not with the amount of IRT Let's do some more tests if I put 2, 2:02, It can form a triangle, it forms an equilateral triangle and does not form a scalene. Another test: That 2, 3 and 4 we saw earlier, it can form a triangle, not an equilateral triangle, but may form a scalene triangle Let's do one more test: That our 2, 2 and 3.
it can form a triangle, but it can not be equilateral or scalene this because it is an isosceles triangle. Finally, let's do a test with 2, 2:05. 2, 2 and 5 can not make a triangle, because 2 is less than + 5 2 7 which is 2 here is less than 5 + 2 that is 7, 5 but it is not lower than 2 which is 4 + 2.
Then the third side would be too large to form a triangle. So, in the case here, the logical result would be, he can not form a triangle, It can not be equilateral and scalene less. And then you can ask me, "But Guanabara, we have two equal sides and 1 different, it would be isosceles right?
" No! Because of 5, 2 and 2a can not form a triangle, and we see that for this result I just mark You get the idea? We will return to this our reasoning about triangles, later, when we go see relational operators But for now, our time is up, and we'll have to leave for our next class, that only comes out next week.
In the next lecture I will show a new tool, in addition to VisuAlg, it is much easier for you to learn algorithms. But it is not so professional. If you downloaded the package here in www.
cursoemvideo. com, surely you already have the Scratch application. I do not know if you have already installed.
Install as the next class we will use it, and you will see that you will have great fun with this tool, And will, break, learn a lot of Logic Programming. Again I wanted to ask first: Practice! Second, work with the course of the project in Video.
By clicking here, you sign the channel and will receive all the information whenever you have a new class, Side button "Sign" has a gear, click it, and say you want to receive an email whenever you leave a new class, so you will not miss anything. By clicking here, you will watch all algorithm course lectures so far, so if you come now, it is welcome, but you have some previous classes, to be able to understand the construction of algorithms, and I recommend you click here and watch them all. Do not forget to always share classes give Joinha in the videos, Share to your Facebook, get there on Facebook and tell your friends pros: "Look!
Watch this lesson here. learn to program! " We are here in the first steps to building algorithms, and surely you will learn how to program.
I'll show various tools, I will show several techniques, I will always teach you a rhythm a little more well cadenced, for you to understand algorithms, for you to build algorithms. Up to me to teach you, and I'm doing my part. It depends on you learn, for that you need to practice.
Never forget. Study hard, a big hug and see you soon! Hi!
Someone came clattering hell. I said fighting with you! Alas, alas, alas!
I really wanted the phone that damn who is calling at 8 am inside the company!
Copyright © 2024. Made with ♥ in London by YTScribe.com