0

My friends tell me that a special loop exists that isn't a 'while' loop or a 'do while' loop.

Does anyone know what it's called and the correct syntax for it's use?

James
  • 2,454
  • 1
  • 22
  • 22
  • 4
    You should consider getting a good, introductory book on C, like one of those listed in [The Definitive C Book Guide and List](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list). – James McNellis Aug 13 '10 at 13:36
  • 2
    Thanks James, I have a book on C but I read the first chapter and got bored. –  Aug 13 '10 at 13:38
  • 8
    @Hades if you got bored in the 1st chapter of a C book, what are you doing here? – Brian Postow Aug 13 '10 at 13:53
  • 11
    Yeah, we can tell, you got bored. How come do you know about safety of exceptions thrown form DLLs and don't know loops in C? – Maciej Hehl Aug 13 '10 at 13:53
  • Getting bored reading the first chapter could mean that you get bored easily or that you simply had a bad book. Since there is only 1 way to be sure, can I suggest you try some other book? – Wolph Aug 13 '10 at 14:05
  • Try another C book then. This one is pretty entertaining: http://flash-gordon.me.uk/ansi.c.txt – Christoffer Aug 13 '10 at 14:18
  • 2
    For someone with a decent bit of exposure to programming, the first chapter of ANY learn-to-program-in-XXX book is bound to be boring. – tdammers Aug 13 '10 at 14:21
  • 2
    @tdammers: I haven't thoroughly read a language tutorial in years, but I can't imagine getting caught in the situation Hades seems to be in. I look for grammar/syntax, a library reference, and any coding standards people have posted. By that point, I ought to know all the loop constructs! – Nicholas Knight Aug 13 '10 at 14:26
  • @Nicholas Knight: Yeah, I also haven't read a programming book in the order of chapters for years... quite surprised some programmers actually start at the beginning. Maybe the kind of people that also read copyright notice and ISBN code ? They are usually quite boring. – kriss Aug 13 '10 at 17:24
  • 3
    -1 I can't in good conscience vote to close this, since it really is a programming question, but per @Maciej's comment, it is a waste of time. – Ben Zotto Aug 13 '10 at 18:25

11 Answers11

16

A for loop maybe?

for (i = 0; i < 15; ++i) {
  /* do stuff */
}
tdammers
  • 20,353
  • 1
  • 39
  • 56
  • If you used 42 instead of 15, your loop would be the answer to life, this question, the universe and everything. – Tim Post Aug 13 '10 at 14:50
  • 7
    No, it would stop one step before reaching the answer. – tdammers Aug 13 '10 at 14:54
  • Why is 42 such a crucial number? :) +1 for for-loop though. That made me chuckle. –  Aug 13 '10 at 17:54
  • @AJ google calculator can help show why: http://www.google.com/search?q=the+answer+to+life+the+universe+and+everything – cobbal Aug 13 '10 at 22:53
10

A goto loop perhaps? That is pretty special.

start:
       /* do stuff */
       if ( !done ) goto start;
Christoffer
  • 12,712
  • 7
  • 37
  • 53
10

There are 3 kind of loops in c.

The for loop: http://cprogramminglanguage.net/c-for-loop-statement.aspx

for (initialization_expression;loop_condition;increment_expression){
  // statements
}

The while loop: http://cprogramminglanguage.net/c-while-loop-statement.aspx

while (expression) {
  // statements
}

The do while loop: http://cprogramminglanguage.net/c-do-while-loop-statement.aspx

do {
  // statements
} while (expression);

And you can emulate loops with a function ofcourse:

Emulating a do while loop:

void loop(int repetitions){
    // statements
    if(repetitions != 0){
        loop(repetitions - 1);
    }
}

Emulating a while loop:

void loop(int repetitions){
    if(repetitions != 0){
        // statements
        loop(repetitions - 1);
    }
}
Wolph
  • 78,177
  • 11
  • 137
  • 148
9

A signal handler loop perhaps? That is pretty special.

#include <signal.h>

void loop(int signal)
{
    if ( !done ) {
        /* do stuff */
        raise(SIGINT);
    }
}

int main() {
    signal(SIGINT, loop);
    raise(SIGINT);
    return 0;
}
Christoffer
  • 12,712
  • 7
  • 37
  • 53
7

A setjmp loop perhaps? That is pretty special.

static jmp_buf buf;

int i = 0;
if ( setjmp(buf) < end ) {
    /* do stuff */
    longjmp(buf, i++);
} 
Christoffer
  • 12,712
  • 7
  • 37
  • 53
6

There's the for loop, although I don't know how special I'd consider it.

Ryan Brunner
  • 14,723
  • 1
  • 36
  • 52
  • 1
    A `for` loop *could* be considered a special case of a `while` loop. – FrustratedWithFormsDesigner Aug 13 '10 at 13:37
  • I would say that it most certainly is a fancy while loop. – Ben313 Aug 13 '10 at 14:38
  • 1
    ...And a `while` loop is nothing more than a dressed up set pair of jump/goto and conditional statements. – FrustratedWithFormsDesigner Aug 13 '10 at 14:52
  • However, the fact that in C (and C++) is nothing than syntactic sugar for a while loop may be misleading. This is not the situation in some other languages, where the semantics of *while* and *for* loops are clearly different: the iteration count a *while* loop may be unknown before the loop is entered. Not so for a *for* loop, where the iteration count is known before entering the loop, and changing the loop counter is not allowed from within the loop. – Schedler Aug 13 '10 at 18:24
2

and don't forget recursion

void doSomething(int i)
{
    if(i > 15)
        return;
    /* do stuff */
    doSomething(i + 1);
}
tenfour
  • 36,141
  • 15
  • 83
  • 142
0

you left out a for(;true;) loop

Greg Bogumil
  • 1,903
  • 15
  • 23
0

My answer here could help you understand how C for loop works

Community
  • 1
  • 1
kriss
  • 23,497
  • 17
  • 97
  • 116
0

infinite loop ?

for(;;){ }

I like this one :-)

kriss
  • 23,497
  • 17
  • 97
  • 116
0

A Y combinator loop? That's special enough to be apple only (for now). Also special enough to leak memory all over the place

#include <stdio.h>
#include <Block.h>

typedef void * block;
typedef block (^block_fn)(block);
typedef void (^int_fn)(int);

int main(int argc, char ** argv) {
    block_fn Y = ^ block(block f) {
        return ((block_fn) ^ block(block_fn x) {
                return x(x);
            })(^ block(block_fn x) {
                    return ((block_fn)f)(Block_copy(^ block(block y) {
                                return ((block_fn)(x(x)))(y);
                            }));
                });
    };

    int_fn loop = Y(^ block(int_fn f) {
            return Block_copy(^ (int a) {
                    if (a <= 0) {
                        printf("loop done\n");
                    } else {
                        printf("loop %d\n", a);
                        f(a - 1);
                    }
                });
        });
    loop(10);

    return 0;
}
cobbal
  • 69,903
  • 20
  • 143
  • 156