Loop statement

There are several loop statements in C++, and they are forwhile, and do...while. The for loop is usually used when we know how many iterations we want, whereas while and do...while will iterate until the desired condition is met.

Suppose we are going to generate ten random numbers between 0 to 100; the for loop is the best solution for it since we know how many numbers we need to generate. For this purpose, we can create the following code:

// For.cbp#include <iostream>#include <cstdlib>#include <ctime>using namespace std;int GenerateRandomNumber(int min, int max){    // static used for efficiency,    // so we only calculate this value once    static const double fraction =        1.0 / (static_cast<double>(RAND_MAX) + 1.0); // evenly distribute ...

Get C++ Data Structures and Algorithms now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.