Tag Archives: #mastery22

#Mastery22 What type of repetition should I use?

When programing, the use of repetition is very usfull, it makes a program shorter, and more efficient.
But there are many types of repetition in Python, witch one should I use?

Here is a qwick guide with my recomendations.

WHILE:

While loops are used when there is a factor that whill be changin ‘n’ times and you want to stop when said factor changes, also if the user is in control of the loop, if the factor is proned to change you use a while loop. (Learn More about While loops).
examples:

#Mastery22 What type of repetition should I use?x=0
while (x==0):
     print(“Hi”)
     x=int(input(“stop?, 0=no 1=yes”)

Another example.

FOR:

#Mastery22 What type of repetition should I use?For loops are used when you want your process to loop for the amount of time inside a range, When the amount of times it will loop is already pre-determined, either by the user or the programmer, you use a For loop.(Learn more about For loops).

1 example.

RECURSION:
Recursion is used when a function will repeat inside itself, func-ception, it will till a certian answer is reached, this one is tricky because you need to build the fuction with recursion in mind.
My most recent WSQs have been using recursion, so why dont you check that out?.

#Mastery22 What type of repetition should I use?


H0yos Adventures 2015-04-08 00:06:24

here is the link for the code of recursion example

https://github.com/Hoyos1148/Mastery-21-22/blob/master/recursion

here is the link for the video

https://www.youtube.com/watch?v=VjGRJaC3R80

Mastery 22

In order to eceive credit for the 22nd mastery, when a certain type of repetition should be used must be described.

There are 2 ways of creating repetition

1: Loops

     Examples of loops are ‘for’ or ‘do and while’ loops. These repetitions sould be used when large amouns of repetitions are needed.

2: Recursion

     A recursion is where a function is created to recall itself. This repetition should only be used when the amount of repetitions needed are fairly small. This is because the recursion process must recall itself fully each time making the amount of time between completions of reetitions grow exponentially.

#Mastery22

Here is the link to my video explaining .

Sorry because in the begining I say mastery23, is !

https://www.youtube.com/watch?v=vkK-MwaAn3Y

When to use what kind of repetition in a program

We have seen many way to make a loop, and one may wonder how con you know which one should be implemented?

This question is not that hard to answer:

  • While:

The while loop is implemented when we don’t know how many times a loop will be excecucted. For example when we want to do a cycle until we find something or we espect to find a certain value. For example:

>>> while(n>=0):

               n-1

In this case we don’t know what value will be given to n, so we use a while, so that whatever value it may take the cycle runs until it gets to cero. (Assuming n is not negative)

  • For:

The for loop is used in cases where we now exactly how many times we want the cycle to repeat itself, everytome in a cycle for we tell it how many times we want it to execute itself.

For example: when we want to print every value inside a list we use a for loop, wich we use as the index, another application of the for loop; when we want a variable that is increasing or decresing each time a cycle runs a for loop might solve our problem. To print the list we code something like this:

>>>for i in list:

      print(list[i])

Which means that i will take the values that cover the number of elements inside the list, and when we print we acces the element of the index i, wich will eventually sweep the hole list printing every element in order. 

This is my of my

When to use what type of repetition in a program

Using recursion will make your code easier to understand and shorter than other repetitions, but sometimes it’s just not the best way to do it.You have to make the code longer and with more statements.If you want your code to be super effective all the time you have to be very carful about what type of repetition you are going to use, because yes, the recursion is easier but when time is something to be considered you shouldn’t use the recursion type, because it takes more time than other types of repetition.

#Mastery22 #TC1014 #LaNieveDeColimaExploto #PenguinLove

En algunos casos los codigos tienen que ser mas largos para ser mas eficaces

#mastery 22

 

In this mastery I am going to expalin when to use a type of repetition.

 

FOR:

We can use for as a loop when you know very well how many times the repetition will be done.

example:

for(i=1; i<=10; i++)      In this case I know that the loop is going to be repeated 10 times, so now I can use a FOR loop

 

DO WHILE:

We use a do while when you are not sure how many times the loop will be repeated, it is going to continue while the conditional is true, when it is not it will go out of the loop.

example:

do {

cout <<“Hello<< endl; 

} while (nu

1==1); 

as you could see we d

mo not know how many times it is going to be done 

 

WHILE DO:

It is exactly the same than the do while, but in this case the structure is the oposite and at the first time it is going to do the order if the condition is true.

example:

while (num1==1); do {

cout<<“Hello”<< endl;

}

 

 

When to use what type of repetition in a program

When you need the loop to go on and on until the conditional is false, it’s convenient to use a while loop. When you have a predefined range that works as the conditional in a program you need to use for loops, finally when you need to call a function inside the same function it’s called a recursion, it makes it simpler but not always works as efficiently, each of these have different purposes, it’s a matter of knowing which suits the logic of the program best.

Here’s an example of each:

When to use what type of repetition in a program

When to use what type of repetition in a program

When to use what type of repetition in a program

When to use what type of repetition in a program

Mastery 22 When to use what type of repetition in a program

I already talk about this in the last mastery, well like I said using recursion it will makw our code more easy to understand and shorter tha other repetition, but sometimes is not the better option.So we have to make the code longer and with more statements.If we want that our code be very effective all the time we have to be very carful about what type of repetition we are going to use, because yes the recursion is easier, but when the time is a priority we must not use the recursion type, because of the time, it takes more time than others.