♫ ♫ Music Hello! Welcome to another lesson of their course algorithms! My name is Gustavo Guanabara, I'm your teacher and now we will continue the study of conditional structures, seeing the other two types of conditionals that we have.
Then last lesson we saw simple conditional, conditional compound and now we see two types, which are nested conditionals and the structures of choice. In case you missed the previous classes, just click here and you will have access to all classes from the first to the last. This is the class 8, in class 7 we saw the first conditional structures.
But it is very important that you follow this course from the start, creating an easy to understand sequence. Let us then to class today! So let the conditional structures Part 2.
Last lesson, we saw the following conditional structure. If I have a lot of money, so I'll take a trip to Disney. Otherwise, I'll stay home.
But unfortunately life is not so simple, right? One does not usually two choices. You have several possibilities.
So for example, if I have 10 thousand reais or more, I can go to Disney. If I do not have 10 thousand reais, I can not necessarily stay at home. If I have for example, a 2000 real, I can do an intermediate trip, I do not need to go to Disney.
But I can do a more simplesinha trip. So I could complement this structure creating an adverse situation, which is for example the following: If I have a lot of money, I'm going to Disney. Otherwise, if I have a little money, then I will visit my hometown.
If not, then yes I stay at home. And then it starts to become real. It is more consistent with our day to day.
We have only two choices "or this or that. " Sometimes we have more than one. In this case here I have 3.
Or go to Disney, or I'm going to visit my hometown, and I stay at home even. Let's see how this is reflected in the statement, using the Visualg. Using formal language algorithms to describe this structure you see on the screen now would look like the following code: If a situation occurs first, then it executes a block A.
Otherwise, if a situation occurs 2, then execute one block B. If not, perform a C Block Note here, that we have two conditional structures. Greater condition is external and another conditional situation that is within it.
And that makes me use two composite conditional structures within one another. Let's see the implementation of that great phrase inside something that looks more like an algorithm. In this way we will put: if money is greater than or equal to 10,000, then write "She left Disney.
" Otherwise, if the money is between 5000 and 10,000, we visit family in our hometown. Otherwise, write upset hashtag (#chateado) because you will stay home. The name given to this thread composed conditional structures is conditional nested.
And this "nested" expression comes from the nest, because I have a structure within another as if to form a nest. Let's see some practical situation that applies this type of conditional structure. What I have been proposing here is whether a student is approved, disapproved or in recovery.
And if you remember well, last class, in class 7, we saw how to create a conditional structure to see if a student was pass or fail. And if you remember well, last class that was class 7, was as exercise you build a program where they would be typed two notes of a student calculate the average and show the situation. I recommend that you have done this exercise and now I will give a simple solution to solve this exercise and add the situation of the student to be in recovery.
Let's start the algorithm asking the two student grades. Let's write. .
. First note . .
. I will read "N1", which is 1 note . .
. I'll read the second note . .
. Read "N2" . .
. And my will calculate the average, which is N1 + N2 divided by 2. And if you remember well, if I leave it will go wrong because the division is done before, and I want to first perform the sum.
We will show the average student . . .
We will show the "M" formatted . . .
We will declare the variables, N1, N2 and M . . .
Let's run the program . . .
First note . . .
3. Second note . .
. 5. 5.
The average was 4. 25. We will now make the situation of the student.
For a student approved, we will consider 7 or more. So if the average student is greater than or equal to 7, then will write on the "OK Student". In the exercise you did .
. . Otherwise, write "The student is FAILED.
" However, this case is the following If the student is not reproved He has another situation What if it is in recovery or fail Then the average If greater than or equal to 5 -> Code if (M> = 5) AND The average is less than 7 -> code and (M <7) So -> then code Then I'll write -> Escreval code ( "Student in RECOVERY") -> Senao code I write failed student -> Escreval code ( "Student FAILED") Note that the latter "If not," will only happen if the average is not greater than or equal to 7 "> = 7" E is not 5 to 7 Therefore, it is less than 5 Note that, in my code The indentation was made To allow the structures to be displayed within one another Then you will see This structure "If" I'm marking It is within the framework of "If" out That is, they are nested and now, my work properly Algorithm so let's run we will see the first situation, a student who took 3:06 he ta with average 4. 50 and he failed since the student who took 7:05 had average 6 and is in recovery the last situation, is a student who took 10:06, he took the media 8 and is approved again, here is the code if necessary Pause the video, type in your visualg, and see the result because propia it was clear? !
with this, I made a simple nested conditional structure where I have only one structure inside another however, there are situations where I have to nest more conditional structures this will make it difficult to visualize my code but understanding is not difficult we see our situation 2 for nested conditional structures you should remember that last class we did a practice for calculating BMI which is the mass of a person divided by height squared in the previous class, we just made the calculation, and show if the person is within the ideal range or not however, we saw that there are various groups, from the person who is very underweight, to the person with morbid obesity would BMI of 40 or more we will solve this problem by creating a nested conditional structure from the exercise of the previous class so I opened here exactly the algorithm that we've done in the last class if you have not seen the class 7, visit cursoemvideo. com go to class 7, download the files from the exercises, and you will have this file, or you pause the video now and enter your What we'll do is just eliminate the conditional structure, and allow the calculation of BMI it will be from where we'll start So in my case here, the calculation of BMI is weight divided by height squared we've seen this last class And now we will make the BMI ranges starting with the range of a person who is very underweight which is any BMI below 17 Then we'll do here, if by chance the BMI is below 17 [if (BMI <17)] [Then] in this case as I will have a lot of 'if' structures [if] Nested My tip 1 is to keep the code always indented My tip 2, is whenever you open one if [it] automatically close with an End If [ENDIF] so will prevent you from curling at the end Then, if my BMI is less than 17 Write ( "Very underweight") The second situation if no I will have another situation The second situation It is when the BMI is between 17 and 18. 5 What is a person simply underweight So let's put a SE BMI is greater than or equal to 17 and BMI is less than or equal to 18.
5 then again I have done the SE, do ENDIF for me not to roll So if the BMI is between 17 and 18. 5 Recalling that 18. 5 is eighteen point five I'll write ("Under weight") if not, we go to the third situation which is when the person is between 18 and 25 that was the situation we did in the last class of the ideal weight If the BMI is greater than or equal to eighteen point five.
I'll get the same from up here and BMI is less than 25 Then, in order to Write ( "ideal weight") if not, we will have another condition that is when it is between 25 and 30 and this characterizes overweight If the BMI is greater than or equal to 25 and BMI is less than 30 Then, in order to He will write ( "Overweight") but I'll make the other situation What is BMI greater than or equal to 30 and lower BMI than 35 that characterize . . .
Obesity if not, we will test another track which is the IMC greater than or equal to 35 and imc less than 40 this imc It features a person with severe obesity if no I have the option the imc over 40 as the last option and I lined my terms I do not need to test imc the most equal to 40 if it is not any of the above obviously he is in morbid obesity What's up! As I said before I have a structure that when you look like that, if you look unadvisedly you will end up finding it complicated but in the end if you opened a (if) and automatically closed (ENDIF) your code will be fully aligned That is one (if) within the other and will work correctly lets test A person make a very thin person which weighs 40 kilograms and is 2 meters it is much lower weight because it has 10 imc another situation we clocar a very fat person which weighs 200 kilograms and has one meter she has imc 200, it is morbid obesity note 200 is not her weight 200 is the imc A person 85 kilos who has two meters it is at its ideal weight we see another situation a person who is 55 and a half kilos and has one meter and 56 it is also at your ideal weight do the test with your weight and your height and see what would be your range of imc I hope you have understood the concept of conditional aligned structures and practice enough at the end of this class there are two exercises and you can make them using this type of structure however there are some situations that I have many tests with simple values númerios what's up there is another conditional structure which is very useful in those cases It is the conditional structure choice if the structure choice if It has a very simple way of understanding let it you will put choose between and end choice a variable and a case for each value according to the value of each case a block will run still exists a block end which is the other case that is if any of the above values are displayed one (block D) will be executed oh, another case is a structure Opicional It can be used and either can be used as ometida let's see some example in our practice 3 we will make a program that I baptized child hope and you will understand what I'm talking let's start the program I will put a . .
. . one estruturazinha here put a dash another trait just to create an environment here and start my program put child centralized hope we put here thank you so much for help let's put 1 to donate 10 Reais two to donate 25 reais 3 to donate 50 reais 4, to give other values and 5 To cancel I will read Donation D I will have a variable, D of integer and then not to use an aligned conditional structure If the D is 1 is one thing, but if D is 2, will make a very complicated structure I can use the choice Choose D, will soon put FimEscolha And here I can create each case, such as: If it has entered 1 If he has typed 2 If it has entered 3 case 4 and if 5 I will also enter here a variable called value case 1 the value gets 10 Case 2, the value gets 25 Case 3, the value gets 50 Case 4, I will ask the value "What is the value of the donation?
R $" Read (Value) Case, Case 5 Value gets 0 In the end, will write another line "Your donation was R $" Value "Thank you very much! " and will end the program over a line. And let's run Look there .
. . Thank you for help To give 1 10, 2, 3, 4 or 5 or donate 25 reais 2.
I pressed enter typed Your donation was $ 25 Thank you! I will make another Will donate R $ 50, I will shake 3 Your donation was $ 50 Thank you! Let's do one more I will tighten 4 now to give other values What is the value of the donation?
I will donate R $ 200 Your donation was $ 200 Thank you! And finally I will donate I will cancel by pressing the 5 Your donation was $ 0. Even so, thank you!
And if you're looking at your code, you'll see that the choice structure is much simpler than getting aligning many conditional structures however, there is an important observation here. The structure choice does not serve to test the range of values, it is if it is greater than or equal, less than else that's not possible and it only works basically for integer values some languages accept the choice that would be the Switch for character data type but in the case here Visualg It is recommended to use only integer values then the choice is not a conditional structure that you can use in all situations but there are some that you think you can not use and can ends. Let's see another example.
My fourth practice of this class It would be the amount of dependents of an employee let's do a simple algorithm here Let's make a program to write on the screen "What is the official name? " Will read the name Go read "What is the employee's salary? " Go read The salt) The variable salary and in the end go ask to read, "What is the number of dependents?
" are people who depend on it read call (Dep) Then I have a name which is of type character I'll have salary that is the actual type and will be dependent which is integer and I have here a new employee's salary The new salary will be given according to the number of dependents So, I choose the dependent FimEscolha On here If the employee has no dependent the new salary You will receive the previous salary it more only 5% increase then it will be 5 times salary divided by 100 we have seen how that makes the percentage calculation in previous classes If it is 1, 2 or 3 dependent This fully valid structure The new salary it will be the salary plus 10% If it is 4, 5 or 6 The new salary will be 15% increase And in another case That is, if it has more than 6 dependents The new salary will be the salary plus 18% Let's make a case here, all employees earning $ 1000 for easier account So I have the official Jose which has the salary of R $ 1,000 and has 3 dependent if you look here in the code to 3 dependents he will get 10% increase Soon, the new salary you will have to be R $ 1,100 I did not show the right salary You have to write here "The new salary" will put his name will be $, salt Configured with 5 houses in total, 2 decimals Put here for him to read the salary, put the R $ here to you do not feel like typing Let's run again Jose Win R $ 1,000 and has 3 dependent that would give 10% increase I sent show salary and not the new salary right Again Jose Win R $ 1,000 and has three dependents, the new salary Joseph is R $ 1,100 We do here, Maria That makes $ 1000 and have no dependent It will only take 5% increase Here in this case, if I have 5 dependent is 15% increase So let's put there André Win R $ 1,000 5 and is dependent R $ 1150 is the new salary it The largest increase is 18% and for those with more than 6 dependents Let's do an example, Claudia Win R $ 1,000 9 and is dependent She will have an increase of 18% and will earn R $ 1,180 So it's very important to know that choice if There is a possibility you test multiple values separating them by commas I hope you have understood all conditional structures that we put in this course Are the simple conditional structures, composite, lined and choice case Now, to complete its study of conditional structures, especially the last two Let's see two examples working and now I want you to do these exercises and show that you have learned these two structures The first proposed exercise is as follows: The Use of a student and resort to Wikipedia We will have the following information If you take a look at the entry in Wikipedia School Note You have the ratings of A to E Here is the following We will consider the average of a student If the average is between 9 and 10, the classification is A. Between 8 and 9, the rating is B. Between 7 and 8 C.
Between 6 and 7, D. Between 5 and 6, E. And below 5, classification F.
So this is the purpose of our exercise I want to calculate the average student And according to his note, I'll show rating A, B, C, D, E or F Let's see it working Let's put here for example, the student 5:08. The average is 6. 5.
The use is D. As it is between 6 and 7. Let's see another example To note 10:07 The average is 8.
5. The use is B. Making again 10:09 Average 9.
5. The use is A. Analogously 4:02 The average is 3.
And the use is F. So this is your mission. And do not forget, as we are working with real numbers This will not be possible to apply the structure choice case.
The second exercise of this class is the following: We will analyze a football match. According to the number of goals made by each team. The situation is the following: Let's see a hypothetical match between Bangu and Madureira.
I'll ask how many goals Bangu made. 3. How many goals did Madureira?
6 The difference between goals between Madureira and Bangu was 3. This is a normal game. Let's see another situation Bungu made 3 goals.
And Madureira 3. The difference is 0. And there was a tie.
Now, if by chance the Bungu made 1 goal. And Madureira made 6 goals. The difference of 5 goals.
It features a rout. Then, according to the performance I showed here, you will be able to create your own algorithm. For analysis of football matches.
And remembering that we are now working with integers You can rather use the choice structure If necessary, if you prefer to use your choice. I recommend you do using the SE and also using the choice. Two different situations.
I hope you have understood this lesson ends the concept of conditional structures. In the next lesson, we will now leave for interactive links, which are the repetition structures. And you'll learn how to make a code to repeat more than once.
Again, I want to thank the visit of everyone, all the praise that we are getting through social networks, YouTube, by Facebook, everywhere And I wanted to ask again if you have not signed up, click here, sign up Sets for you to receive emails whenever we are new classes Then click here to sign up and then the engrenaginha side Join You authorize the receipt of emails and clicking here you have access to all classes organized in Playlists all cute And always remember, accessing the site cursoemvideo. com You can subscribe in the course Algorithm And have access to the answers of this algorithm that leaves just one week after this lesson was available You have a week to practice and I did not give the answer before you try So when you leave the class 8 also leaves the answers class 7 Never leaves before, but that students who are following weekly They will have a very large facilitation So I recommend before looking at the answer try I wanted to ask also as always, for you to enjoy this video Share on social networks, show the link to your friends Show, disclose the site Video Course Because it is completely free, it is totally for you Sincerely, good studies and until next time!