Chris Bourke · Transcript · · Updated
3.2 - Conditionals (2026-09-04) – Transcript
Video by Chris Bourke · Watch the original on YouTube
Summary
AI-generated from the transcript. Verify details against the source video.
- The speaker demonstrates how placing a semicolon after a conditional statement binds it to an empty statement rather than the intended print statement.
- Using static analysis tools like linters and enabling compiler flags such as wall and extra helps catch potential programming errors and warnings before execution.
- The session covers writing and transpiling a decibel level categorization program between Java and C based on user specifications.

Use this video with Pro
Reading, copying and transcript downloads are free. Create saved AI results for this video with a signed-in Pro account ($9/month).
Transcript
Search ignores accents. All transcript text stays visible.
>> All right, so today we're going to finish up on conditionals with just an exercise and some miscellaneous stuff. Uh, we left it off with this last pitfall here. Uh, what what's wrong with this code right here? Uh and uh and what will happen if we were to actually execute this?
Uh so, let me go ahead and compile it. It compiles just fine with no warnings, no errors, or anything, at least by default. But, what what do you think is going to happen? Is it going to print A as 10 or not? Yeah. It will. Why?
>> Because you put a semicolon after the statement. >> Yep. >> So, it goes uh a semicolon it means the code or that line is over. >> Exactly. So, what This is called bounding, right? That this condition here is bounded to the next executable statement. Now, if you put that into curly brackets, then it's bounded to that entire block. You don't have to do that, right? You could have written it like this. And indentation doesn't matter. It'll just go to the next line
or the next executable statement. In fact, you could put this all on the same line. And now it's bound to that execute that that executable print statement. Uh but, what we did uh is we gave it no statement, right? Uh here it is in Java, right? And I'm going to save it, and I'm going to run it again, and it's still going to print A as 10. But, if I formatted this correctly by going shift command F, you actually see what the problem is.
Because it's saying, "Oh, well, that condition is bound to the next executable statement, which is empty, and therefore it's not going to do anything. It goes down to the next executable statement, which is the actual print statement." Yeah. >> I'm surprised it didn't give you any sort of compiler error. It seems like the sort of Java would uh yell at you about.
>> Oh, uh you were surprised with Java. Yeah. Uh Well, it's because empty statements like that are actually idioms, right? That you sometimes you do use them. So, for example, uh we're going eventually going to get to a for loop here. You could have write a for loop as int I equals 0, I is less than 10, I plus plus, and then you're going to be doing something. Well, what if you want a what's called a poll loop where it just keeps going? You can go ahead
and give it empty statements like this. That's not great practice. That's not something that you should do, but languages do support that. So, empty statements are actually an idiom and used in a lot of code. So, that's why the compiler is saying, "I'm just fine with that." because some people do that.
I would not recommend it though, right? Uh so, again, the problem here is that the above will compile in both languages. It'll run in both languages. But, then since the semicolon is is there, then that means that it binds to an empty executable statement.
Right? Now, I gave a a preview of this tool. It's called a linter. Right? How can we catch these kind of things, right? You can avoid many programming errors by using your GCC. Now, this is just C as what's called a linter. Now, when I say lint, right? We already have an idea of what lint is in real life, right? You wash your clothes, they're clean, but nevertheless, you might have lint on them because when they come out of the dryer, there's little pieces of other
fabric that have collected and it's on your clothes. And so, you get rid of it using a lint brush, right? That is a little sticky brush that picks up all the little pieces of lint. Lint is not necessarily Does lint having lint on your clothes doesn't necessarily mean your clothes are dirty, right?
That's the idea. Uh your clothes are indeed clean. It's just that you probably want to take care of that before you go out or something, right? And that's what lint is in code. Uh it is something that is probably okay in code. Uh that it's syntactically correct. It'll run, but you probably want to take a second look at it and see if that's really what you mean, right?
It's called a static analysis tool. Static means that it checks code before it actually runs for these kind of conditions. There are things called dynamic analysis tools like say a debugger that once you when the when the program is actually running then then you can analyze the program. But static analysis tools are before you even compile, right? It can detect many errors that should be addressed before
you know you you actually execute the program. It could be that they are false flags, right? That they're they're false positives. And okay, no, I meant to do that, but at the very least you want to know is you know is that a problem, right?
And to do this with a GCC, all you need is the wall flag, right? This is stands for warnings, all of them. Now, you would think that that's all of them, right? It's actually not.
Even though it says it's all. You can also use W extra, right? To get Oh, wait, sorry. I'm in the wrong editor. Let me go ahead and move that over here. There we go. You can also use the W extra flag. That'll put on even more warnings, okay? In fact, what we do on the grader is we have you'll get into this starting this next week with your hack if you haven't already.
>> [snorts] >> There's always going to be a test case in there for warnings where we turn on both of these flags. Now then we go in and we shut off a couple of them that we know are you can shut off individual flags.
We do that with things that we know are not a problem for your programs. But we all we enable both of these and it's worth one point for your design on your rubric on all of your hacks. So make sure that you're addressing all of those warnings. It'll come up as a red X that you failed this test case because you have warnings there, okay?
And the warnings are sometimes descriptive, sometimes they're not. Remember that AI system, right? That I that I uh mentioned before. Uh what was it? Uh CS50 AI. Uh .ai.
Yeah, there we go. And then it'll go to CS50 chat. All right. What do those mean, right? Uh that you cannot That's a perfect question for you for AI to ask uh because the warnings are written for people who are very experienced at C, right?
They're not written necessarily for uh new folks. So, for example, what if I were to do this one with the wall flag enabled? All right, there we go. If clause does not guard. So, hopefully you see it immediately. Oh, okay, that semicolon should not be there. Uh but you can always come over here and ask a question. I'm getting
getting the warning whatever. What does that mean? This is a perfect uh you know, use of AI. All right, great question. Let's break it down. Blah blah blah, right? And then it'll give you a whole tutorial on what I just said that it's not it's not uh it's not binding it's binding to an empty executable statement, right?
Uh and again, it gives a different example here that if you have you would one condition here and you don't have those curly brackets, which you should always have uh because it's best practice, then it means that this one is not bound to the condition up here.
And then you can give it your code for a more specific explanation. Okay? Uh what about one of those other ones? It'll uh we we saw it before uh that what happens if you use this instead, right? There we go.
All right, I'm going to get another different warning. Suggest parentheses around assignment used as a truth value. And you can ask the AI, what does that mean again? Well, it means that you are probably using a assignment operator rather than an equality operator there.
What about Java? Well, Java has a linter enabled already. We all again on GradeScope we enable even more warnings. But for example, you can have say int B. All right, there we go.
And it compiles just fine. All right, there's no red there. But there is a warning here. And this warning is about an unused variable. This value of the local variable is not used. Are you sure you meant to to declare this variable because the entire program doesn't use it. Address those warnings. Get rid of that cruft. All right? Get rid of the garbage in your code. All right?
Unfortunately, again, there's no warning here at all because that's kind of expected in a programming language. That that you do have some empty empty statements like that. Okay?
Uh so, yeah, that's a nice linter tool. Some other things to avoid. Should you ever write code like this? Uh let's say uh int A is equal to 10. If A is equal to A, then print F A is A.
Who knew that? All right? Would you ever write code like that? No, why? A is equal to A. It's the the value stored in the variable is always going to be equal to itself. All right? This is what's called a tautology, something that is always true.
So, get rid of the statement. If you want to just print that A is A, then just print A is A. All right? What about the opposite? If A is not equal to A. Is that ever going to be true?