D Programming – Loops(For Loop)

Most of the time, students get confused of the usage of looping. I always say, “Imagine a block of code that prints 10 consecutive numbers. One way of doing it is by printing them one-by-one. That would be easy if printing 10 numbers is the case but if your program needs to print 1000 numbers then that would be a very long program just for printing. The answer to that problem is looping. If you use a loop, you just define a starting point, a condition of where you are going to stop, and the process if it is increasing or decreasing. A block of 1000 codes would just take three to four lines of codes.”

 

In this program we are going to use a loop called a for loop. I used a variable x to hold the value that will be printed then I declared the starting value and ending value which are inside the range.

Below is my program.

import std.stdio;

int main(){
	
	for(int i = 0; i<11; i++){
		writeln(i);
	}
	
	return 0;
}

 

Here is my output:

d-for

 

Thank you for reading this short post. 🙂

Don’t foget to leave a comment.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s