Documente Academic
Documente Profesional
Documente Cultură
It's buggy.
what.
Was I expecting?
Yeah, I was kind of expecting if I hit y, then print yes.
But let's think about what my code is asking.
Wherein lies the bug?
Why did my program not print Y-E-S?
Yeah?
AUDIENCE: It's not capitals?
DAVID MALAN: Yeah, it's as simple as that.
It's not capitalized.
So here too, precision-- super important.
I wrote a program that says if c, if the char the user
has typed in equals capital Y, print this.
Else, if it equals capital N, print that.
I didn't do either of that.
So the program is not broken per se.
It's just missing a feature, if you will.
It's lacking support for lowercase.
But if I do do an uppercase Y, that, of course, works.
So why is this written in the way it is?
Well, there's a couple of details here.
And this may be a question you would have based on your comment earlier.
Why is it equals equals and not just equals, like in math?
Seems a little weird.
Yeah?
AUDIENCE: One equals is for assigning values [INAUDIBLE]..
DAVID MALAN: Yeah, it's the same kind of answer.
Humans already used up the equal sign for a different purpose--
for assignment, as it's called.
Move a value from the right to the left.
So when they realized, oh shoot, we kind of painted ourselves into a corner,
how do we now check for equality like in arithmetic?
Well, you need a different symbol so the computer knows the difference.
So equals equals means, unfortunately, equals.
And equals means assignments.
And that's all.
And once you kind of remember that, it's all pretty straightforward
but that's why.
There's another thing I did a little differently.
And this is an annoying detail.
Why did I suddenly switch, do you think, to single characters--
dammit.
Why, all of a sudden, did I switch to single apostrophes instead
of double quotes, like I did before?
AUDIENCE: Single quotes only work for characters.
DAVID MALAN: Why, yes.
How astute.
Yes, so that's exactly it.
Single quotes, as I just wrote, are literally
meant for when you have single characters, like y or n in this case.
Double quotes are used when you have multiple characters for proper strings.
And we'll tease apart why that is on before long.
But for now, that's literally the reason.
If you're checking one character, it's literally single quotes.
If it's more than one character, you absolutely
need double quotes for multiple characters.
But there's a way to fix this.
I could certainly just kind of cheat, be like, OK, I fixed my program.
So now I can do make noswitch.
Now I can run noswitch again.
And now I can type in y and it works.
Unfortunately, now I can't type capital Y so this is all kind of dumb.
So what would be a better fix, do you think, to the program up here?
AUDIENCE: [INAUDIBLE] both of them.
DAVID MALAN: Accept both of them somehow.
So how would you do that in Scratch, for instance?
What would you do?
What kind of puzzle piece would you try again?
AUDIENCE: Use an or.
DAVID MALAN: OK, we can use an or.
Exactly.
So there's an or puzzle piece, which you may or may not have used.
And I would like to be able to just type the word or but
computers are generally a little cryptic, although some languages--
Python-- will literally introduce the word or again.
In C, it's two vertical bars.
And you just have to remember that.
Two vertical bars allows you to say, if c equals equals capital
Y and down here, I can say, if c equals equals capital N,
now I can ask two questions at once.
And so now if I zoom out and recompile this, make noswitch, Enter.
And then go ahead and run next here dot slash noswitch.
And now I can do lower case y, I can do capital Y, I can do lowercase n,
I can do capital N. But I can't do like a question mark
because there's no support for that.
s doesn't work either.
It wasn't one character and the last was not y or n.
There's another way it could have done this.
How else could I have implemented this, especially
if I didn't even know that these two vertical bars existed?
What other puzzle piece or a block of code could I use?
AUDIENCE: [INAUDIBLE]
DAVID MALAN: Yeah, exactly.
I could just have another else if.
I could say if the character equals y, just as it was a moment ago.
But I could also do this. else if c equals equals capital Y,
then go ahead and print out quote, unquote, "yes" as well.
And then I could do the same for no.
So how do you choose between these two options?
Because this is just the first of many times
where you're going to have to make a decision as
to how to implement something.
Someone like to argue in favor or against either of these?
Little farther back?
Yeah.
AUDIENCE: [INAUDIBLE].
DAVID MALAN: Yeah, the first one used fewer lines of code
and frankly, that's a good thing.
Because the fewer lines of code, frankly the less likely
you are to have mistakes, perhaps, in your program.
Because you've written less code, fewer places to make mistakes.
And there's another argument I think in favor of the first one.
Not just fewer lines of code, but what else?
AUDIENCE: [INAUDIBLE].
DAVID MALAN: Exactly.
The second one is also redundant in so far as I'm literally saying,
printf yes twice.
And that's just kind of seems unnecessary, right?
We saw examples in Scratch where why do things multiple times
if you have a loop.
Well, same here with conditions.
Why do things multiple times if you can combine them into one?
Because plus, if you decide later on that you
want to change the output to yes exclamation point,
it could have updated it in half as many places
if there were only one block instead of two.
Now you might disagree and a reasonable person
could make the case that no, this is cleaner
because it's just super explicit now what I'm checking for.
And this is what's going to boil down in a class like this to something
called design.
There's going to be correctness, which is your code working as prescribed.
But is it well designed?
Like, would a reasonable person kind of vote in your favor
that yes, you did a good job implementing this.
The equivalent of someone evaluating an essay.
Like yes, you expressed your thoughts but they were all over the place.
And you also have a third axis of evaluation style.
You'll notice that all of my code to date has been very nicely indented
and I have comments in the files that I wrote in advance.
That's a matter of good style, which means
you have sort of pretty looking code that's
just easier to read than if you just wrote everything out onto one line.
But more on those in just a bit.
So let me go ahead and open up one alternative
and not do this one from scratch.
Let me go ahead and open up a file called switch.c,
just to introduce one other idea or one other feature.
Whoops.
OK, accidental but good takeaway.
What did I just do and why is this looking the way it is?
I misclicked.
AUDIENCE: You opened the compiled file.
DAVID MALAN: Yeah, I opened the compiled file, the program, not the source code.
So what I'm looking at is machine code.
And because my browser doesn't know it's machine code, 0's and 1's, it's
kind of misinterpreting the 0's and 1's as though they are ASCII characters.
Recall that ASCII was like capital A is 65, capital B is 66.
The IDE is trying to interpret the 0's and 1's in my programs machine code
as though it's characters.
But it's not actually English or English like syntax.
It's just random 0's and 1's in a sense.
And so that's why we're seeing the crazy characters and colors.
Because it's being misinterpreted as colors or characters
that I didn't myself type.
So no worries.
Ignore the problem and close it.
And then open up instead switch.c.
So this is not a feature that Scratch had but it's just to demonstrate--
and even in C, there's even more ways of implementing the same idea.
Let me scroll up ever so slightly.
And you'll see that up toward the top of this file, I have main as before.
I prompt the user for a char.
But this time, I'm not using if's and else's.
Notice I'm using a new keyword that we didn't have in Scratch called switch.
It takes between parentheses, a variable that you want to look at
and a variable or value on which you want
to make decisions-- on the basis of which you want to make decisions.
So you have four cases, it seems here, which
are kind of nice in that they kind of say what they are.
What does this program do when run?
Even though we've never actually looked at the switch statement before.