How to create an infinite loop in C

4 Answers

0 votes
#include <stdio.h>
#include <string.h> // strcmp

// Infinite while(1) Loop

int main() {
    char input[64] = "";

    printf("Starting infinite while loop. Type 'quit' to exit.\n");

    while (1) { // infinite loop
        printf("Enter something: ");
        scanf("%99s", input);

        if (strcmp(input, "quit") == 0) {
            printf("Exiting loop...\n");
            break;
        }

        printf("You typed: %s\n", input);
    }

    printf("Program finished.\n");
    
    return 0;
}



/*
run:

Starting infinite while loop. Type 'quit' to exit.
Enter something: a
You typed: a
Enter something: b
You typed: b
Enter something: a
You typed: a
Enter something: quit
Exiting loop...
Program finished.

*/

 



answered Apr 10 by avibootz
edited Apr 10 by avibootz
0 votes
#include <stdio.h>
#include <string.h> // strcmp

// Infinite for(;;) Loop

int main() {
    char input[64] = "";

    printf("Starting infinite for loop. Type 'stop' to break.\n");

    for (;;) { // infinite loop
        printf("Enter something: ");
        scanf("%99s", input);

        if (strcmp(input, "stop") == 0) {
            printf("Breaking out of loop...\n");
            break;
        }

        printf("You typed: %s\n", input);
    }

    printf("Program ended.\n");
    
    return 0;
}


/*
run:

Starting infinite for loop. Type 'stop' to break.
Enter something: a
You typed: a
Enter something: b
You typed: b
Enter something: a
You typed: a
Enter something: stop
Breaking out of loop...
Program ended.

*/

 



answered Apr 10 by avibootz
0 votes
#include <stdio.h>
#include <string.h> // strcmp

// Infinite do { } while(1);

int main() {
    char input[64] = "";

    printf("Starting infinite do-while loop. Type 'exit' to quit.\n");

    do { // infinite loop
        printf("Enter something: ");
        scanf("%99s", input);

        if (strcmp(input, "exit") == 0) {
            printf("Leaving loop...\n");
            break;
        }

        printf("You typed: %s\n", input);
    } while (1); // infinite loop

    printf("Done.\n");

    return 0;
}



/*
run:

Starting infinite do-while loop. Type 'exit' to quit.
Enter something: a
You typed: a
Enter something: b
You typed: b
Enter something: a
You typed: a
Enter something: exit
Leaving loop...
Done.

*/

 



answered Apr 10 by avibootz
0 votes
#include <stdio.h>
#include <string.h> // strcmp

// Infinite Loop Using goto

int main() {
    char input[64];

    printf("Starting infinite loop using goto. Type 'end' to break.\n");

loop_start:
    printf("Enter something: ");
    scanf("%99s", input);

    if (strcmp(input, "end") == 0) {
        printf("Exiting loop...\n");
        return 0;
    }

    printf("You typed: %s\n", input);
    goto loop_start;
}


/*
run:

Starting infinite loop using goto. Type 'end' to break.
Enter something: a
You typed: a
Enter something: b
You typed: b
Enter something: a
You typed: a
Enter something: end
Exiting loop...

*/

 



answered Apr 10 by avibootz
...