1
00:00:01,800 --> 00:00:04,630
In this session, we will continue the program
that we were writing.
2
00:00:04,630 --> 00:00:09,680
Recall that, we were writing a while loop,
which will read a bunch of numbers.
3
00:00:09,680 --> 00:00:15,309
And it is supposed to sum them up, until you
hit a minus 1.
4
00:00:15,309 --> 00:00:20,470
In the loop that we have seen, so far we just
read the numbers until minus 1 was encountered.
5
00:00:20,470 --> 00:00:25,009
So, let us now complete the program and compute
their sum as well.
6
00:00:25,009 --> 00:00:29,210
So, for computing there sum, how do we normally
do it?
7
00:00:29,210 --> 00:00:32,290
We will add numbers two at a time.
8
00:00:32,290 --> 00:00:34,360
So, the first two numbers will be added.
9
00:00:34,360 --> 00:00:40,179
Then, that sum will be added to the third
number and so on, until you hit a minus 1.
10
00:00:40,179 --> 00:00:46,199
So, let us try to do that, in the course of
a while loop.
11
00:00:46,199 --> 00:00:50,399
What I will declare is, I will declare a new
variable s.
12
00:00:50,399 --> 00:00:54,629
So, here is the new variable s that, I have
declared.
13
00:00:54,629 --> 00:00:59,660
s is supposed to the hold the sum of the variables
that, I have read so far.
14
00:00:59,660 --> 00:01:06,640
Now, it is very, very important that, when
you declare a variable, you should initialize
15
00:01:06,640 --> 00:01:09,040
them properly.
16
00:01:09,040 --> 00:01:13,740
In the case of a, we did not initialize it
because, we were reading the first number,
17
00:01:13,740 --> 00:01:16,130
as soon as was declared.
18
00:01:16,130 --> 00:01:23,410
But, in the case of sum, you would use s to
maintain the sum, as you read numbers.
19
00:01:23,410 --> 00:01:26,310
So, it is important that, you start with s
equal to 0.
20
00:01:26,310 --> 00:01:32,570
So, the initialization step marked by this
arrow is quite important.
21
00:01:32,570 --> 00:01:37,380
If you do not initialize it properly, then
the sum may not be correct, as we will see
22
00:01:37,380 --> 00:01:38,380
soon.
23
00:01:38,380 --> 00:01:44,460
So, we keep a variable s, which is supposed
to hold the sum of n numbers, sum of these
24
00:01:44,460 --> 00:01:48,190
numbers and initialize the sum to 0.
25
00:01:48,190 --> 00:01:53,530
Then, the difference from the loop that, we
have seen so far is highlighted in red.
26
00:01:53,530 --> 00:01:57,280
So, earlier recall that, we were reading the
number.
27
00:01:57,280 --> 00:02:02,020
And just testing, whether the number is minus
1, if it was not minus 1, you read one more
28
00:02:02,020 --> 00:02:04,310
number so, that was the loop.
29
00:02:04,310 --> 00:02:11,560
Now, inside the loop, what we will do is,
we will keep up, running sum of the numbers
30
00:02:11,560 --> 00:02:13,230
that we have seen so far.
31
00:02:13,230 --> 00:02:17,890
So, initially s sum is initialized to 0.
32
00:02:17,890 --> 00:02:22,750
Then, if the first number is not minus 1,
you add the first number to s.
33
00:02:22,750 --> 00:02:25,710
So, s will now be the first number.
34
00:02:25,710 --> 00:02:28,260
Now, read the second number.
35
00:02:28,260 --> 00:02:31,870
If the second number is not minus 1, you will
enter the loop again.
36
00:02:31,870 --> 00:02:34,110
So, you will add the second number to s.
37
00:02:34,110 --> 00:02:38,770
So, s is now first number plus second number.
38
00:02:38,770 --> 00:02:43,070
And this keeps on going until you hit minus
1, in the input.
39
00:02:43,070 --> 00:02:49,550
So, let us continue with this.
40
00:02:49,550 --> 00:02:56,040
Let us try to trace the execution of this
program, on a sample input and try to understand,
41
00:02:56,040 --> 00:02:58,020
how it works.
42
00:02:58,020 --> 00:03:03,610
Let us say that, I compile the program successfully
and run the program.
43
00:03:03,610 --> 00:03:11,140
So, I run a dot out and let us as before,
let us the first number be 4.
44
00:03:11,140 --> 00:03:19,740
So, after initialization, when you declare
the variable a is undefined and s is also
45
00:03:19,740 --> 00:03:21,090
undefined.
46
00:03:21,090 --> 00:03:24,900
After the initial statement, s equal to 0,
s is now 0.
47
00:03:24,900 --> 00:03:27,340
And then, you scan the variable a.
48
00:03:27,340 --> 00:03:35,360
So, a becomes 4, because 4 was the input.
49
00:03:35,360 --> 00:03:39,180
And sum is still 0.
50
00:03:39,180 --> 00:03:44,550
You enter the loop and you say, s equal to
s plus a.
51
00:03:44,550 --> 00:03:48,180
So, sum becomes 0 plus 4, which is 4.
52
00:03:48,180 --> 00:03:49,180
And you read the next number.
53
00:03:49,180 --> 00:03:51,530
Let us say the next number was 15.
54
00:03:51,530 --> 00:03:55,410
So, a becomes 15, a is not minus 1.
55
00:03:55,410 --> 00:03:58,349
Therefore, you enter the loop again.
56
00:03:58,349 --> 00:04:06,360
And sum is now 4 plus 15, which is 19.
57
00:04:06,360 --> 00:04:12,270
So, sum at any point of time is the sum of
the number that, we have read so far.
58
00:04:12,270 --> 00:04:14,250
So, we have read 4 and 15.
59
00:04:14,250 --> 00:04:15,730
So, the sum is 19.
60
00:04:15,730 --> 00:04:18,530
Now, you read the next number.
61
00:04:18,530 --> 00:04:25,889
Let us say, the next number was minus 5.
62
00:04:25,889 --> 00:04:27,469
Minus 5 is not minus 1.
63
00:04:27,469 --> 00:04:32,259
Therefore, you enter the loop again s equal
to s plus minus 5.
64
00:04:32,259 --> 00:04:34,749
So, s becomes 14.
65
00:04:34,749 --> 00:04:36,840
Then, you read the next number.
66
00:04:36,840 --> 00:04:39,659
And let us say, the next number was minus
1.
67
00:04:39,659 --> 00:04:42,889
So, since the number read is minus 1.
68
00:04:42,889 --> 00:04:44,820
You go back to the loop.
69
00:04:44,820 --> 00:04:47,789
And this condition becomes false.
70
00:04:47,789 --> 00:04:53,409
So, you exit out of the loop.
71
00:04:53,409 --> 00:04:59,500
And then print that, the sum is let us say
14.
72
00:04:59,500 --> 00:05:06,270
So, when you verify it by hand, you would
see 4 plus 15 plus minus 5 is 14.
73
00:05:06,270 --> 00:05:09,740
So, you have, the program has executed correctly.
74
00:05:09,740 --> 00:05:15,030
The important thing to note is, the final
minus 1 is not summed up.
75
00:05:15,030 --> 00:05:21,360
So, that is, it is used as the end of the
input and you should not compute the sum of
76
00:05:21,360 --> 00:05:22,979
the numbers, including minus 1.
77
00:05:22,979 --> 00:05:24,639
Minus 1 is excluded.
78
00:05:24,639 --> 00:05:27,229
Then, the program executed, correctly.
79
00:05:27,229 --> 00:05:33,650
We will introduce a few terminology associated
with the notion of a loop.
80
00:05:33,650 --> 00:05:37,509
Each execution of a loop is known as an iteration.
81
00:05:37,509 --> 00:05:48,120
So, in the above loop, when the input was
4, 15 minus 5 minus 1, the loop runs for three
82
00:05:48,120 --> 00:05:54,860
iterations, corresponding to the inputs 4,
15 and minus 5.
83
00:05:54,860 --> 00:05:58,660
So, for input minus 1, the loop is broken.
84
00:05:58,660 --> 00:06:00,210
So, you do not enter the loop.
85
00:06:00,210 --> 00:06:04,309
So, you do not count an iteration corresponding
to minus 1.
86
00:06:04,309 --> 00:06:10,669
So, you entered four numbers including the
minus 1 and the loop executed three times.
87
00:06:10,669 --> 00:06:14,099
So, you say that, the loop had three iterations.
88
00:06:14,099 --> 00:06:18,150
So, this is a technical term associated with
the loops.
89
00:06:18,150 --> 00:06:19,150
.
90
00:06:19,150 --> 00:06:24,210
And here is a concept that, I will introduce
to help you argue about the correctness of
91
00:06:24,210 --> 00:06:25,210
a loop.
92
00:06:25,210 --> 00:06:28,129
So, there is a notion known as a loop invariant.
93
00:06:28,129 --> 00:06:36,659
Now, a loop invariant is a property relating
values of the variable that holds at the beginning
94
00:06:36,659 --> 00:06:37,960
of each loop.
95
00:06:37,960 --> 00:06:45,580
So, thus bit abstract let me just illustrate
with the example, that we just saw.
96
00:06:45,580 --> 00:06:50,710
So, loop invariants are a good way of thinking
about the correctness of loops that, you have
97
00:06:50,710 --> 00:06:52,050
to do.
98
00:06:52,050 --> 00:06:56,460
So, in our program what will be the loop in
invariant?
99
00:06:56,460 --> 00:07:01,139
Let us look at the property of that, we are
interested in.
100
00:07:01,139 --> 00:07:05,849
There are two variables in the program, s
and a.
101
00:07:05,849 --> 00:07:09,560
And both of those variables are involved in
the loop.
102
00:07:09,560 --> 00:07:15,930
But, the interesting property that we have
relates to s.
103
00:07:15,930 --> 00:07:21,990
What is the property that, s holds with respect
to the loop.
104
00:07:21,990 --> 00:07:29,779
So, we can see that, s holds the sum of all
values read so far, except the last value
105
00:07:29,779 --> 00:07:33,369
is that true, the first time that we enter
the loop?
106
00:07:33,369 --> 00:07:36,159
Yes, because s was initialized to 0.
107
00:07:36,159 --> 00:07:37,939
And you had actually read the number.
108
00:07:37,939 --> 00:07:42,310
So, it is true that, s holds the sum of all
values, except the first one.
109
00:07:42,310 --> 00:07:45,009
So, that is true, when you first enter the
loop.
110
00:07:45,009 --> 00:07:51,580
And that, any point when you enter the loop,
you sum the last value that was the read.
111
00:07:51,580 --> 00:07:53,589
And read one more number.
112
00:07:53,589 --> 00:07:59,819
So, you will see that, s still holds the sum
of all values read so far, except the last
113
00:07:59,819 --> 00:08:00,830
one.
114
00:08:00,830 --> 00:08:05,020
So, this is the loop invariant in the program.
115
00:08:05,020 --> 00:08:12,139
And loop invariants help you, argue about
the correctness of the loops.
116
00:08:12,139 --> 00:08:20,219
So, if the loop invariant is correct and the
program maintains loop invariant, then the
117
00:08:20,219 --> 00:08:24,409
value of s when the program stops, will be
correct.
118
00:08:24,409 --> 00:08:26,050
Why is that?
119
00:08:26,050 --> 00:08:31,919
Because, the loop terminates, because the
last value read was a minus 1.
120
00:08:31,919 --> 00:08:38,729
And the invariants says that, s holds the
sum of all values, except the last value.
121
00:08:38,729 --> 00:08:45,769
So, this means that s holds the sum of all
numbers, except the minus 1.
122
00:08:45,769 --> 00:08:50,990
Therefore, when the program ends that is,
you exit out of the loop, s holds the sum
123
00:08:50,990 --> 00:08:54,019
of all number that you were supposed to add.
124
00:08:54,019 --> 00:09:00,610
So, here is how arguing about loop invariant
and seeing, whether loop invariant holds in
125
00:09:00,610 --> 00:09:04,570
the loop that you have written, helps you
argue that the program is correct.