![]() |
|
Tutorial Understanding for loops - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: C, C++, & Obj-C (https://sinister.li/Forum-C-C-Obj-C) +--- Thread: Tutorial Understanding for loops (/Thread-Tutorial-Understanding-for-loops) |
Understanding for loops - phyrrus9 - 10-09-2017 Loops are one of those things that sort of get people. In C, you're told that there are a few: while do-while for foreach (this one is a trick) In reality, this isn't true. Some loops are made up of other loops, some loops don't even exist at all. Let's take a look at the basic syntax of a for loop in C: Code: for (setup; condition; post-op) statementSpoiler:
[/code] for (initialization; conditional; increment) { //code } [/code] I want to go into the reasons I chose to write it that way, in due time, but for now let's try to understand the reasons for using a for loop. You're told that you need a for loop whenever you want to repeat steps over a known portion of data, like say if you had a split array of strings: Code: [this, is, a, test]Code: void concatenateList(char *out, char * const * in, int length)
{
int index;
for (index = 0; index < length; ++index)
sprintf(out + strlen(out), "%s", in[index]);
}Let's look at another example. From time to time, programmers need an infinite loop. You may need one because thread X is waiting on data from thread Y, which will send SIGHUP to X when it's complete. In this case, you don't want thread X to die while it waits, so it can't return out. So what you would do is add a signal handler to the thread that dispatches it's CPU time whenever it gets SIGHUP. Then, at the end of the task handler you would leave an infinite loop. A lot of people would write it like this: Code: while (true) { }compares 0=0 if true, does nothing repeat You can do the same thing with a for loop: Code: for (;;);Code: jmp $Code: xor ax, ax
test ax
je -8Turns out the for loop is that one that doesn't really exist. The reason I labeled the parts the way I did is so that it would make sense when I rearranged it like this: Code: setup
while (condition)
{
statement
post-op
}So, for loops compile into while loops, that's pretty neat to know. Now you can go ahead and take off that whole increment/init restriction. For loops are just a block method shorthand, you can use them like they're specific task inline functions, as long as you use the right format. (variables defined before they're used, statements don't use conditions, etc. Let's look at one last example, just to show you what you can use them for. Finding the tail end of a linked list. Structure: Code: struct llist
{
int value;
struct llist *next;
};
struct llist *head;while loop: Code: struct llist *tail = head; //temporary
if (tail != NULL)
while (tail->next != null)
tail = tail->next;
//tail is now at end of listCode: struct llist *tail;
for (tail = head; tail != NULL && tail->next != NULL; tail = tail->next);
//tail is now at end of listAlso, keep in mind that a semicolon is a statement, so your loop doesn't actually have to have any body to it. Sorry this is such a short one, I'll go into more depth with other loops if people want, I just remembered that it was interesting and figured I'd write it. Fun fact to get people thinking: in C++, there are only 2 differences between a class and a struct, and the most important difference is that they are spelled differently. RE: Understanding for loops - Mr.Kurd - 10-09-2017 Thank you bro, Can you talk about (break and return) in loops? RE: Understanding for loops - hackedia - 10-09-2017 Loops is the one cunt that threaten me in programmings but i always ask in stack for that btw thanks
RE: Understanding for loops - davedazer - 10-09-2017 Hello, is there any C++ complete tutorial like this one? can anybody redirect me to one? RE: Understanding for loops - phyrrus9 - 10-10-2017 (10-09-2017, 06:23 PM)Mr.Kurd Wrote: Thank you bro, Can you talk about (break and return) in loops? Sure. break and continue are for loops, return is for methods so it functions the same in both cases. I'll try to remember to get a writeup out for you. RE: Understanding for loops - Slacker - 10-10-2017 Well posting here for when I become knowledgeable enough to use this. Please keep it up bro and tag me in future knowledge so I may add it to my archive |