How to create an infinite loop in C++

4 Answers

0 votes
// Infinite while(true) Loop

#include <iostream>
#include <string>

int main() {
    std::string input;

    std::cout << "Starting infinite while loop. Type quit to exit.\n";

    while (true) { // infinite loop
        std::cout << "Enter something: ";
        std::cin >> input;

        if (input == "quit") {
            std::cout << "Exiting loop...\n";
            break;
        }

        std::cout << "You typed: " << input << "\n";
    }

    std::cout << "Program finished.\n";
}


/*
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
0 votes
// Infinite for(;;) Loop

#include <iostream>
#include <string>

int main() {
    std::string input;

    std::cout << "Starting infinite for loop. Type stop to break.\n";

    for (;;) { // infinite loop
        std::cout << "Enter something: ";
        std::cin >> input;

        if (input == "stop") {
            std::cout << "Breaking out of loop...\n";
            break;
        }

        std::cout << "You typed: " << input << "\n";
    }

    std::cout << "Program ended.\n";
}



/*
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
edited Apr 10 by avibootz
0 votes
// Infinite do { } while(true); Loop

#include <iostream>
#include <string>

int main() {
    std::string input;

    std::cout << "Starting infinite do-while loop. Type exit to quit.\n";

    do { // infinite loop
        std::cout << "Enter something: ";
        std::cin >> input;

        if (input == "exit") {
            std::cout << "Leaving loop...\n";
            break;
        }

        std::cout << "You typed: " << input << "\n";
    } while (true); // infinite loop

    std::cout << "Done.\n";
}



/*
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
// Infinite Loop Using goto 

#include <iostream>
#include <string>

int main() {
    std::string input;

    std::cout << "Starting infinite loop using goto. Type end to break.\n";

loop_start: // infinite loop
    std::cout << "Enter something: ";
    std::cin >> input;

    if (input == "end") {
        std::cout << "Exiting loop...\n";
        return 0;
    }

    std::cout << "You typed: " << input << "\n";
    goto loop_start; // infinite loop
}



/*
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
...