CS50 x 2024 Notes C - 08

All right, so if we want then maybe tighten this up a bit, let me propose that we could do this instead.

So besides this version of the code, let me just do something more canonical, more conventional. So you're totally fine with using a variable like counter. It's what Scratch uses by default. It's very verbose. It does what it says. Frankly, once you get comfy with programming, like most typical programmers, whenever they have a single integer in a program whose sole purpose in life is to count, they'll just use " i " for integer just like I used " c " for character. When you have larger programs, you don't want to start using A and B and C and D and E and F and so forth for your variables because nothing's going to make any sense. But when you're doing something super simple like counting with an integer, using a short-named variable is totally stylistically reasonable.

But I can tighten this up further, not just renaming counter to i. What else can I do, if you recall ?

Yeah, instead of i equals i minus 1, I can actually tighten this up this way. And we didn't see the minus before, but it's the same idea - - i minus equals 1,

or even more succinctly, i minus minus. So when you get comfortable with programming, any of these approaches are correct. This would be more conventional at this point. So if you want to write code like most other people write code, adopt ultimately these kinds of conventions.

And let me go ahead and create a file called " meow ". So code meow.c. And let me do this the sort of wrong way. Let me include stio.h at the top, int main( void ) thereafter. Inside of there, let me do printf " meow ". And then you know what ?

I don't want to keep typing that. Let me just go ahead and copy/paste two more times.

So I claim this is correct, make meow, ./meow, done. I've got code that prints " meow " three times. But this again, should already rub you the wrong way. Why ? Because I have duplication. I mean, I literally copied and pasted it. And that's kind of a good rule of thumb. If you, in the future, start finding youself copying and pasting code within the same program, you're probably doing something wrong. There's a better way to design it even if it's correct. So this is clearly a candidate for a loop. So let me go ahead and actually do that. Let me just go ahead and remove all of this duplication.

Let me give myself a variable called i, set it equal to 3. Let me go ahead and give myself a while loop and check that i is greater than 0. Inside of this loop, let me print out just " meow " once. But I'll reuse that code again and again because here I'm going to do i minus minus. So that's the exact same code, the tight version of it that we saw a moment ago.

Let me go ahead and " make meow " again, ./meow, and it still works. Why is this version better ?

Because if you want the cat to meow five times, you change it in one place.

If you want to make the cat a dog, you change the meow to a woof in one place, albeit changing the file name eventually, but changing it in one place, not worrying about changing it again and again. But there are other ways to do this. For instance, let me propose that, instead of just doing it this way, just to be clear - - we can actually count in different directions. I'm kind of forcing this idea of starting at 3 going down to 0. But when normal humans in this room, if you ever count something, you probably do 1, 2, 3 and done. Like, that's how we would count in the real world. Well, we can do that, too, here code-wise.

We could initialize i to 1. We could check that i is less than or equal to 3. And we've not seen this syntax before, but there's no easy way on a typical keyboard to type a less than or equal sign like in a math book. So we use two characters, a less-than sign and then an equal sign back to back. And that means less than or equal to. And this is the same idea so long as I plus plus i inside of it, because that'll start at 1, then 2, but it won't stop then. It will go up to until i is equal to 3. Once i becomes 4, then that Boolean expression isn't going to be true. So it stops after three " meow "s total.

But there's another way, too, and this is probably the most conventional and the way you should do it, even though it's just as correct. In CS, if you've seen already last week, we almost start counting from 0. Why ? Just because, so we're not wasting a pattern of bits. So generally when you start writing code that counts, you should, quote, unquote, " almost always " start at 0, count up to but not through the total you care about. So you don't get one extra by accident. And so this would be the most conventional way of doing what we just described. But they're all correct. You can make an argument that all of them are equally good. This is what most people, quote, unquote, " would do ".

So how about doing a little bit differently versus the while loop.

So this one here. And this one, if you can believe it, is probably even more conventional than the other way. And this is going to be thematic in programming. There's rarely one way, one right way to do things. You're going to have bunches of different tools in your toolkit. And your code might look different from someone else's because each of you tends to reach for a different tool in that toolkit.

And here's another tool - - a for loop. A for loop is just another way of achieving the exact same idea using slightly different syntax. And it's appealing, frankly in general, because it's a little more succinct. It just saves some keystrokes even though you have to memorize, the order in which it works.

This code is identical to this code here functionally. But aesthetically, of course, it looks different. How does it work ? In a for loop, notice that in the parentheses is not a single simple Boolean expression. There are three things.

One, before a semicolon, is a place to initialize a variable to do your counting typically.

Second is the Boolean expression. So it's still there. It's just surrounded on the left and the right by two other things.

Lastly is the update. What do you want to do at the end of every loop through this block of code ?

So you can probably imagine where we're going with this. How does this work ? The first thing that happens is that a variable called i is defined and initialized to the value of 0. That happens once and only once. Then we check the condition. Is 0 is less than 3 ? Obviously yes. So now we don't do the plus ples yet. We go into the loop. And this is where the for loop's a little confusing at first. We print out " meow ". Then what happens ?

There's no more lines. So we go back to the for loop and we increment i at that point.

So now I is 1. Then we check the condition, i is less than 3 ? Yes, because 1 is less than 3. We go back into the loop and print " meow ".

Now we go back to the plus plus, so i is now 2. We check the condition, 2 is less than 3 obviously. So we go back into the loop and print " meow ".

Then we do the increment, i is now 3. Is 3 less than 3 ? No, so we exit the loop, and we're done, or we keep going down here if there's more code. But how many times did I say " meow " ? 1, 2, 3 total, when my hand was 0, 1 and 2. It takes some getting used to, but most people would write loops using a for loop, I would say.

If you really want to be cool and save syntax, yes, it is correct and common to eliminate the curly braces if you only have one line of code therein. We in class will always put the curly braces there because this is the kind of thing where, if you get forgetful, you go in later and add a seconf line. Like, darn it, like you forgot the curly braces, things will not work as expected. So in general, use the curly braces, but you do not have to strictly. Oh, could you do it without the condition ? Yes, there are very fancy things you can do that we won't focus on today.

But yes, if you want to get rid of the condition, you could get rid of this here. And that would actually make the loop go forever, which may be a good thing if it's like a clock that you want to tick forever, but often not a good thing in code.

相关推荐
大圣编程1 小时前
Java 多维数组详解
java·开发语言
殳翰4 小时前
下服务器端开发流程及相关工具介绍(C++)
开发语言·c++
落寞的星星4 小时前
这个对象就包含了已经转换好的DFA和各种词法分析器运转所需要的参数。下一步,我们就可以用ScannerInfo对象创建出Scanner对象,请看下面的代码:
开发语言·c#
nothing&nowhere5 小时前
用 Python 做问卷数据清洗:无效样本检测与处理实战
开发语言·python·数据清洗·数据处理·问卷星·问卷星脚本·刷问卷
2601_961593425 小时前
Rust 开发环境配置繁琐?RustRover 开箱即用搞定编码调试
开发语言·后端·macos·rust
HZZD_HZZD5 小时前
DL/T 645-2026新国标深度解读与智能电表协议适配实战:从帧结构变化到Java采集器升级的全链路改造方案
java·开发语言
多加点辣也没关系8 小时前
JavaScript|第4章:类型转换
开发语言·javascript
聪慧的水蜜桃8 小时前
【YFIOs】用C#开发硬件之设备上云
开发语言·c#
yqcoder8 小时前
httpOnly 是什么,又有什么用?
开发语言·前端·javascript
山登绝顶我为峰 3(^v^)38 小时前
C/C++ 交叉编译方法
java·c语言·c++