text stringlengths 1 81 | start float64 0 10.1k | duration float64 0 24.9 |
|---|---|---|
But I'm going to just blindly
say y for yes, OK, nice. | 5,730.638 | 2.392 |
>> Let's run it again, n for no, nice. | 5,733.03 | 2.75 |
Suppose like certain people I know,
my caps lock key is on all too often. | 5,735.78 | 3.83 |
So I do capital Y, enter, error. | 5,739.61 | 4.13 |
OK, it's not exactly what I'm expecting. | 5,743.74 | 2.39 |
Indeed, the computer
is doing literally what | 5,746.13 | 2.04 |
I told it to do-- check for
lowercase y and lowercase n. | 5,748.17 | 3.624 |
This doesn't feel like good
user experience, though. | 5,751.794 | 2.166 |
Let me ask for and accept
either lower case or upper case. | 5,753.96 | 5.05 |
So it turns out, you might want
to say something like in Scratch, | 5,759.01 | 3.08 |
like literally or C equals
equals capital single quoted y. | 5,762.09 | 6.06 |
Turns out, C does not have
this literal keyword or. | 5,768.15 | 3.25 |
>> But it does have two vertical bars. | 5,771.4 | 1.48 |
You have to hold Shift usually,
if you're using a US keyboard, | 5,772.88 | 2.583 |
and hit the vertical bar
key above your return key. | 5,775.463 | 3.447 |
But this vertical bar
vertical bar means or. | 5,778.91 | 3.5 |
>> If, by contrast, we wanted
to say and, like in Scratch, | 5,782.41 | 3.81 |
we could do ampersand ampersand. | 5,786.22 | 1.96 |
That makes no logical sense here,
because a human could not possibly | 5,788.18 | 3.15 |
have typed both y and lowercase y
and capital Y as the same character. | 5,791.33 | 5.78 |
So or is what we intend here. | 5,797.11 | 2.36 |
>> So if I do this in both places, or c
equals equals capital N, now rerun, | 5,799.47 | 6.81 |
make logical, rerun logical. | 5,806.28 | 3.11 |
Now, I can type y. | 5,809.39 | 1.81 |
And I can do it again with
capital Y, or capital N. | 5,811.2 | 2.72 |
And I could add in additional
combinations still. | 5,813.92 | 2.71 |
>> So this is a logical
program insofar as now | 5,816.63 | 2.18 |
I'm checking logically for
this value or this value. | 5,818.81 | 3.13 |
And I don't have to, necessarily,
come up with two more ifs or else ifs. | 5,821.94 | 4.48 |
I can actually combine some of the
related logic together in this way. | 5,826.42 | 3.54 |
So this would be better
designed than simply | 5,829.96 | 1.99 |
saying, if C equals lower case y,
print yes, else if c equals capital Y, | 5,831.95 | 5.54 |
print yes, else if c equals
lower-- in other words, | 5,837.49 | 2.584 |
you don't have to have
more and more branches. | 5,840.074 | 1.916 |
You can combine some of the equivalent
branches logically, as in this way. | 5,841.99 | 6.85 |
>> So let's take a look at just one
final ingredient, one final construct, | 5,848.84 | 5.31 |
that C allows. | 5,854.15 | 0.697 |
And we'll come back in the
future to others still. | 5,854.847 | 2.083 |
And then we'll conclude by looking
at not the correctness of code-- | 5,856.93 | 4.47 |
getting code to work-- but the design
of code, and plant those seeds early on. | 5,861.4 | 4.67 |
>> So let me go ahead and
open up a new file here. | 5,866.07 | 5.267 |
You know what? | 5,871.337 | 0.583 |
I'm going to re-implement
that same program, | 5,871.92 | 2.53 |
but using a different construct. | 5,874.45 | 1.49 |
>> So let me quickly give myself
access to include CS50.h | 5,875.94 | 4.17 |
for the CS50 library,
standard Io.h for printf. | 5,880.11 | 4.04 |
Give me my int main void. | 5,884.15 | 2.36 |
And then over here, let
me go ahead and do this. | 5,886.51 | 2.8 |
>> Char c gets get char, just like before. | 5,889.31 | 2.7 |
And I'm going to use a new construct
now-- switch, on what character? | 5,892.01 | 4.76 |
So switch is kind of like
switching a train tracks. | 5,896.77 | 3.05 |
Or, really, it is kind of
an if else, if else if, | 5,899.82 | 2.25 |
but written somewhat differently. | 5,902.07 | 1.91 |
>> A switch looks like this. | 5,903.98 | 1.51 |
You have switch, and then what
character or number you want to look at, | 5,905.49 | 3.57 |
then some curly braces like in
Scratch, just say do this stuff. | 5,909.06 | 2.94 |
And then you have different cases. | 5,912 | 1.48 |
>> You don't use if and else. | 5,913.48 | 1.35 |
You literally use the word case. | 5,914.83 | 2.22 |
And you would say something like this. | 5,917.05 | 1.74 |
>> So in the case of a lowercase y,
or in the case of a capital Y, | 5,918.79 | 5.03 |
go ahead and print out yes. | 5,923.82 | 3.53 |
And then break out of the switch. | 5,927.35 | 1.67 |
That's it. | 5,929.02 | 0.56 |
We're done. | 5,929.58 | 1.3 |
>> Else if, so to speak,
lower case n, or capital N, | 5,930.88 | 6.39 |
then go ahead and print
out no, and then break. | 5,937.27 | 5.29 |
Else-- and this kind of is the
default case indeed-- printf error-- | 5,942.56 | 5.462 |
and just for good measure, though
logically this break is not necessary | 5,948.022 | 2.958 |
because we're at the end
of the switch anyway, | 5,950.98 | 1.916 |
I'm now breaking out of the switch. | 5,952.896 | 1.624 |
So this looks a little different. | 5,954.52 | 1.76 |
>> But, logically, it's
actually equivalent. | 5,956.28 | 1.992 |
And why would you use
one over the other? | 5,958.272 | 1.708 |
Sometimes, just personal preference,
sometimes the aesthetics, | 5,959.98 | 3.24 |
if I glance at this
now, there's something | 5,963.22 | 2.2 |
to be said for the
readability of this code. | 5,965.42 | 2.09 |
I mean, never mind the fact that this
code is new to many of us in the room. | 5,967.51 | 3.18 |
>> But it just kind of is pretty. | 5,970.69 | 2.825 |
You see lowercase y, capital Y,
lower case n, capital N default, | 5,973.515 | 4.245 |
it just kind of jumps
out at you in a way | 5,977.76 | 2.39 |
that, arguably, maybe
the previous example | 5,980.15 | 2.05 |
with the ifs, and the vertical bars,
and the else ifs, might not have. | 5,982.2 | 3.58 |
So this is really a matter of personal
choice, really, or readability, | 5,985.78 | 5.82 |
of the code. | 5,991.6 | 0.76 |
>> But in terms of functionality, let me
go ahead and make a switch, dot slash | 5,992.36 | 5.87 |
switch, and now type in lowercase y,
capital Y, lowercase n, capital N, | 5,998.23 | 7.6 |
David, retry because that's
not a single character. | 6,005.83 | 3.42 |
Let's do x, error, as expected. | 6,009.25 | 2.8 |
And, logically-- and this is something
I would encourage in general-- even | 6,012.05 | 3.59 |
though we're only scratching the
surface of some of these features. | 6,015.64 | 2.15 |
>> And it might not be obvious when you
yourself sit down at the keyboard, | 6,017.79 | 2.77 |
how does this work? | 6,020.56 | 0.81 |
What would this do? | 6,021.37 | 0.87 |
The beautiful thing about having
a laptop, or desktop, or access | 6,022.24 | 3.39 |
to a computer with a compiler,
and with a code editor like this, | 6,025.63 | 3.66 |
is you can almost always answer these
questions for yourself just by trying. | 6,029.29 | 3.7 |
>> For instance, if the rhetorical
question at hand were, | 6,032.99 | 3.58 |
what happens if you forget
your break statements? | 6,036.57 | 2.97 |
Which is actually a
very common thing to do, | 6,039.54 | 1.86 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.