♫ ♫ Music Presentation Hello, welcome to another class of your algorithm Course my name is Gustavo Guanabara I am your teacher and now we go to the second part of the class structures Repeat. Last class if you remember well, that was class nine we saw the structure while it was the first repetition structure that we discussed. And now, we will start to second and you can ask me .
. . hey, but if I've learned one so I'll learn another?
In fact you will learn three and each serves a different situation I will try to illustrate this for you. Well, as I said this is the second part of the repeating structure of class, and now we'll see the repeat command. Let's see his structure.
Repeat a block until the expression occurs. And the function of the operating mode "Repeat" is very similar to working fashion "Choice" . .
. except that the expression it is logically reversed. You will understand now already how it works.
Let's first take a look at an example, we already did last class with "As" and we will change pro "Repeat" and let's see what the main use of this structure. So I put here already an algorithm to add five numbers. So I recommend you take a break in this video and analyze more calmly, we will analyze here quickly.
Then you have a variable to sum it will add up the numbers. A counter will start at 1 and go up to 5. While my counter is less than or equal to 5 it will adding 1 more on the counter.
Then the repetition structure it will occur Starting at 1 and ending at 5 So it will happen here 5 times. What will happen 5 times? He will read a number And it will add, accumulate this number in the variable S.
In the end it will show the variable S with the sum. Let's see working only for you to understand better. He will ask that the first value, 1 second, 3.
I talked and typed 3 4 = P ♪ ♫ ♩ ♫ ♭ ♪ 7, 3, 2. Between 1 . .
. and then the sum of 1, 4, 7, 3 and 2 gives 17. This is the structure.
And this will happen, as we saw, 5 times. And if you are still doubts repetition structure "While" Back in the previous class Watch it again Exercise, go there in the course site Video here O cursoemvideo. com You go there, is the inscription on its course You will have access to class 9 In class 9 you will have access to the algorithms on the side The algorithms you do not have access to Youtube So if you are watching on Youtube Runs there in Cursoemvideo.
com site From a peek in class Because along with the lesson has exercises So it's a much more enriching experience. And the registration is simple You go there, sign up, it's all free. Go on, sign up, sign up for the course.
Says, "Sign me up in the algorithm of course. " And you'll have access to all classes And the years mentioned. And then I want to propose a change in this algorithm.
I did for 5 numbers If I wanted to do for 10 was just change the 5 to 10. But I do not know how much I want to do I will let the user choose how much it will want to do So I'll add a question . .
. you will understand what I'm going to do here. So back to the algorithm here I will remove all references to the counter Because now I do not want an accountant I will not count how many times because I do not want 5 times Then I remove the counter here Obviously I have to delete my expression of "While" Let's do this I will create a variable "resp" Response That will be character I'll put here O While my response Equals Yes It will add up and will ask, O "Do you want to continue?
" I'll put like "S / N" Only pro user know what he has to enter. and I read my answer. When I run, it already ends the program That's because I did 'While (resp = "S")' And this problem occurs because the first thing he does is to test the response and I have not yet given the answer For this I will do a workaround here, and will, play my answer worth "S" right away.
That is, if the user started to run the program the first answer is obviously yes, but want to continue. Let's run again ah . .
. he forgot to remove a reference here The "Cont" there is no more I will enter the value. You want to continue?
Yes. Will enter another value. You want to continue?
Yes. Will enter another value. You want to continue?
Do not. Then he stops, and sum. It was clear the difference right?
So before I was doing 5 interactions Now I do not know how many will occur Let the user to select whether he wants to or not responding yes or no And then we had a little discomfort it was the use of the word answer is tested before all This is because while there is a repetition structure with logical test in the early And as the logical test was performed before testing the response, which is given at the end, this created discomfort in scheduling The solution to this is to use a structure where the logical test is done at the end This is because the first time you will do, you'll want to do anyway So you want to take the test when you finish the first repetition And not when you start it Then the "Repeat" is just that It is the option to "As" but instead to take the test at the beginning, it will make the test at the end We will return here pro framework for you to understand the difference If you remember well this was the structure that we had last class which is the "While you do not tidy up your room, you're grounded" This "While" it will be repeated and will only leave when the "clean up the room" is true and then he goes to command "liberated", which is the last. Rewriting this same logic, using the "Repeat" it would be this follows: Repeat "punishment" to "clean up the room" Note that for this case, it was even easier to understand So from now on, he repeats "punishment" that is, it starts with "punishment" to "clean up the room" When you arrive at "tidy the room" the logical test is done, and if it is not met, it returns From the moment in which it reaches the expression, which is "the fourth pack" it will come out, and so execution continues with you "liberated" the punishment to "clean up the room" And then if you pay close attention the use of the expression that is in pink, I will now show you, It is the logical inverse of each other While the "while" you use "not tidy the room" as an expression, the "Repeat" you will repeat "to clean up the room" It is a question even of phrasal construction You will be grounded "while not tidy the room" You will be grounded "until you clean up the room" So there is a logical inversion in this expression Pay close attention to the expressions that are pink So you see that in the first upstairs you must use the logical operator "not" and second, it is not necessary This is a matter of logical reasoning and you will have to practice it, to be able to understand right Let's do this I'll change that our previous program for that instead of him using the "As", use the "Repeat" I will keep the same variables I will remove this ugly hack that we did here I will remove this "while" and will replace the "Repeat" I'll take the "FimEnquanto" and I will put "Up" and an expression Then I do the following I'll repeat it all until the answer is "no" "Do you want to continue? " No "Up to" the answer is not equal to (resp = "N") Let's run and note that it is now running the first time independent of the response variable (resp) is empty or not First value.
You want to continue? Yes. Second value.
You want to continue? Yes. Third value .
. . and so it will do as I have said yes Right?
I'm putting there "yes" I will enter the last, and I will enter "no" Then he will add: 1 + 5 + 6 + 8 + 3 + 5 + 6 that will give "34" There is a clear difference? If you happen to roll a little, never forget you can pause the video back a bit reassistir class and be sure never to practice To pause the video, type in your VisuAlg, and see the result of running Now come to some practical exercises solved so you can enhance the study of repetition structures having the structure "Repeat" The first example proposed practical will be using the same code that we have seen previously is from 1 to 10, only now we will make the structure "Repeat" I'm already here with a new algorithm I will make a program to count from 1 to 10 For that I will need an accountant, obviously, starting with 1 I will declare the counter variable as Full Let us now to repeat. I'll put the "Repeat" "Up until" My counter that is greater than 10 Note that this is the reverse in "While" would "While the counter is less equal to 10" Here it is "Until the counter is greater than 10" I'll add another one on the counter "Cont Cont receives plus 1" All I put in here will be repeated 10 times in my case here simply a "Type" with the counter.
I will run with the timer him exemplifying It started with 1 two. . .
3 . . .
and so on . . .
until number 10 Note: the repeat loop, the logical test is done at the end, and down here, the variable "Cont" changing its value, until my count I chose to do And in this case I have not much more to explain It is because the structure "Repeat", it is simple if you understand the "As" If you are having trouble with the "Repeat" it's probably because you had problems with "Whereas" And if you had no problems with the "As", and did not get it right the "Repeat" no problem you can do all the programs that you do with the "Repeat" using "While" But we will improve a little more this example because that we've done I will propose an example in the example come on Put an additional item that is to display the multiplication table of any number And the multiplication table is basically from 1 to 10, except that multiplying with a value So, for example, suppose I want a number . . .
the multiplication table number is 7 Let me declare this variable N here Huh? ! with 7 Instead of writing only the counter I will write a number, then I write an x, then I'll write the counter Running, look He will do: "7x1", "7x2", "7x3" .
. . to "7x10" So basically what I did It was to add a number, and show that number, an "x", and a counter, to be similar to the one times table After the counter will put an equals symbol on the screen in quotes, of course, for him to write So're there, look: "7x1", "7x2" .
. . Lack a variable to the result would, for each of the steps the result would be: N, which is 7 times Cont, which is the value that in the end will have to show the result ( "R") Running.
. . "7 = 7x1", "7x2 = 14" .
. . to "7x10 = 70" If you want the multiplication table to another number for example, the number 9 Just change here for 9 (N <- 9) and run If you want to let the user interact with it, you can instead of making the award of N worth 9 you can make a "Write (" Want to see the multiplication table of what number?
") "Read (N)" And from now the user will type in which multiplication table he wants to see I want to see the multiplication table of 5 . . .
He showed I will run again I want to see the multiplication table of 2 And he showed . . .
It was clear? So this is how we do We do simple exercises and ends up increasing the difficulty, this also is not so difficult, but more tweaking our logic we can do anything Let's get to our second practical example for us not to lose the chop and understand a little more about the structure "Repeat" The second proposal is a practice that I have read several numbers and in the end, show how many of them are negative Let's make this program. Put here .
. . "ContaNegativos" Is the name of my program I will always have a number to read so I have to read a number I will read that number several times So that "Read" has to be within the "Repeat" It is important that you understand the time to read inside a loop and out of a loop In my case here, as I read several times, it has to be within the "Repeat" In the previous case of the multiplication table, if you go back in the video, you will see that the reading was made out of "Repeat" this is because Once again There is no formula There is no recipe That outside of repeat must type Within repeat does not write This can be used any command can be used anytime Let's do this occurs 5 times To do so will create a counter, I'll call C Will start with 1 And it will be until C is greater than 5 So, He will repeat until C is greater than 5 Will read several numbers Need incrementing the value of C each loop Let's see if it will read .
. . 2, 3, 4, 5 figures and for running it Now I want to know how many numbers are negative Then I'll call variable "TotN" total negative At the beginning of the program, total negative 0, ie, it has no negative number After reading a number, I'll check If this number, for him to be negative, is less than zero If the number is less than zero TotN receives TotN plus 1 (TotN <- TotN + 1) As I will not be "Otherwise" clause, that is, if the number is positive I do not want to do anything "End if" At the end of the program, we will write on the screen the result "We typed" TotN, "negative" We will see?
Take a look at the code Pause the video Enter your own version And let me run together Let's type 3 least 1 eight less 7 six 5 typed values When you press enter, he has to show that these five values, two are negative. They were entered 2 negative and my logic is working properly And then just, you did the previous class, with all exercises all cute This class is being very easy right? The idea is exactly that, the algorithms As we go increasing the amount of information If you practice, it will not become more difficult, it gets easier And since practice makes ease Let's another practical example The third practical part is to calculate the factorial of a number And then, you can hit a certain desperation Because see factorial, math, do not know math Factorial is very simple, for example factorial of 5 is 5 times 4 times 3 times 2 and 1.
It is basically a countdown And we already did count in the previous class Let's redo now, only now using repeat. Let's create here, another algorithm, factorial algorithm Let's begin our countdown I'll create that whole kind of counter variable and the number you want factorial and we ask here, fazero type user Which number he wants to calculate the factorial So we put Enter a number and that number will be used to calculate the factorial then read (N) To make a countdown to N 1 My counter starts with N. What is the first value It will repeat Up until the counter Is equal to 1 In the case here, not less than 1 And each interaction, it will take a value of the counter Counter receives counter minus 1 (C <- C -1) We will write the value of C here Just to illustrate number 6 It will make 5, 4, 3, 2, 1 So factorial, that's basically it The value one symbol times "x" and other value Let's see here 5 is 5 times 4 times 3 times 2 times 1 (5 x 4 x 3 x 2 x 1) Note here, he got an X more Then we can even remove it.
But this, is the calculation will be done to show a factorial of a number 5 times 4 times 3 times 2 1 (5 X 4 X 3 X 2 1) is the factor calculation 5. So, Let's create a variable factor F And the factor will start with 1 This is because if it starts with zero, since it is a multiplication any number times zero is zero let's do this Factorial receive factorial times "C" at the end of repetition we write factorial of the value of "C" it's the same as "F" we see here number is "five" factor "five" It is equal to "120" note here I'll take this . .
. this type, so he does not have to count I'll put "five" the value of the factor five It is equal to "120" I'm going to test with another number for example: Factorial "one" is "one" And the factor of "tens" It is equal to 3,628,800 once again we display here the code for you to pause the video and make their own tests at home And now you already made your I come to propose another amendment, plus an additional to the program because here we're making a factor for a number only and if I want to make a factor to multiple numbers regardless of how many times the user will decide whether to continue? Yes or no?
there you will tell me: hey there! You are not using a repeating structure? And then I'll show an additional thing, which is to put a repeating structure within the other let's see it working in practice and sure enough, you will understand so basically doing like this: he will do it for a time we create our response variable down here we call "R" this time?
type "character" then I'll do all repeat up until my answer equal the "no" You want to continue? "no" prior to the I'll ask the question again I'll put "escrevaL" here Here's a "Type" You want to continue? Yes or not read "R" and will clear the screen let's see it working type a number "four" the value of the factor of "4" is "24" You want to continue?
"Yes" now I want to do the factor of "3" It is equal to "6" You want to continue? Yes, I want to once again As that is the factorial of "7"? It is 5,040 You want to continue?
No, I'm satisfied And he finished program execution We saw it was simple? so before I had a code to the factorial of a number and now I have some code to do, adding just two or three lines a code to make the factorial of several numbers and even ask pro user if he wants to continue or not and we will continue it has another practical example to us during this exercise class The last practice of this class It is to check whether a number is or is not "primo" there comes the Guanabara more math people, is mathematical algorithm algorithm is life, life is math what it is a "prime" number? a prime number is a number which is divisible only "one" and himself so for example: the number "three" it divisible by "A" It is not divisible by "two" and it is divisible by "three" to be divisible by "one" and "three" it is a prime number number "five" for example: it is divisible by "A" It is not "two not by "three" not by "four" and it is "five" by being divided by two ends he is prime the "four" number for example: it is divisible by "A" It is divisible by "two" not by "three" and it is by himself, "four" here it is divisible by 3 numbers So it is not prime then a prime number is a number that is on the count of 1 to it only 2 numbers divisible so let's start making a counting program simple that way so I'll enter a number it goes from 1 until this number then, I'm here with a program of prime number then, as we entered into an agreement here first you have to make a score "1" to a number that the user will enter so I'll have a counter and a number which are of the "whole" let's start counting "C" starts with "1" and repeat until "C" is greater than the number what number?
a number that I'm going to type up here I will read a number I began to count: goes from "1" to "N" wherein each interaction He will do that "C" receive "C + 1" this is a simple count, then write here "C" let me take this "L" here, only for him to write all on the same line so basically I'll type "7" it will count "1" to "7" I will type "9" it will count "1" through "9" I'll type "3" it will count "1" to "3" but it is not this count I want the count will help me but what I want to know is whether this number each one of them it is divisible the number entered then we'll check if "C" is divisible by "N" how do we do this? we utillizar an 'If' if the number counter divisável is by the number "C" "Divisible" is the remainder of the division counter by the number "zero", we have seen this before so that's if this number is divisible if the counter which is 1, 2, 3 . .
. to get the number is divisible I will do the following: I'll have a variable "ContDiv" (variable name) which is the counter divisísíveis in my case "ContDiv" will start with "zero" and acada time it is divisible My "ContDiv" will receive more "one" (+1) ContDiv more "A" in the end, we will show that "ContDiv" altogether there "ContDiv" divisible values we see working so for example: number 4" number 4": it is divisible by "1", By "2 and" 4 " we make a u here I changed the business is "N" here and "C" here pardon! I will check if "No" the number is divisible the counter not divisible by the number counter we illustrate here type a number "4" it will show a count of "4" then, "4", it divisible by "1" By "2" and "4" It is not divisible by "3" like this in all there are three values divisible let's see an example of a "prime number" for example, "7" is a prime number it is divisible only by "1" and "7" then it will show the count then there are two divisible values, i.
e. It is "1" and "7" and then, if you think a bit, you'll understand Divisible the counter is equal to "2" the number is prime if it is larger than this it is not prime let's solve this problem showing the very end that identifies whether a number is prime or not then here at the end instead of displaying the total dividers we will do the following: If the total splitters, which is the "ContDiv" is greater than "2. " So he will write the value "C" if it is greater than "2.
" it is not prime if no that is, if it is "2" or less it will write the value "N" is prime To "if" (ENDIF) And let's run as I said, number "3" is a prime number number "3" is prime we will remove the count here she no longer needed most let's run Number "3" "3" is prime we'll see another example "7" I talked to is prime It is really primo number "10", e. g. "10" is divisible by "1" with "10" but it is also divisible by "2" and "5" then he is not prime It is not prime "9" it is divisible by "1" through "3" and "9" then it is not prime but the number "5" that is divisible by him and "5" is a prime value what's up?
understood everything right? not understand? watching new class open your visualG practice just so you will learn and as is usual before I go I'll let you two examples to make two exercises for you to practice Alone just looking at the class and trying to do their first exercise I propose It is the "super meter" let's see it working you will understand what I'm talking about basically when I run this program I'm going to want this menu appears press "1" to count "1" to "10" press "2" to tell "10" to "1" and "3" to exit It was simple, right?
so let's see the result I pressed "1" he will tell "1" to "10" I pressed "2" he will tell . . .
"10" to "1" if I press "3" he exits the program This is easy So try to make Boot hands-on there, and try to make this count Let the second year that I want to leave you do at home After the super counter Let's do the choosing People We will select people According to its category Let's see the program running So we started the selector people. And ask first, what sex? Male or female?
I'll put men there How old? Twenty two Hair color? 1 is black, 2 is brown, 3 is blond, 4 is red.
There's black hair. You want to continue? Yes.
Will ask again. What is sex? Female.
Age twelve. Blond hair. You want to continue?
Yes So, what I want to select is as follows: I will enter several people. And in the end want to know, how many men over 18 have brown hair. And how many women are between 25 and 30 years and are blondes.
Let's see some examples here. working in our program. I'll cadastar A man.
Over eighteen, he has nineteen He has brown hair You want to continue? Yes I will now registering a woman Between 25 and 30 It has 27 He has blond hair So I have one of each I'll put another woman That fits in the category So woman with 29 years. blond hair too.
Post another, only this will not is a woman 29 years But her hair is red You want to continue? No And then what I want to answer is: I signed up to date a man that meets the need of there choice and two women We will see the final result, and see if I get it right You want to continue? No And there will trigger the final result What is The total number of men over 18 years old and brown hair Only one The total number of women between 25 and 30 years and with blond hair.
two And now it is with you Your work, your efforts Will make you learn even more about algorithm Mias still on repetition structure I hope this class is very useful For many people it is rolled with enough repetition structure Once again I wish you if you liked the project And you want to encourage to do more classes, more courses, more news into the site Video Course Do not forget, by clicking here, you sign the channel Here you receive information whenever you are a new class Up here you have cursoemvideo. com and here you will see really all classes, really all exercises download the packages, download the Visualg, download booklet, download everything necessary Clicking here you will have access to all classes from beginning to end A YouTube playlists But do not forget, up here is the most important, this is the site, this is where you have to enter To view all of the exercises, see everything that's going on within the Video Course, all that is being created So we already have the HTML5 course, we are creating the algorithm Course Comes more other courses around the front Do not forget to share gives joinha in the video, comment Contact us on Facebook is facebook. com/cursosemvideo So contact us, we share this idea, to show your friends and the course only grows up to you.
A big hug! Good studies and until next time! That was the first structure, which was the first, which was the first .
. . Ehhh .
. .