Repetition Structures

Introduction to Programming Decision Structures
12 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€65.43
List Price:  €93.47
You save:  €28.04
£55.38
List Price:  £79.12
You save:  £23.73
CA$95.75
List Price:  CA$136.80
You save:  CA$41.04
A$107.11
List Price:  A$153.03
You save:  A$45.91
S$95.36
List Price:  S$136.24
You save:  S$40.87
HK$547.90
List Price:  HK$782.75
You save:  HK$234.85
CHF 63.27
List Price:  CHF 90.39
You save:  CHF 27.12
NOK kr772.60
List Price:  NOK kr1,103.76
You save:  NOK kr331.16
DKK kr487.88
List Price:  DKK kr697.01
You save:  DKK kr209.12
NZ$117.84
List Price:  NZ$168.36
You save:  NZ$50.51
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,700.43
List Price:  ৳11,001.09
You save:  ৳3,300.65
₹5,837.33
List Price:  ₹8,339.41
You save:  ₹2,502.07
RM333.67
List Price:  RM476.70
You save:  RM143.02
₦92,489.99
List Price:  ₦132,134.22
You save:  ₦39,644.23
₨19,533.24
List Price:  ₨27,905.82
You save:  ₨8,372.58
฿2,588.46
List Price:  ฿3,697.97
You save:  ฿1,109.50
₺2,275.23
List Price:  ₺3,250.47
You save:  ₺975.23
B$358.08
List Price:  B$511.57
You save:  B$153.48
R1,314.42
List Price:  R1,877.82
You save:  R563.40
Лв127.95
List Price:  Лв182.79
You save:  Лв54.84
₩96,452.39
List Price:  ₩137,795.03
You save:  ₩41,342.64
₪267.23
List Price:  ₪381.78
You save:  ₪114.54
₱4,034.64
List Price:  ₱5,764.02
You save:  ₱1,729.38
¥11,065.06
List Price:  ¥15,807.91
You save:  ¥4,742.85
MX$1,201.03
List Price:  MX$1,715.83
You save:  MX$514.80
QR255.83
List Price:  QR365.49
You save:  QR109.65
P965.79
List Price:  P1,379.76
You save:  P413.97
KSh9,296.24
List Price:  KSh13,280.92
You save:  KSh3,984.67
E£3,349.82
List Price:  E£4,785.67
You save:  E£1,435.84
ብር4,026.64
List Price:  ብር5,752.60
You save:  ብር1,725.95
Kz58,375.85
List Price:  Kz83,397.65
You save:  Kz25,021.80
CLP$66,628.33
List Price:  CLP$95,187.41
You save:  CLP$28,559.08
CN¥507.11
List Price:  CN¥724.48
You save:  CN¥217.36
RD$4,113.13
List Price:  RD$5,876.15
You save:  RD$1,763.02
DA9,393.54
List Price:  DA13,419.92
You save:  DA4,026.38
FJ$158.35
List Price:  FJ$226.22
You save:  FJ$67.87
Q545.71
List Price:  Q779.62
You save:  Q233.91
GY$14,678.83
List Price:  GY$20,970.66
You save:  GY$6,291.82
ISK kr9,808.34
List Price:  ISK kr14,012.52
You save:  ISK kr4,204.17
DH708.72
List Price:  DH1,012.51
You save:  DH303.78
L1,243.72
List Price:  L1,776.81
You save:  L533.09
ден4,025.16
List Price:  ден5,750.48
You save:  ден1,725.31
MOP$565.76
List Price:  MOP$808.27
You save:  MOP$242.50
N$1,323.80
List Price:  N$1,891.23
You save:  N$567.42
C$2,582.07
List Price:  C$3,688.83
You save:  C$1,106.76
रु9,353.99
List Price:  रु13,363.42
You save:  रु4,009.42
S/263.57
List Price:  S/376.55
You save:  S/112.97
K270.82
List Price:  K386.91
You save:  K116.08
SAR262.49
List Price:  SAR375
You save:  SAR112.51
ZK1,859.31
List Price:  ZK2,656.27
You save:  ZK796.96
L325.94
List Price:  L465.65
You save:  L139.71
Kč1,644.39
List Price:  Kč2,349.23
You save:  Kč704.84
Ft25,663.75
List Price:  Ft36,664.08
You save:  Ft11,000.32
SEK kr762.14
List Price:  SEK kr1,088.81
You save:  SEK kr326.67
ARS$61,304.48
List Price:  ARS$87,581.58
You save:  ARS$26,277.10
Bs486.59
List Price:  Bs695.16
You save:  Bs208.56
COP$277,527.04
List Price:  COP$396,484.19
You save:  COP$118,957.15
₡35,650.64
List Price:  ₡50,931.67
You save:  ₡15,281.02
L1,732.56
List Price:  L2,475.19
You save:  L742.63
₲522,432.19
List Price:  ₲746,363.69
You save:  ₲223,931.50
$U2,704.33
List Price:  $U3,863.50
You save:  $U1,159.16
zł282.07
List Price:  zł402.98
You save:  zł120.90
Already have an account? Log In

Transcript

And welcome to lesson 14 repetition structures. In this lesson we are going to learn about loops and how they can are used and how they can be controlled so that we don't have to write tons and tons and tons of code. So welcome to this lesson you're going to learn a lot please feel free to listen to it again if you need to. So, what is repetition structure? A repetition structure causes a statement or a set of statements to execute repeatedly. So if we wanted it to print hello world 30 times we could actually tell it to do that or we may want some other reason for it to jump out of a loop.

With repetition structures what we have our we have conditional loops count control loops Sentinel loops and nested loops. And conditional loops, we have a while a do while and a do until, and we're going to talk about these loops, the conditional loops on the next slide. Every repetition structure allows you to go through the loop as many times as you want, and then there must be some sort of condition to allow it to exit the loop. So the while loop is a loop, as long as a condition is true. It's gonna process the statement. While the condition is false, then it's going to exit the loop.

And this is called a pretest loop, meaning it's good to test the condition before it executes the statements. So if we look at our words, right down here below, where I have an example where it says While number less than final number print, you can keep going. So number and final number are variables and there's a value inside that variable. The user could enter a number, that maybe that's the final number, maybe that's the number that we want it to go through the loop and do that. And then we could use number to start out at zero. And every time we go through, and we execute that print statement, we could add one two number until number finally becomes equal to final number and then that statement wouldn't be true anymore.

And so the loop would stop and the program would move below the loop. Let's look at the picture here. Of course we have our stock start, and then we have our condition number has to be less than final number. If that's true, then we're going to print, you can keep going. If it's false, then that's going to end the loop. So once again, while number is less than final number, we can print, you can keep going.

If it's not less than the final number, then we're going to add. So what happens is if we have a user that enters the final number, we can pop up question and say, how many times would you like this program to run? And then we can call our variable final number. Say the user says five times. If we're looking at our diagram over here, and the user says five times, we will start out asking the user how many times would you like the program to run the user would put in the number five and that would be the final number variable. And then once we print, you can keep going, we're going to add one to number, a number is going to become two.

And then two is still less than the final number, which is five. So the condition is still true. And we'll print it again, you can keep going, then we'll add another number to the variable number and that will become three, three will still be less than final number, so it's still true. And it will print again, you can keep going, we'll add another number to number making it four, four is still less than final number, so it will print you can keep going. Then it's going to add another number to the variable number, which will be five, it will no longer be less than the final number. So it will make the decision to go on the no side and then end the program.

The do while loop is similar to the while loop except it's going to do the process statement at least once and then it's going to test the condition. So while the condition is true, continue to process the statement and while the condition is false exit the loop. So do print Do you want to continue type while you continue or no to quit, as long as the user puts in Why? It will print enter your name, but if the user puts in the letter N, the loop will stop. So we use a do while loop when we want it to execute at least one time. The do until loop is a lot like the do while loop where it's going to process statement wants and then test the condition.

Now while the condition is true Continue to process that statement. And then until the condition is false, we'll exit the loop. So we're going to do print, do you want to continue type? Why do you continue into quit until the continue interest equals No, we're going to print enter your name. So it's just going to run until that changes. Another way that we can control how our loop runs is called a count control loop.

So we use a count control loop when we know the number of iterations and we will use a for loop whenever we want to do that. And we have a counter variable involved. So if we look at our example below, it says for counter variable, each equal max value, process the statement and then and the for. So what we're doing here is we're saying we have a counter variable. We want to equal max variable, it can be the same thing as what we saw before, we could ask the user, how many times did they want the loop to iterate, and that could be the max value. And then every time we go through the loop, we add one to the counter variable, so that when the counter variable reaches the max value than the loop will stop.

When we're doing this type of loop, we do not want to forget to add one to that counter variable, because what will happen is it'll never stop. If we have a loop that doesn't have a condition to let it exit. It never ever stops. And what happens is it keeps going keeps going and keeps going. And then eventually the server is just going to crash because it just can't take anymore. So sentinels very similar to that for loop and every other loop structure, in that you have to have a value that marks the end of the loop if you don't have some sort of escape from the loop again, it's gonna keep going on forever and ever and ever, and then it's going to crash.

Now the reason we call this is Sentinel is because we're going to give it a specific value in order to exit the loop. So if you look at the picture, we're saying, Would you like to see a friendly message, press one for yes and zero to quit. And if the user presses zero, that's the sentinel value, that is the special value that it needs in order to quit. You could say use q to quit or whatever you wanted to do. If you want to have them put in a string value to print then you could have them do that. We just have to make sure that our types and our values match up here.

So would you like to see your friendly message for one for yes or zero to quit and then get the number of times for that? If it's Yes, in the times or are not zero, then we're going to continue to do something and if it's No, and I noticed here that I have it backwards, where if it's no, it's going to print hello world. And if it's Yes, it's not. So that is a mistake that I made in my programming. And that's going to be a problem because it's never going to print anything until n equals zero, and then it's going to print it once. But you know what, maybe we wanted it to do that.

We can put messages and commands in no as well as on the side. So we can also have nested loops. And what that means is we have a loop inside another loop. And so when we do that, the inner loop is going to go through all of its iterations for every single iteration of the outer loop. So the outer loop starts it, then it jumps to the inner loop as long as that condition is true. The inner loop will keep going keep going, keep going until it has an exit.

And then because it's inside another loop, it's just going to jump back to the outer loop. So the inner loops are going to complete the iterations faster than the outer loops. And they could also complete it way many more times, for example. So let's look at the example that we have over here. We have a loop inside a loop. So we ask the user how many students do you have only we get a number of students, and then we say how many test scores do you have?

So the first loop, we're asking for the student number, and the students name, and then the second loop, we're asking for the test scores. And then once we get all the students and then we get all of the test scores for all of the students. We're going to process an average To let the teacher know what the average score was. So take a good long look at that and see if you can follow through that program. And remember, a loop inside a loop will go many times. So let's go ask the student.

As long as that's true, then we jump into the next loop. And then we run all the scores for that student and then we go back to the outer loop, we get another student name, and then we run all the scores for that student. Yeah, you did a really great job this time loops can get very, very complicated repetition structures can get very, very complicated. So but remember, at this point, we're learning all the main concepts so that we can go on to learn programming languages. At this point, what I want you to be comfortable with is to make sure that you understand what a repetition structure is, what are the differences kinds of repetition structures and what do they do and how do they work? Those are the important things to know.

When you start learning a programming language, you are going to be bogged down in syntax and rules. And some programming languages are a lot more complicated than other programming languages. So it's very, very important to understand all the basic concepts before you actually get started on the programming language but you did a fantastic job. pat yourself on the back. So now, on to the next lesson.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.