What is a for loop in D programming?
Overview
In this shot, we’ll learn about the for loop in the D programming language. Loops are code blocks that repeat a particular logic until a condition is satisfied.
Syntax
for ( init; condition; increment ) {statement(s);}
Parameters
init: This initializes the loop counter value.condition: This evaluates eachforloop iteration. The loop ends once evaluatedfalse.increment: This increases the loop counter value.
import std.stdio;int main () {/* for loop execution */for( int a = 1; a < 20; a = a + 2 ) {writefln("Increasing by 2: %d", a);}return 0;}
Explanation
-
Line 5: We initiate the
forloop.-
The first argument in our
forloop is to declare our counter variableint i = 1. The counter variable is used to know the number of iterations made by theforloop. -
The second argument in the
forloop is a condition. We use the condition to tell theforloop when to start and stop. -
The third argument in the
forloop is the increment. It increases the value of the counter variable, enabling us to keep track of the iterations made by theforloop.
-
-
Line 6: We print the counter variable to the screen to illustrate the number of iterations made by the
forloop.