tags:

views:

380

answers:

11
+1  Q: 

Loop in C help!

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?

+7  A: 

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

Ryan Brunner
A `for` loop *could* be considered a special case of a `while` loop.
FrustratedWithFormsDesigner
I would say that it most certainly is a fancy while loop.
Ben313
...And a `while` loop is nothing more than a dressed up set pair of jump/goto and conditional statements.
FrustratedWithFormsDesigner
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
+17  A: 

A for loop maybe?

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

you left out a for(;true;) loop

gbogumil
+11  A: 

A goto loop perhaps? That is pretty special.

start:
       /* do stuff */
       if ( !done ) goto start;
Christoffer
Special in an awful scary way.
GWW
@GWW sorry I can't upvote your comment more than once!
Brian Postow
+1 Yeah, The goto loop is pretty special. I can't really consider the for loop special, so this must be the correct answer. :P
Erik B
Special, I think, as in "special education" in the US.
David Thornley
I was going to offer the "goto loop" but you beat me to it..
R..
+10  A: 

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
+1 for recursion! :)
FrustratedWithFormsDesigner
+6  A: 

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
Evil. BTW, why is the `jmp_buf` declared `static`?
R..
Global variables declared static are not visible in other compilation units. I think it defaults to "int" that way.
Axel Gneiting
To limit its scope to the current translation unit scope? Otherwise an ingenious programmer might define **extern jmp_buf buf** in another file, and jump to the above loop from there. Now this would be evil...
Schedler
I meant as opposed to automatic...
R..
+2  A: 

and don't forget recursion

void doSomething(int i)
{
    if(i > 15)
        return;
    /* do stuff */
    doSomething(i + 1);
}
tenfour
A: 

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

kriss
+10  A: 

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
You're the king of special loops!
D.H.
Well, I am very good at knowing what NOT to do in production code.
Christoffer
Yeah, that's special alright.
FrustratedWithFormsDesigner
Depending on your system's signal semantics, this could simply exit (default `SIGINT` behavior) after the first round.
R..
A: 

infinite loop ?

for(;;){ }

I like this one :-)

kriss
A: 

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
I don't know if the use of blocks count though...
KennyTM
-1 for some non-C language in the answer to a question tagged C
R..
@R this is C as interpreted by Apple's versions of gcc or clang: http://en.wikipedia.org/wiki/Blocks_(C_language_extension%29
cobbal
I will, however, accept -1 for posting such a useless and hideous answer. It is C though
cobbal