Last active
December 22, 2015 01:08
-
-
Save bfroehle/6393876 to your computer and use it in GitHub Desktop.
Emulate Python's for-else construct in C++.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
// Emulate the for-else Python construct in C++. | |
// http://docs.python.org/2/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops | |
int main() { | |
for (int n = 2; n < 10; n++) { | |
bool success = true; | |
for (int x = 2; x < n || (success = false); x++) { | |
if (n % x == 0) { | |
std::cout << n << " equals " << x << " * " << n / x << "\n"; | |
break; | |
} | |
} | |
if (!success) { | |
// loop fell through without finding a factor | |
std::cout << n << " is a prime number\n"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment