text
stringlengths
1
81
start
float64
0
10.1k
duration
float64
0
24.9
Now I'm, of course, going to say printf positive, backslash n.
5,442.37
4.66
And then else if-- I could do this.
5,447.03
3.66
>> I could do if i equals 0.
5,450.69
2.72
But I'd be making at least one mistake already.
5,453.41
2.43
Recall that the equal sign is not equal, as we humans know it.
5,455.84
3.64
>> But it's the assignment operator.
5,459.48
1.53
And we don't want to take 0 on the right and put it in i on the left.
5,461.01
4.63
So to avoid this confusion, or perhaps misuse of the equals sign,
5,465.64
6.17
humans decided some years ago that in many programming languages
5,471.81
2.93
when you want to check for equality between the left and the right,
5,474.74
3.26
you actually use equals equals.
5,478
1.635
So you hit the equals sign twice.
5,479.635
1.375
When you want to assign from right to left, you use a single equal sign.
5,481.01
4.59
So we could do this-- else if i equals equals zero.
5,485.6
3.76
>> I could then go and open my curly braces,
5,489.36
2.35
and say, printf 0, backslash n, done.
5,491.71
4.377
But remember how these forks in the road can work.
5,496.087
2.083
And, really, just think about the logic.
5,498.17
1.666
i is a number.
5,499.836
1.674
It's an integer, specifically.
5,501.51
1.81
And that means it's going to be less than 0, or greater than 0, or 0.
5,503.32
5.28
So there is kind of this implied default case.
5,508.6
3
>> And so we could, just like Scratch, dispense with the else if,
5,511.6
3.32
and just say else.
5,514.92
0.827
Logically, if you the programmer know there's only
5,515.747
2.083
three buckets into which a scenario can fall-- the first,
5,517.83
3.805
the second, or the third in this case-- don't
5,521.635
1.875
bother adding the additional precision and the additional logic there.
5,523.51
3.59
Just go ahead with the default case here of else.
5,527.1
2.59
>> Now, let's go ahead after saving this, make
5,529.69
2.26
conditions dot slash conditions-- not a great user interface,
5,531.95
3.81
because I'm not prompting the user, as I mentioned earlier.
5,535.76
3.154
But that's fine.
5,538.914
0.666
We'll keep it simple.
5,539.58
0.874
Let's try the number 42.
5,540.454
1.436
And that's positive.
5,541.89
1.35
Let's try the number negative 42, negative.
5,543.24
2.88
>> Let's try the value 0.
5,546.12
2.124
And, indeed, it works.
5,548.244
0.916
Now, you'll see with problems before long, testing things three times,
5,549.16
4.74
probably not sufficient.
5,553.9
1.08
You probably want to test some bigger numbers, some smaller
5,554.98
2.458
numbers, some corner cases, as we'll come to describe them.
5,557.438
3.082
>> But for now, this is a pretty simple program.
5,560.52
1.98
And I'm pretty sure, logically, that it falls into three cases.
5,562.5
2.66
And, indeed, even though we just focused on the potential downsides
5,565.16
4.2
of imprecision and overflow, in reality where many of CS50's problems,
5,569.36
4.12
we are not going to worry about, all the time,
5,573.48
2.52
those issues of overflow and imprecision, because, in fact, in C,
5,576
3.05
it's actually not all that easy to avoid those things.
5,579.05
2.839
If you want to count up bigger, and bigger, and bigger,
5,581.889
2.291
it turns out there are techniques you can use, often involving things called
5,584.18
3.33
libraries, collections of code, that other people wrote that you can use,
5,587.51
3.73
and other languages like Java and others, actually
5,591.24
2.67
make it a lot easier to count even higher.
5,593.91
1.89
So it really is some of these dangers a function of the language you use.
5,595.8
4.01
And in the coming weeks, we'll see how dangerous C really
5,599.81
2.9
can be if you don't use it properly.
5,602.71
2.24
But from there, and with Python, and JavaScript, will
5,604.95
2.66
we layer on some additional protections, and run fewer of those risks.
5,607.61
5.01
>> So let's make a little more interesting logic in our program.
5,612.62
3.2
So let me go ahead and create a program called Logical
5,615.82
3.29
just so I can play with some actual logic, logical.c.
5,619.11
4.694
I'll just copy and paste some code from earlier so I get back
5,623.804
3.066
to this nice starting point.
5,626.87
3.08
>> Let me this time do char C. I'm going to give it a name of C
5,629.95
4.03
just because it's conventional, get a character from the user.
5,633.98
4.53
And let's pretend like I'm implementing part
5,638.51
2.22
of that Rm program, the remove program before that prompted the user
5,640.73
3.4
to remove a file.
5,644.13
1.27
How could we do this?
5,645.4
1.35
>> I want to say, if C equals equals, quote unquote,
5,646.75
4.34
y, then I'm going to assume that the user has chosen yes.
5,651.09
5.214
I'm just going to print yes.
5,656.304
1.166
If it were actually writing the removal program,
5,657.47
1.97
we could remove the file with more lines of code.
5,659.44
1.98
But we'll keep it simple.
5,661.42
1.041
>> Else if c equals equals n-- and now here, I'm going to say,
5,665.95
5.3
the user must have meant no.
5,671.25
1.73
And then else, you know what?
5,672.98
1.38
I don't know what else the user is going to type.
5,674.36
1.84
So I'm just going to say that that is an error, whatever
5,676.2
2.333
he or she actually typed.
5,678.533
1.537
>> So what's going on here?
5,680.07
1.11
There is a fundamental difference versus what I've done in the past.
5,681.18
3.35
Double quotes, double quotes, double quotes, and, yet, single quotes,
5,684.53
4.77
single quotes.
5,689.3
0.87
It turns out in C, that when you want to write a string,
5,690.17
2.69
you do use double quotes, just as we've been using all this time with printf.
5,692.86
3.82
>> But if you want to deal with just a single character, a so-called char,
5,696.68
5.35
then you actually use single quotes.
5,702.03
1.75
Those of you who've programmed before, you might not have
5,703.78
1.67
had to worry about this distinction in certain languages.
5,705.45
2.4
In C, it does matter.
5,707.85
1.6
And so when I get a char and I want to compare that char using equals
5,709.45
3.11
equals to some letter like y or n, I do, indeed, need to have the single quotes.
5,712.56
5.79
>> Now, let's go ahead and do this.
5,718.35
1.42
Let's go ahead and do make logical dot slash logical.
5,719.77
6.41
And now I'm being prompted.
5,726.18
1.125
So, presumably, a better user experience would actually tell me what to do here.
5,727.305
3.333