Skip to content

Instantly share code, notes, and snippets.

@mshafae
Created October 4, 2022 03:34
Show Gist options
  • Save mshafae/268280c9d47f4388b5a74ad22b735cd2 to your computer and use it in GitHub Desktop.
Save mshafae/268280c9d47f4388b5a74ad22b735cd2 to your computer and use it in GitHub Desktop.
Counting to 5 with three different loops.
// Gist https://gist.github.com/268280c9d47f4388b5a74ad22b735cd2
#include <iostream>
const int kMax = 5;
int main(int argc, char const *argv[]) {
std::cout << "Counting to " << kMax << " with a for loop.\n";
for (int counter = 0; counter < kMax; counter++) {
std::cout << counter << "\n";
}
std::cout << "Counting to " << kMax << " with a while loop.\n";
int while_counter = 0;
while (while_counter < kMax) {
std::cout << while_counter << "\n";
while_counter++;
}
std::cout << "Counting to " << kMax << " with a do-while loop.\n";
int do_counter = 0;
do {
std::cout << do_counter << "\n";
do_counter++;
} while (do_counter < kMax);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment