Estruturas Condicionais 1 - Curso de Algoritmos #07 - Gustavo Guanabara

1.15M views4045 WordsCopy TextShare
Curso em Vídeo
Veja como funcionam as estruturas condicionais, utilizando o comando SE..ENTAO..SENAO. Nessa primeir...
Video Transcript:
♫ ♫ Opening Music Hello, Be Welcome to another lesson of their algorithm course. My name is Gustavo Guanabara, I am your teacher And now after our class exercises, we'll continue with the matter Viewing Conditional structures. This is the first class conditional structure, and you will understand how the conditions It is important when building an algorithm.
So let's go now to our class that question: "What if something happens" Let's see how it does. so this is our seventh class of, conditional structures. As I said this is the first part and we will see two types of conditional structures.
and above all let us understand, "What is a condition. " In fact conditions are present in our lives all day, we will show you an example here. Let's imagine a real situation and a sentence "If I have money then I will make a trip to Disney.
" This is a trivial thing to happen in our lives if something happens, we'll make a certain decision. Let's see this happening in the tool we've seen in some previous classes Scratch So we're here in Scratch Let the control section Let's put: "When the flag is clicked. " I'll put here a sensor asking "Can I walk?
Yes or not". My answer will stay here. I'm in the control section also and here you will see that I have a structure "if" then I put down here "If something happens".
So let the Operator I will put an equality operator here. If my answer It was "yes" That is I can walk? he says "yes" then I will make him ten steps.
Button "repeat" here I'll walk. I'll change his appearance to "next costume. " And as we saw earlier It is good that we wait a little, to be able to see the animation.
I'll put "0. 1" here. Let's run He will ask: "Can I walk ?
, yes or no? ". If I put "yes" he will walk We repeat the process.
Can I walk? I'll put "no. " He will stand You get the idea?
Then he just walked, it just made the procedure to walk up the answer was yes . . .
. Yes, you can walk And if you pay close attention There is a definition of this condition We will see Looking at the code here I have an "if" and he has a limitation, it is it has the structure, it falls and it will only finish down here. So everything is in here it will make only happen this response.
So a condition it has to be delimited All that is in my hand will happen if something happens Let's see how it behaves using Visualg. So if you notice here I have the "if", which is conditional structure Part of the "I have money" is an expression right? If, by chance, I have money, then I will make a trip to Disney the "I will make a trip to Disney" is my action In Visualg there are these three demarcations: The command, expression and action.
And it is represented as follows: If an expression is true, then execute a block And there is the command "ENDIF" And some of my students sismam to say ENDIF (reading together) There is no "ENDIF" is the end of "If" And how can I not use space identifiers, the "ENDIF" has no space between them I will always represent the change between uppercase and lowercase, For you realize that there are two words So, the right to speak is "End If" and not "ENDIF" We will exercise the simple conditional structures making an example So this "if" applied to my previous sentence would be as follows: If money is greater than or equal to ten thousand, then write "She left Disney. " To "If" If you understand what I wrote there, it was exactly what I wrote in the previous sentence Only now that a structure of computational algorithm, written in Visualg The name of this structure, where a block is executed if an expression is true, It is called conditional simple Let's go now to the first practical situation And my proposal here, and improve that algorithm age And you must remember very well, it was in the last class, where we made an algorithm in which the Creuza typed the year she was born And it shows the back of their age. From there we So I have here an algorithm to calculate the age, we quickly because we've done this.
then I will write on the screen what year it I will read the year [Read (year)] I have to declare the variable year up here I will also read, in what year were you born And I read the birth which is also another variable that I'll have to get up here remember, I will not create the algorithm initially thinking of variables I'm creating, and comforme variables arise, I will declare the person's age is calculated through the current year minus the year of birth (year - ano_nasc) this result of this difference, will be assigned to a variable I'll call old it should also be stated here above all it are integer I will show age on screen ( "On" year "you will have" old "years. ") I'll put one type l here (Escreval), and let's run what year are we? we are in 2014 what year I was born?
1978 And he gave me the answer, 'in 2014 you will have 36 years. ' and I want the following situation if the person is over 21 years I want to complement this message saying "and you are already in the majority" let's see how it works we do here: if by chance that age is greater than or equal to 21 years [(age> = 21)] So [then], I'll put the End If [ENDIF] and I will write the command inside it will be escreval ( "And you already will have come of age. ") let's run we put here in 2014 we put 1978 in 2014 you will have 36 years and you will have come of age.
let's make a small change I will return here for escreval, I will take that point I'll take the one you do not get repetitive and complement here then it will show: "in this year you will have such age" and have already reached the age of majority. Let's do it again the example 2014 1978 "In 2014 we will have 36 years old and already will have come of age. " coso you make a situation where the age is less than 21 years for example, we are in 2014, and the person was born in 2000 only show that in 2014 you will get 14 years in this case, the message that the mairidade was reached, only occur if it has been reached if by chance the person has less than 21 years nothing is displayed, unless the default message there you may be asking, but Guanabara, I want to show also the person under age And then there is another structure that is simple to understand The situation would then be similar to the following Using the same phrase that I used as an example earlier we will complement it using a more complete condition let's look at an example if I have money, so I will make a trip to disney if not, I'll stay home And then, again, I complementei my conditional structure using but, and this is something that you use in your day-to-day, in their daily lives you say, 'If anything happens, I will do it, if not, if not, I'll do something else.
' the scratch also has a tool for that so here we have the If, ​​and if you look at the controls section: If the closed and the If with Else then have the option to do one here If with Else I will take the repeat here and I will do here I'll pull this expression over here Now I can eliminate that if so here I am saying. He asked. I can walk, yes or no?
If the answer is yes. He walks If the contrary I'll make him think I do not know #chateado Let's run now Can I walk? Yes!
he was. . .
and walked! Once again Can I walk? I will say no he said.
. upset Very simple to understand, is not it? so let's see how it behaves using visualg in visualg we will use the same structure Only.
. . that with complement but let's see how it is!
If an expression occurs So. . .
The block performs the if no performs the block B It is one thing I want to make clear here is that neither the then . . .
nor otherwise . . .
It has the the tilde ~ this occurs the reason we have seen previously reserved words They can not be accentuation once again the end of this is We will once again rewrite that sentence anteriror using closer syntax algorithm using visualg that phrase of the trip would be as follows if money is greater than 10000 then departed Disney if no write #chateado is the name that's on this structure which incorporates only the syntax It is. . .
conditional composed It is now that agent we're advancing studies of our conditional structures I can show you a very important concept which is the indentation just take a look at the the code I just showed and you will understand easily which is an indented code for a start-looker this code It seems that the lines are a little bagunçaas They are slightly misaligned but in fact they are indented if you pay attention commands Write They are slightly displaced on the right the first indicates the command write left disney it's inside the then It is within the structure the second shows that #chateado It will be written only if the above condition is not met That is It is within only. The indentation process is not required. Your code will work without it, but it is very important that when you are learning to program you learn to make the indentation correctly.
For indenting code just use the TAB key (↹) keyboard to cause a horizontal shift to the right. Let's see how this applies to a second practice area. Where we check if a number is odd or even.
then, my problem is simple I want to read a number and say if it is even or odd in this case, you can not use simple conditional structures for it since I have two types of condition if established that number is even, I'll write that it is even. otherwise, it is in Otherwise I will say that it is odd tell if a number is even or odd is simple just divide it by two and see if the rest of the division is 0 or 1 we understand two practical examples and you will easily understand this let's see two examples that will make that clear the first is the number 5 if I take the 5 and divide by 2 5 divided by two will give 2 and left 1 for 2 x 2 4 to 5 left 1 we will do the same with number 8 8 ÷ 2 will give 4 because 4x2 equal to 8 then the rest is 0 see the difference there pentad when divided by 2 the rest 1 Number 8 when divided by 2 the rest 0 is there, we'll get the point to identify the number pair or odd when the remainder is equal to 1 the number is odd when the remainder is equal to zero the number is even it will happen ever! with any number!
You can take any integer divide it by two It is to see what is the final rest is the final remainder is zero this number you chose it's pair! otherwise it is odd! It is if you remember well there in the classes of arithmetic operators we saw an operator that of the rest of the division which is the module he is made by Symbol Percentage% let's see how this algorithm would be ready!
I will create a new algorithm here in visualg I will save it in my class folder 7 named age let's start a new algorithm we put the name of odd or even declare a number n any that is integer we read that number enter any number read n thereby I will enter any number is it will be saved . . .
for example I typed 5 the number 5 It will be stored in the variable n just check here in the variable area visualg from now on I have the value the number that the user entered stored in the variable n what I will do is the following if the rest of the division by 2 n I represent so n is 2%, means I'll get n I will divide by 2 but what matters to me is not the division is yes the rest of it if the rest of the division by 2 equals 0 So I write on the screen the number n it's pair if no I will write The number n it is odd note here I wrote the escreval the even number is the odd number indented That is aligned in the structure thus you can draw a vertical line imaginary which will delimit the structure the same imaginary line you see more clearly using scratch where you fear to the Senao It is the end of it aligned note here! the repeating unit structure it is within the That is it is indented the think command It is within but It is also indented as it has a space Here the left this same space It is placed here in visualg a process that agent will call indentation that I did using the tab key, keyboard And here it is an important trick that has VisuAlg, if you are still not familiar with the process of indenting the VisuAlg still help you. You just press Ctrl G, and the indentation is made automatically.
So let's assume here that I do not know indenting, so I'll put a charge on top of another, this will generate a visual confusion, to initially min pressing ctrl + g keys indenting is done automatically and commands escreval They are placed inside the structure but I always like to tell my students the following not addictive! the ctrl + g learn only It is taken as the indentation to yourself to with his own hands using the tab key if you vitiate using ctrl g you end up dependent on it and other programming languages as editors php Java editors none of those It has auto indentation then you will have a big problem not vitiating the visualg therefore, it is not with him you'll work you will work php using phpstorm, our . .
. . .
partner there . . .
of course html5 you will use the netbeens the sun (Sun Microsystems) to program in Java is none of these tools has its ctrl g none of these tools has auto indentation this because indenting It is a procedure that . . .
every programmer should know to So during the practice use ctrl + g only, so you can see if you correctly indentou but never vitiating it make your own indentation let's go now . . .
. . to last practice I envisioned for this class which is the practice 3 which is calculating the imc the imc or.
. body mass index or yet mass index corporia It is a calculation made to know if you're skinny normal or, a little fat the imc It is one of the most famous calculations . .
. recognized muldialmente to set including to morbid obesity calculating the imc It is made primarily based on two parameters your. .
. Weight! Your body mass!
And your height! To calculate the imc of a particular person you will do the following account mass divided by height squared this calculation will generate a number which is the IMC a person to be considered at your ideal weight should have the IMC between eighteen and means . .
and twenty five is this is the last practice that I propose to you we'll make an algorithm to read the body mass of the individual in kilos is the height in meters we calculate the imc It is to show if the person is within the ideal weight range Then, I created a new algorithm here in visualg imc calculation is let's start by asking mass and then put dough here pasta in kilos read m I will also read the height in meters read n and the They are real type variables since weight You can have fractional parts height is also to calculate the imc we'll have to do . . mass divided by height squared height squared I can do the times or, using the arithmetic operator exponentiation I can do so (A) squared the result of this account will be assigned to a variable that will define how imc which also has to be declared we will write the imc let's run we put here I'm 87 kilos I have one meter and 88 my imc was 24 point 6, 1, 5, 2, 1 .
. . Format it here five placing e.
g. 5 houses in all and two decimals We are going to check the imc it's like I said earlier to be considered the ideal weight the imc have to be 18. 5 .
. . .
and 25 let's see how I write something between a value and another using visualg using mathematics the thing would be represented well 18. 5 imc 25 only you can not write that way using algorithms then we'll have to also use logical operators we have seen in previous lessons I will check here if the imc is greater than or equal 18. 5 and the imc is less than 25 I can not just do so .
. If the IMC is greater than or equal to 18. 5 and lower than 25 .
. . no!
I have to esplicitar imc the two expressions then my imc is greater than or equal to 18. 5 and it is less than 25 So I will put the ENDIF here put inside so if it is in range I can write Congratulations You it is in your weight ideal if no once again Write you is not in the range ideal weight then that It is the structure the calculation program IMC if it's necessary pause the video now type your own version it will run once again make my data 87 kilos is one meter and 88 the imc 24 point six two because of the formatting He gave me the message, congratulations you are in the ideal weight let's do another example a very thin person For example, a person who weighs 40 kilos . .
. and has two meters high she is out of your range Note that the imc is very low which is ten is you are not in the ideal weight range the same way, I will make a very fat person a person weighing 140 kg it has two meters high she's with imc 35 It is also not in the ideal weight range I am of course! then there multiple sites, multiple tools, multiple applications for mobile to calculate the imc is you just create a .
. very important. .
very valuable using visualg only that agent has a small problem the tracks of imc are many and I'll show them all to you from now then as I said the ideal BMI is between 18. 5 and 25 but there are several tracks released worldwide if your imc is below 17 you are very underweight between 17 and 18 and means are underweight we have the range we've seen of ideal weight we have the overweight range obesity severe obesity and finally when you fear imc 40 or more morbid obesity is you've're itching to ask me but Guanabara, as I do it to work on my program that I just do and then I you dico I will show you but only in week because now our time is up already but before leaving I'll let you two exercises to practice the content algorithm you will always see the response of these algorithms accessing this class in cursoemvideo. com site why, just accessing the site It is enrolling in this free course you'll see the answer all the years we've seen including of past lessons it's just you enter the class referring to this video that is the lesson 7 in this case and see the resolution of all the exercises let's go then the proposed exercises per min for you can do this class 7 The first exercise I named of "Is able to drive?
" I'll show this exercise working and you have to do in your home using Visualg. Then the program I propose is the following: I laid there for a menuzinho . .
. I put a header right . .
. I used the type for it I put there DMV we put the current year 2014 let's put year of birth 1978 this program it will do the following it will calculate the person's age and see if it is fit . .
. . to take its portfolio of motoriasta and for that you will consider the age of 18 18 or more a person can have take your driver's license let's see an example here 1978 I'll be old .
. 36 years it makes me able to take the portfolio let's do another example we will once again put the current year we put a person born in 1999 she has the age of 15 it is unfit to take the wallet then this result and I hope this is the exercise you have to do! Go to the second exercise exercise number 2 is student pass or fail let's see the program running so I put there, school tired boar that is crazy name of my school I will ask two notes of the student, then he took for example 4.
5 and will take 9 4. 5 and 9 he will have to average 6. 8 and is deprecated this because I considered average 7 as the minimum average for approval we see another situation Enter the first note 8 and then he took 7.
5 8 and 7. 5 the average is 7. 8 and this time the student is approved So!
There are two exercises for you to do during this week and next week Agent comes with another class Including! To solve that problem the imc agent saw with multiple tracks I hope you enjoyed this lesson I hope you are practicing Huh! Pause the video!
See execution try doing the exercises just practicing you will learn To finish. . as always!
I want to ask you to do two things First. . Sign in the channel, if you are not registered yet Click the gear!
and ask for you receive new e-mails whenever you have new classes and by clicking here you will have access all classes of algorithm course organized . . .
categorized and never forget accessing . . .
the site you're up here the cursoemvideo. com you have access to all classes You can register for the course and can have access all handouts packages for download the resolution of exercícos these exercises I do not solve during class the exercises I leave proposed here they are resolved in the website! then you'll have to get there make your registration, enroll in the course of algorithm and access the answers A big hug.
. good studies! And until next time!
Barulinho Holy shit, they killed Kenny!
Copyright © 2024. Made with ♥ in London by YTScribe.com