Understanding the For Loop (examples in C)

preview_player
Показать описание
---

Understanding the For Loop (examples in C) // For loops seem to give beginners more trouble than while loops. So, I thought we would break them down a bit and try to take some of the mystery out of one of the most common programming constructs out there.

***

Welcome! I post videos that help you learn to program and become a more confident software developer. I cover beginner-to-advanced systems topics ranging from network programming, threads, processes, operating systems, embedded systems and others. My goal is to help you get under-the-hood and better understand how computers work and how you can use them to become stronger students and more capable professional developers.

About me: I'm a computer scientist, electrical engineer, researcher, and teacher. I specialize in embedded systems, mobile computing, sensor networks, and the Internet of Things. I teach systems and networking courses at Clemson University, where I also lead the PERSIST research lab.

More about me and what I do:

To Support the Channel:
+ like, subscribe, spread the word

Рекомендации по теме
Комментарии
Автор

15:34 What I say to myself in half of my code reviews XD

SimGunther
Автор

TNice tutorials is actually a very good and straight forward tutorial. No having ask questions or guess, no over-explaining elents, and brings

tankapdsharma
Автор

This video will help coding beginners a lot!

trustytrojan
Автор

As for the value of these videos, I'd say the gain I get is tangential as the more people that learn from you the better the industry gets. On the subject of not needing for loops, C gives us many ways to do things, it is only our desires which push us in a given direction. We could after all use labels, gotos and simple ifs. That might make for a really good video to show how looping constructs break down at a low level. Maybe even demonstrate one of my favorite ways of handling cascading errors.

anon_y_mousse
Автор

The lighting .... the audio .... the cringe .... the whole setup .... I'm amazed. Haha. Please keep going, I want to see how this will evolve.

_modiX
Автор

It's interesting to see the point of view of a beginner, especially because it's a beginner channel.

unperrier
Автор

I would say a for loop enthusiast is one that writes infinite loops this way:

for (;;) {
// code
}

ahmadhadwan
Автор

Hey Matthew, your videos are really dope, thank you.

kevinkkirimii
Автор

លោកគ្រូ your daughter is finally back on the video Last time I saw you all in Africa. she definitely looks after you for sure.

maveasna
Автор

Hi, First of all, great video great content. I have learned a lot from the video series. One thing that I want to add to the explanation is, It's perfectly right to use a while loop for everything but one difference is that my professor told me back when I was in university. If some problem requires a fixed number of iterations or you could say if you know how many times the loop is going to run then use FOR loop and if you don't know the number of iterations like you are reading from a file and the condition is read till '\n' then use While loops. I think this makes sense and it makes sometimes the implementation easy.

sarimjalil
Автор

Your merch store is down! I'm taking a c programming for electrical engineers course and wanted that shirt to wear!

kerr
Автор

Hi, Jacob. Could you please give your opinion on the "CuTest" unit testing framework? Should I use that one or criterion if I want to test my C applications? Thank you in advance

joaomane
Автор

I missed for loops iterating over lists in the video, here is the for loop I always write:

NODE* head = ...;
for(NODE* i = head; i != NULL; i = i->next) { ... }

or simply

for(NODE* i = head; i; i = i->next) { ... }

or if there would have been a '->=' operator in C:

for(NODE* i = head; i; i->=next) { ... }

cmdlp
Автор

Jacob, what are good projects to work on to practice skills in c?

jimshtepa
Автор

I'm surprised you didn't get into the topic of pre and post increment within the loop counters.

skilz
Автор

Between Producer and Signature, wNice tutorialch SKU would you recomnd? Is Signature worth the 50% price bump? ItNice tutorialnk I want to have

subhajitmoyra
Автор

I would never write this for real code for anyone but me, but a backwards iteration trick you can use as a mnemonic is the “goes to” operator -->:

for (size_t i = ARRAYSIZE; i --> 0; /* no update statement */) {
printf("%d", arr[i]);
}

(This is actually parsed as i-- > 0, but that way it’s harder to remember how to avoid off-by-one. Also note this version works with unsigned types.)

danielrhouck
Автор

You don't mention if there is a performance difference between a for and while? Is that so compiler dependent that it can't be answered generically or is there a rule of thumb? Cheers.

AlexJacksonSmith
Автор

Isn't the comma operator not defined in which order the statements will be executed?

jenselstner
Автор

I've tried that bit of code, but trying to compile that with GCC v13.1, I get a compilation error stating:

```
example.c: In function ‘main’:
example.c:15:35: error: variable-sized object may not be initialized except with an empty initializer
15 | int myvalues[ARRAYSIZE] = {1, 2, 3, 4};
| ^
```

What compiler are you using there?

_baco