1
00:00:00,540 --> 00:00:03,010
So let's go back into our PowerShell console,

2
00:00:03,010 --> 00:00:07,030
we'll review how to use the do/while and do/until loops,

3
00:00:07,030 --> 00:00:15,640
and then we'll look at how we implement those inside PowerShell.

4
00:00:15,640 --> 00:00:20,440
So let's talk about the do/while and the do/until kind of flows and what

5
00:00:20,440 --> 00:00:23,240
that looks like when we execute it here within PowerShell.

6
00:00:23,240 --> 00:00:26,440
So the first thing we'll do is actually create a variable

7
00:00:26,440 --> 00:00:29,770
called number and assign that with a number 1.

8
00:00:29,770 --> 00:00:32,090
So that's going to be our starting one.

9
00:00:32,090 --> 00:00:36,010
Now, to use the do/while, we're going to use the do command,

10
00:00:36,010 --> 00:00:38,840
and then, of course, we have our script block.

11
00:00:38,840 --> 00:00:41,070
Then, of course, we have our while command,

12
00:00:41,070 --> 00:00:45,140
and then we have our script block of anything that we need to do.

13
00:00:45,140 --> 00:00:45,530
So,

14
00:00:45,530 --> 00:00:48,800
what I'm going to do is I'm actually going to do each piece, so we'll

15
00:00:48,800 --> 00:00:52,780
say Do, and then we'll do our first one. Go down here.

16
00:00:52,780 --> 00:00:55,540
And the first thing I want to do is just render the

17
00:00:55,540 --> 00:00:58,040
number that will display the number to us.

18
00:00:58,040 --> 00:01:01,140
And then, of course, because we're using this kind of process,

19
00:01:01,140 --> 00:01:06,140
I can do number++, that will increment my number for me.

20
00:01:06,140 --> 00:01:09,340
I can then close out that do loop.

21
00:01:09,340 --> 00:01:13,100
Then I can say while, and then in regular brackets,

22
00:01:13,100 --> 00:01:16,110
remember we're not using the squirrely brackets here for this one,

23
00:01:16,110 --> 00:01:22,290
I can say number and then say is less than or equal to the number 10.

24
00:01:22,290 --> 00:01:24,690
So that gives me my loop process.

25
00:01:24,690 --> 00:01:29,620
So we can say do while so and so. So I'm going to press Enter now,

26
00:01:29,620 --> 00:01:33,840
and you'll see it iterates and then literally just stops at number 10

27
00:01:33,840 --> 00:01:39,320
because that was the comparison operator or the statement at the end

28
00:01:39,320 --> 00:01:45,550
that will say keep doing this while that one is less than specific 10

29
00:01:45,550 --> 00:01:46,730
number, or whatever that would be,

30
00:01:46,730 --> 00:01:48,880
whether it's greater than, less than, equal to, it

31
00:01:48,880 --> 00:01:53,050
doesn't really make a difference, but it runs at the end. Now what that means is,

32
00:01:53,050 --> 00:01:53,470
of course,

33
00:01:53,470 --> 00:01:58,110
is that it will keep iterating all the numbers here, and only once it's

34
00:01:58,110 --> 00:02:02,340
increased the number and then hits this line will it ever execute.

35
00:02:02,340 --> 00:02:06,160
Now what about if we change that and do a different statement?

36
00:02:06,160 --> 00:02:12,180
So we're going to do the Do Until loop. So, same thing again,

37
00:02:12,180 --> 00:02:16,810
we're going to do Do, switch statement, Until a specific thing.

38
00:02:16,810 --> 00:02:18,850
So let's just change my code again.

39
00:02:18,850 --> 00:02:27,540
So Do question, we'll do number so and so, then we'll increase the number again.

40
00:02:27,540 --> 00:02:33,500
So we'll say number++, we'll close that Do piece out, and

41
00:02:33,500 --> 00:02:41,320
then we'll say until the number is greater than 10, and then

42
00:02:41,320 --> 00:02:44,440
we'll do Enter. Now what happens?

43
00:02:44,440 --> 00:02:48,990
It basically runs until it gets bigger, and once it

44
00:02:48,990 --> 00:02:51,110
goes past the bigger, it just stops.

45
00:02:51,110 --> 00:02:56,370
Now, what would happen if we just clear that and redid our do statement here?

46
00:02:56,370 --> 00:03:01,560
So number, number here Do Until and changed it to have

47
00:03:01,560 --> 00:03:03,730
the same syntax which we had before,

48
00:03:03,730 --> 00:03:07,530
which was less than or equal to. How many times would it iterate?

49
00:03:07,530 --> 00:03:11,050
Well if we enter, notice it just keeps going and going,

50
00:03:11,050 --> 00:03:13,440
I'm going to stop that from running.

51
00:03:13,440 --> 00:03:16,540
So if we go back to our code for a second,

52
00:03:16,540 --> 00:03:22,250
you can see it says do it Until the number is less than or equal to that.

53
00:03:22,250 --> 00:03:25,030
And, of course, it just continues to iterate.

54
00:03:25,030 --> 00:03:28,340
Now, what is the value of number?

55
00:03:28,340 --> 00:03:33,040
Well if we go to number, it's 13290.

56
00:03:33,040 --> 00:03:35,570
So, of course, when you're building something like this,

57
00:03:35,570 --> 00:03:39,970
notice an important thing is to actually clear the values out.

58
00:03:39,970 --> 00:03:42,760
So every time I would want to run,

59
00:03:42,760 --> 00:03:46,320
I need to have a default value that I set this back to.

60
00:03:46,320 --> 00:03:49,610
So if we go to our Do number,

61
00:03:49,610 --> 00:03:56,440
number, and Until and then Enter that, it will stop at number 1.

62
00:03:56,440 --> 00:03:59,660
So just a little kind of side note there to be careful that

63
00:03:59,660 --> 00:04:02,300
when you're using loops of any description,

64
00:04:02,300 --> 00:04:05,450
whether it's a do/until or a do/while and you're

65
00:04:05,450 --> 00:04:10,620
repopulating or increasing a variable, if you need it to be reset,

66
00:04:10,620 --> 00:04:19,000
you need to somehow have a reset option; otherwise, the next time it's called it begins at the number where you left off.

