1
00:00:00,340 --> 00:00:04,030
So let's talk about using Do‑While and Do‑Until as

2
00:00:04,030 --> 00:00:06,440
part of a loop within PowerShell.

3
00:00:06,440 --> 00:00:09,530
So the first thing to understand is what is a loop?

4
00:00:09,530 --> 00:00:09,710
Well,

5
00:00:09,710 --> 00:00:14,890
a loop is a programming and a scripting language construct that allows

6
00:00:14,890 --> 00:00:18,960
you to process a sequence of instructions inside code.

7
00:00:18,960 --> 00:00:24,100
It also allows iteration of several items or several times,

8
00:00:24,100 --> 00:00:28,180
depending on how you're iterating, as long as the condition is met.

9
00:00:28,180 --> 00:00:29,480
So, for example,

10
00:00:29,480 --> 00:00:32,250
let's say you want to see the number of files in a

11
00:00:32,250 --> 00:00:36,300
folder that can execute and say, go and get me the folder,

12
00:00:36,300 --> 00:00:39,240
iterate through the folder, identify the items,

13
00:00:39,240 --> 00:00:41,320
and then once you've identified all of them,

14
00:00:41,320 --> 00:00:42,060
then stop.

15
00:00:42,060 --> 00:00:48,040
Or it could be, count a certain amount and then write those values out.

16
00:00:48,040 --> 00:00:51,940
Now there are specific loop types that are available to us.

17
00:00:51,940 --> 00:00:54,890
The first one is what's referred to as a For loop.

18
00:00:54,890 --> 00:00:58,640
So a For loop is known also as a For statement,

19
00:00:58,640 --> 00:01:01,420
and it's typically used to iterate through a set of

20
00:01:01,420 --> 00:01:05,040
commands a specified number of times.

21
00:01:05,040 --> 00:01:07,260
You then also have what's called a While.

22
00:01:07,260 --> 00:01:14,240
This repeats a command or a group of commands where the given condition is true.

23
00:01:14,240 --> 00:01:15,710
Then, of course, we have the Do‑While.

24
00:01:15,710 --> 00:01:18,140
So just like the While loop,

25
00:01:18,140 --> 00:01:23,670
it will execute the block of statements while the condition evaluates to a true,

26
00:01:23,670 --> 00:01:26,640
so do while this is true.

27
00:01:26,640 --> 00:01:27,770
But there's a small difference.

28
00:01:27,770 --> 00:01:32,490
That is the body of the Do‑While is executed first,

29
00:01:32,490 --> 00:01:35,480
then the condition gets tested at the end,

30
00:01:35,480 --> 00:01:40,530
whereas the Do‑Until have a similar syntax to the Do‑While

31
00:01:40,530 --> 00:01:43,670
except the condition is evaluated first.

32
00:01:43,670 --> 00:01:44,760
So think of that logic.

33
00:01:44,760 --> 00:01:48,700
Iterate through these items, compare the value at the bottom,

34
00:01:48,700 --> 00:01:51,880
or compare the value at the top that you've got,

35
00:01:51,880 --> 00:01:54,440
and then continue.

36
00:01:54,440 --> 00:01:57,960
We also then have what's referred to as a Foreach‑Object.

37
00:01:57,960 --> 00:02:03,890
So this is a cmdlet, not a loop, but just like the Foreach statement,

38
00:02:03,890 --> 00:02:04,890
which is another one,

39
00:02:04,890 --> 00:02:10,440
it iterates through each object in a collection and performs operations.

40
00:02:10,440 --> 00:02:14,840
Then we have what's called the Foreach statement, or the Foreach loop.

41
00:02:14,840 --> 00:02:18,400
This is used to iterate through values in a collection of items,

42
00:02:18,400 --> 00:02:23,340
for example, in an array, and then perform actions against each item.

43
00:02:23,340 --> 00:02:26,960
And then, of course, we have an odd one, which is the Foreach() method.

44
00:02:26,960 --> 00:02:30,330
So you can specify a collection or an array.

45
00:02:30,330 --> 00:02:33,610
So let's say we create a variable called at variable

46
00:02:33,610 --> 00:02:36,540
and populate it with numbers 1 to 10.

47
00:02:36,540 --> 00:02:39,070
We can then use the Foreach() method,

48
00:02:39,070 --> 00:02:43,190
which effectively is accessed by going to the end of the variable,

49
00:02:43,190 --> 00:02:45,780
type in a period, and then using Foreach(),

50
00:02:45,780 --> 00:02:50,340
and that can then go through and iterate as well.

51
00:02:50,340 --> 00:02:50,790
Now,

52
00:02:50,790 --> 00:02:54,240
we need to understand the difference between an entry and an exit

53
00:02:54,240 --> 00:02:57,540
loop before we can choose which type to use.

54
00:02:57,540 --> 00:03:03,660
So an entry loop tests the condition is checked before it enters into the loop.

55
00:03:03,660 --> 00:03:04,680
So that's your first one.

56
00:03:04,680 --> 00:03:09,140
The entry loop is, does this value do something?

57
00:03:09,140 --> 00:03:11,190
If so, then continue the loop.

58
00:03:11,190 --> 00:03:15,540
Whereas an exit loop is keep looping and then check the

59
00:03:15,540 --> 00:03:18,340
value at the end and drop out that way.

60
00:03:18,340 --> 00:03:19,360
So what does that look like?

61
00:03:19,360 --> 00:03:21,000
Well, let's look at the difference between the two.

62
00:03:21,000 --> 00:03:23,840
So I have a variable, which is number 1,

63
00:03:23,840 --> 00:03:26,190
and then I have what's referred to as an entry loop,

64
00:03:26,190 --> 00:03:28,430
so this is using a While loop.

65
00:03:28,430 --> 00:03:32,230
So what you can see here, it says, While $number,

66
00:03:32,230 --> 00:03:37,910
so 1, is less than 10, then perform your specific actions.

67
00:03:37,910 --> 00:03:39,100
So in this instance,

68
00:03:39,100 --> 00:03:42,300
it's going to just increment the number by one and keep

69
00:03:42,300 --> 00:03:47,690
going until or while it's less than 10.

70
00:03:47,690 --> 00:03:51,590
So if my number in the right‑hand side, which is $number++,

71
00:03:51,590 --> 00:03:53,440
increments up to 10,

72
00:03:53,440 --> 00:03:58,540
it'll then stop working at that point because it's no longer less than 10.

73
00:03:58,540 --> 00:04:03,180
If we do the Do option, you can see it flips the syntax the other way around.

74
00:04:03,180 --> 00:04:07,070
So it now says, here's my number, increment the number,

75
00:04:07,070 --> 00:04:10,220
and while that implemented number is less than 10,

76
00:04:10,220 --> 00:04:11,040
continue to happen.

77
00:04:11,040 --> 00:04:13,020
Now you may look at that and think, well,

78
00:04:13,020 --> 00:04:15,220
that's okay, it looks very similar,

79
00:04:15,220 --> 00:04:18,690
but it can have completely different effects on the

80
00:04:18,690 --> 00:04:20,210
PowerShell output that we're using.

81
00:04:20,210 --> 00:04:25,750
Now the Do‑While loop is almost the same as a regular While loop.

82
00:04:25,750 --> 00:04:29,730
It executes the block of statements while the condition returns true.

83
00:04:29,730 --> 00:04:35,720
The Do‑While executes first, then the condition test is at the end of the loop.

84
00:04:35,720 --> 00:04:39,260
Now for the Do‑While, the syntax can be very similar.

85
00:04:39,260 --> 00:04:44,840
So you can see it'll say do something while this condition is met.

86
00:04:44,840 --> 00:04:47,510
So if we have a variable again with a number 1,

87
00:04:47,510 --> 00:04:50,830
you can see that it looks very similar to the Do‑While.

88
00:04:50,830 --> 00:04:54,720
So Do $number, which would say number 1,

89
00:04:54,720 --> 00:04:59,980
increment number 2 while that number that you've incremented is less than 10.

90
00:04:59,980 --> 00:05:06,140
So that would then continue to run until that point.

91
00:05:06,140 --> 00:05:10,140
So what are the differences between a While and a Do‑While loop?

92
00:05:10,140 --> 00:05:14,430
Well, if we compare them, the While loop is an entry controlled loop,

93
00:05:14,430 --> 00:05:19,100
so it comes in at the point here and then tests the condition

94
00:05:19,100 --> 00:05:23,540
before the execution of the first loop or iteration.

95
00:05:23,540 --> 00:05:27,310
The loop doesn't execute when the condition evaluates to false.

96
00:05:27,310 --> 00:05:30,210
So, remember the less than 10?

97
00:05:30,210 --> 00:05:34,770
Once it reaches 10, it's no longer less than, so the loop continues to execute.

98
00:05:34,770 --> 00:05:40,240
And it doesn't require any other syntax to be able to make that work.

99
00:05:40,240 --> 00:05:43,840
The Do‑While loop is an exit controlled loop.

100
00:05:43,840 --> 00:05:47,910
It tests the condition after the execution of the first iteration.

101
00:05:47,910 --> 00:05:53,920
So it will keep going down, and until it reaches that specific number,

102
00:05:53,920 --> 00:05:55,170
it won't stop.

103
00:05:55,170 --> 00:05:59,260
The loop executes at least once every single time.

104
00:05:59,260 --> 00:06:02,830
So whereas the While loop can actually evaluate to false

105
00:06:02,830 --> 00:06:07,920
immediately and then never execute, the Do‑While will always execute once.

106
00:06:07,920 --> 00:06:12,350
It uses the Do keyword at the starting of the loop and then a

107
00:06:12,350 --> 00:06:16,440
While keyword with a condition at the end.

108
00:06:16,440 --> 00:06:21,040
Now the Do‑Until loop is very similar to a Do‑While loop.

109
00:06:21,040 --> 00:06:24,940
The loop will repeat until the condition returns true,

110
00:06:24,940 --> 00:06:29,440
and these are the opposite of a Do‑While loop.

111
00:06:29,440 --> 00:06:31,560
Now that syntax is very similar.

112
00:06:31,560 --> 00:06:35,030
Instead of it saying Do‑While, it's Do‑Until,

113
00:06:35,030 --> 00:06:39,440
so do this command until this condition is met.

114
00:06:39,440 --> 00:06:42,150
So if we use our standard variable, $number = 1,

115
00:06:42,150 --> 00:06:44,070
and you can see we have a Do‑Until.

116
00:06:44,070 --> 00:06:46,480
So, think how this works.

117
00:06:46,480 --> 00:06:48,310
Do 1.

118
00:06:48,310 --> 00:06:53,170
Increase the number by one until it's less than or

119
00:06:53,170 --> 00:06:54,710
greater than or something else.

120
00:06:54,710 --> 00:06:59,740
Now notice it would be different, so Do‑Until the number is less than 10.

121
00:06:59,740 --> 00:07:02,250
Sounds like a strange way of doing it, but that's what I said.

122
00:07:02,250 --> 00:07:07,730
Depending on the looping mechanism that you use will have different

123
00:07:07,730 --> 00:07:10,660
effects on the output and what you want to achieve.

124
00:07:10,660 --> 00:07:16,080
Now the Do‑While loop is an exit controlled loop.

125
00:07:16,080 --> 00:07:19,150
It uses the While keyword for the condition and then

126
00:07:19,150 --> 00:07:22,140
executes when the condition returns true.

127
00:07:22,140 --> 00:07:25,540
The Do‑Until loop is an exit controlled loop.

128
00:07:25,540 --> 00:07:33,000
It uses the until keyword and then continues execution until the condition returns true.

