An Array Example
Here is a simple program that shows how arrays can simplify things in a code.
We'll cover the following...
Example code #
Let’s revisit the challenge where we printed the twice of the value, but this time we will print twice of five different values using an array:
Press + to interact
import std.stdio;void main() {// This variable is used as a loop counterint counter;// The definition of a fixed-length array of five// elements of type doubledouble[5] values;// setting the values in a loopwriteln("values");while (counter < values.length) {values[counter]=counter*2;writeln(values[counter]);++counter;}writeln("Twice the values:");counter = 0;while (counter < values.length) {writeln(values[counter] * 2);++counter;}// The loop that calculates the fifths of the values would// be written similarly}