Continue Statement
/* * This program makes you say "cheese curls" three times. * Its a peice of software nearly as important as Microsoft Windows. */ #include <iostream> #include <string> int main() { std::cout << "You must say cheese curls on the next three lines" << std::endl; // Repeated ask the user to say cheese until s/he does. int count = 0; while(count < 3 && std::cin.good()) { // Instruct the user. std::cout << "Attempt " << count+1 << ": Please say \"cheese curls\": "; // Read the first word. std::string cheese; std::cin >> cheese; if(cheese != "cheese") { // Complain, discard the rest of the line, and start // the line over. std::cout << "NO!!! You start with \"cheese\"! " << "Please follow instructions." << std::endl; std::cin.ignore(1024, '\n'); continue; } // Now we need to find curls. std::string curls; std::cin >> curls; if(curls != "curls") { // Complain, discard the rest of the line, and start // the line over. std::cout << "Oh, c'mon! You are supposed to say " << "\"curls\" now. You will just have to " << "start the line over." << std::endl; std::cin.ignore(1024, '\n'); continue; } // Count this successful line ++count; } // Final message of approval or complaint if(std::cin.good()) { std::cout << "Thank you for saying \"cheese curls\" three " <<"times. " << std::endl; } else { std::cout << "YOU! You closed the console instead of " << "typing your lines!" << std::endl <<"We'll get you next time." << std::endl; } }

The continue statement goes to the next iteration of a loop. In a while, it jumps to the test. In a for, it goes to the increment.

We will see some serious uses of it later, but it is best used when an error is detected, and we want to abandon this iteration and just go to the next. This is most common in input read loops.