GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — C++ while - creating loops in C++ with while statement C++ whilelast modified January 9, 2023 C++ while tutorial shows how to create loops in C++ with the while statement. The while statement
The
This is the general form of the while (expression) {
statement(s);
}
The C++ while exampleThe following example uses a while statement to calculate a sum. simple.cpp
#include <iostream>
int main() {
int i = 0;
int sum = 0;
while (i <= 10) {
sum += i;
i++;
}
std::cout << sum << std::endl;
return 0;
}
We calculate the sum of 1..10 numbers. The while loop has three parts: initialization, testing and updating. Each execution of the statement is called a cycle. int i = 0;
We initiate the while (i <= 10) {
...
}
The expression inside the brackets following the i++;
This is the last, third phase of the $ ./simple 55 C++ while - calculate factorial
The factorial of a positive integer n! = n * (n-1) * (n-2) * ... * 1 This is the formula to calculate the factorial. factorial.c
#include <iostream>
int main() {
int i = 10;
int factorial = 1;
while (i > 1) {
factorial *= i;
i--;
}
std::cout << factorial << std::endl;
return 0;
}
In the example, we use the while loop to calculate the 10! factorial. $ ./factorial 3628800 C++ while - endless loop
The random.cpp
#include <random>
#include <iostream>
using u32 = uint_least32_t;
using engine = std::mt19937;
int main() {
std::random_device os_seed;
const u32 seed = os_seed();
engine generator(seed);
std::uniform_int_distribution<u32> distribute(1, 30);
while (1) {
int r = distribute(generator);
std::cout << r << " ";
if (r == 22) {
break;
}
}
std::cout << std::endl;
return 0;
}
The example calculates a random value between 0..29. If it equals to 22, the
loop is finished with the $ ./random 25 28 9 9 18 6 20 2 5 15 7 11 22 C++ while - loop over array
The loop_array.cpp
#include <iostream>
int main() {
int vals[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int i, sum = 0;
size_t len = sizeof vals / sizeof vals[0];
while (i <= len) {
sum += vals[i];
i++;
}
std::cout << "The sum is: " << sum << std::endl;
return 0;
}
In the example, we have an array of integers. We go over the array with the
$ ./loop_array The sum is: 55 C++ do while exampleThe do while statement is a specific form of a while statement, where the block is executed before the condition. So the block is always executed at least once. do_while.cpp
#include <iostream>
int main() {
int val, sum = 0;
do {
std::cout << "Enter a number: ";
std::cin >> val;
sum += val;
} while(val != 0);
std::cout << "The sum is: " << sum << std::endl;
return 0;
}
The example asks the user to repeatedly enter a number. It calculates the sum of all those values. It terminates the loop when the user enters zero. $ ./do_while Enter a number: 3 Enter a number: 2 Enter a number: 1 Enter a number: 0 The sum is: 6 This tutorial was dedicated to the while statement in C++. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |