EXIT FOR Statement

8 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€92.81
List Price:  €129.94
You save:  €37.13
£79.68
List Price:  £111.56
You save:  £31.87
CA$136.70
List Price:  CA$191.38
You save:  CA$54.68
A$151.47
List Price:  A$212.07
You save:  A$60.59
S$134.88
List Price:  S$188.84
You save:  S$53.96
HK$781.18
List Price:  HK$1,093.68
You save:  HK$312.50
CHF 90.49
List Price:  CHF 126.69
You save:  CHF 36.20
NOK kr1,087.35
List Price:  NOK kr1,522.33
You save:  NOK kr434.98
DKK kr692.92
List Price:  DKK kr970.11
You save:  DKK kr277.19
NZ$166.33
List Price:  NZ$232.87
You save:  NZ$66.53
د.إ367.25
List Price:  د.إ514.16
You save:  د.إ146.91
৳10,943.35
List Price:  ৳15,321.13
You save:  ৳4,377.77
₹8,337.18
List Price:  ₹11,672.39
You save:  ₹3,335.20
RM473.95
List Price:  RM663.55
You save:  RM189.60
₦123,487.65
List Price:  ₦172,887.65
You save:  ₦49,400
₨27,738.77
List Price:  ₨38,835.40
You save:  ₨11,096.62
฿3,675.50
List Price:  ฿5,145.85
You save:  ฿1,470.34
₺3,235.04
List Price:  ₺4,529.19
You save:  ₺1,294.14
B$509.60
List Price:  B$713.47
You save:  B$203.86
R1,850.72
List Price:  R2,591.08
You save:  R740.36
Лв181.51
List Price:  Лв254.13
You save:  Лв72.61
₩135,590.93
List Price:  ₩189,832.73
You save:  ₩54,241.80
₪370.74
List Price:  ₪519.05
You save:  ₪148.31
₱5,705.78
List Price:  ₱7,988.32
You save:  ₱2,282.54
¥15,303.96
List Price:  ¥21,426.16
You save:  ¥6,122.20
MX$1,697.07
List Price:  MX$2,375.96
You save:  MX$678.89
QR363.69
List Price:  QR509.19
You save:  QR145.49
P1,358.38
List Price:  P1,901.79
You save:  P543.40
KSh13,211.65
List Price:  KSh18,496.84
You save:  KSh5,285.18
E£4,788.95
List Price:  E£6,704.73
You save:  E£1,915.77
ብር5,723.72
List Price:  ብር8,013.44
You save:  ብር2,289.71
Kz83,591.64
List Price:  Kz117,031.64
You save:  Kz33,440
CLP$94,219
List Price:  CLP$131,910.38
You save:  CLP$37,691.37
CN¥723.64
List Price:  CN¥1,013.13
You save:  CN¥289.48
RD$5,794.19
List Price:  RD$8,112.10
You save:  RD$2,317.90
DA13,457.95
List Price:  DA18,841.67
You save:  DA5,383.72
FJ$225.30
List Price:  FJ$315.43
You save:  FJ$90.13
Q775.06
List Price:  Q1,085.12
You save:  Q310.05
GY$20,860.22
List Price:  GY$29,205.14
You save:  GY$8,344.92
ISK kr13,949.49
List Price:  ISK kr19,529.85
You save:  ISK kr5,580.35
DH1,005.73
List Price:  DH1,408.07
You save:  DH402.33
L1,766.28
List Price:  L2,472.87
You save:  L706.58
ден5,712.52
List Price:  ден7,997.75
You save:  ден2,285.23
MOP$802.57
List Price:  MOP$1,123.63
You save:  MOP$321.06
N$1,845.78
List Price:  N$2,584.16
You save:  N$738.38
C$3,670.67
List Price:  C$5,139.09
You save:  C$1,468.41
रु13,313.56
List Price:  रु18,639.52
You save:  रु5,325.95
S/372.22
List Price:  S/521.13
You save:  S/148.90
K385.44
List Price:  K539.63
You save:  K154.19
SAR375.02
List Price:  SAR525.04
You save:  SAR150.02
ZK2,689.66
List Price:  ZK3,765.63
You save:  ZK1,075.97
L462.03
List Price:  L646.86
You save:  L184.83
Kč2,326.92
List Price:  Kč3,257.79
You save:  Kč930.86
Ft36,152.68
List Price:  Ft50,615.20
You save:  Ft14,462.51
SEK kr1,078.64
List Price:  SEK kr1,510.15
You save:  SEK kr431.50
ARS$87,815.44
List Price:  ARS$122,945.14
You save:  ARS$35,129.69
Bs690.51
List Price:  Bs966.74
You save:  Bs276.23
COP$388,367.89
List Price:  COP$543,730.59
You save:  COP$155,362.69
₡50,962.55
List Price:  ₡71,349.61
You save:  ₡20,387.06
L2,463.20
List Price:  L3,448.58
You save:  L985.38
₲746,475.93
List Price:  ₲1,045,096.16
You save:  ₲298,620.23
$U3,821.56
List Price:  $U5,350.33
You save:  $U1,528.77
zł401.98
List Price:  zł562.79
You save:  zł160.80
Already have an account? Log In

Transcript

Hey guys, welcome back. So so far, we've seen that the only way to exit a for loop is to actually reach the end for the counter, or to reach the last pass in the loop or the last iteration. So for example, for x equals one to 10, that means that the loop will not end unless we reach x to be equal to 10. We do the 10th pass or the 10th iteration through the loop. However, there is another way that you can exit a for loop at any point in the loop using the exit for statement. Now we're going to have an example here.

And although it's a trivial example, it represents the case when you want to exit a for loop based on a certain condition and you will not know when this condition is going to be satisfied or at which iteration it could be satisfied on the first iteration on the 10th edition. If you don't know, he just want to exit the loop when this condition is satisfied, okay? So this macro here that we're going to have is going to represent that case. So let's go through the macro here and know what it does. So basically, on column x here, I've put the values one 230 in cells x one, two X 30. The first step in this macro is that it's going to determine what the last row in column x is, which is going to be row number 30.

And then it's going to say for i equals one to LR and is basically our counter. And our LR is going to be 30 at all times because the last row and column x is 30. That is something that will not change. First of all, we're going to generate a random number and this random number is going to be an integer between wean one and the value of the LR, which is basically 30. And I don't need you to worry about the formula for generating this random number. Okay, I just want you to focus on the fact that this formula would just generate a random number between one and the value of LR, which is 30.

And then we're basically going to start selecting the cell based on the iteration loop. So we're going to select cells on E comma 24. So basically, if we're in the first iteration, we're going to select cell x one, if we're in the second iteration, we're going to select cell x two, and so on and so forth. And then we're going to test if the value of the active cell is equal to the random number. And if that's the case, we're going to get a message box that says we reached our goal after our iteration. So we're just going to report that we reached our goal after our iterations Actually, it is going to be the number of the iteration basically.

So this is why we put a variable for for this because we don't know when we're going to reach our goal. And our goal is to basically have the value of the active cell equal to the random number that we generated that is between one and 30. And then we're going to exhibit four and this exit for statement, what it does is it exits the for next loop. And it works with a for each loop as well that we're going to see in the future. So it's going to exit it and execute any statement after it. which in our case series just going to be the N sub statements so as to exit the macro.

However, what happens if we don't reach our goal? So if we go through all the 30 cells, and we test the values in the 30 cells against the random number, and we never have a random number equal to the value of the current active cell, we're gonna have a mess. Box, we haven't reached our goal with a sad face here. And then at this point, because we're going to have to be equal to the value of the last row or it to be equal to 30, we're going to have all our iterations finished. So we're basically going to end if and actually, there won't be any more pauses or iterations on the loop to do. So we're going to execute any statement after the for next loop, and it's going to be the end sub statement, and we're just going to exit the macro.

So here the purpose of this macro to this procedure is to show you the scenario when you're going to do a loop and you want to exit the loop based on a condition but you don't know when this condition is going to be satisfied. And hence, this is why we're generating a random number. Okay, so as to have the condition be satisfied at a point that you don't know when it's going to be satisfied. Okay, so you don't know on which pass loop or which iteration you're gonna have your condition satisfied. And this is why you're using these that for you testing for condition. And when it says fine to use the exit for statement.

So let's start executing the macro here, I'm going to go through it step by step by using f8 on my keyboard. So here, I'm going to get the value of the last row to be equal to 30. And then here is going to be for i is equal to one to 30. So we're going to do 30 passes on this for next loop. However, we might exit if we satisfy our condition before the 30th pass or the 33rd iteration. So now we're going to generate a random number is trying to remember is 25.

And we're going to select cells i comma 24, which in this case, because is equal to one, it's going to be cell x one, and then we're going to test is the value of the active cell equal to the random number. The value of the active cell is one and the value of the random number is equal to 25. So they are not equal and are we on the last iteration? No, we're not. So what are we going to do? We're just going to go into another pass on the loop.

We're going to generate another random number. It's 25 as well. So we're going to test is equal to the value of the active cell. No, it is not. So we're going to do it again and keep Pon doing. Now the active cell here is four and random numbers.

30. So no active cells five and random numbers. 28. So no, six and seven. No, they're not equal. Seven and 21.

They are not equal nine and eight. They are not equal 11 and four, so we're going to keep on doing that until they become equal, and they're still not equal on any pass. See, you guys the randomness factor here. We're generating a random number and we don't know on which pass, we're going to have to exit the loop 23 and 10 No, they're not equal. So here, we have not reached our goal, we've done 30 passes, and we could not get the value of the active cell to be equal to the random number. So we can execute the macro again.

And it says again, here, also 30 passes, we have not reached our goal. And here we reached our goal after 11 iterations. So you can see here that after 11 iterations, we've managed to have a random number equal to the value of the active cell. And what happened here is that it messagebox reached our goal after 11 iterations got executed and then we executed the exit for statement and we exited the loop basically, and we executed the unsub which ends our macro, okay, so this macro, although it is trivial, it's a trivial example, over illustrates the randomness factor when you want to exit a loop but you You don't know on which iteration you're going to need to exit that loop. So I hope that illustrates the exit for statement for you. It is used to basically exit your loop at any point.

And you can use it with a condition to exit the loop because you don't know on which iteration exactly, you're going to need to exit the loop. This is where you put a condition and based upon that, you execute the exit for statement and you exit the loop. Okay, so I hope that was informative for you. Thank you very much for watching this video and I'll see you on the next one.

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.