|
|
In the first line of the program, we include the header file for the iostream library (input-output streams). This is necessary because later in the program we will want the ability to write to the screen (cout) and read input data from the keyboard (cin). In the second line, we inform the compiler that we will be using names defined in the std namespace in the program (these will be: cin, cout, endl). If we didn't do this, it would be necessary to use the longer qualified name format (std::cin, std::cout, std::endl). In C++, a program begins with the execution of the main function. In our example, this is the only function in the program. Line 3 contains the start of the main function definition, which is the beginning of our algorithm's implementation (START block (1)). In the next line, we declare the unsigned integer variable n (a natural number), which we will use shortly. In C++, variables can be declared anywhere before their use, which is why we don't immediately declare the variables i and s here.
Lines 5 through 6 implement the input/output operation (2), where the input data—the argument for the factorial function—is retrieved.
In line 7, we declare the variables i and s, simultaneously assigning them initial values (equal to 1 in both cases) (3).
The while statement is the equivalent of the selection block (4) from our algorithm. The statements contained in lines 9-10 (block 5) will be executed as long as the condition in the while statement is met, i.e., as long as i is less than or equal to n. When the constantly incrementing i becomes greater than n, we proceed to the statements after the while loop, starting at line 12, where the result is printed (6).
The statements in lines 13-14 have no bearing on the execution of the algorithm. They serve to pause the program until an arbitrary character is entered and the Enter key is pressed, which prevents the program window from closing immediately.
The curly brace } on line 15 ends the main function and simultaneously terminates the operation of the entire program (the algorithm's STOP block (7)).
