A Beginner's Guide to D/Conditions and Loops/Simple Looping

From Wikibooks, open books for an open world
Jump to navigation Jump to search


One of the simplest kinds of loops in D is the while loop. This loop will keep looping as long as its condition is true. This also makes the while loop very flexible—virtually any kind of loop can be written as a while loop, if you try hard enough. A while loop looks like this:

while(condition)
{
	//code goes here
}

The while loop will see if the condition is true, and if it is, it'll run the code inside it. It'll keep doing that until the condition is false.

As was shown in the last section, the foreach loop is useful for looping over the contents of some kind of container. But what about when you don't know how much data you have, such as when you're reading from a file? A while loop can help in this case.

import std.stdio;
import std.stream;

void main()
{
	char[][] names;
	File input = new File("names.txt");
	
	while(input.eof() == false)
		names ~= input.readLine();
		
	writefln("Read ", names.length, " names.");

	foreach(name; names)
		writefln(name);
}

So here we create an array of strings named names to hold a list of names we'll read in from a file. Then we create a File object. Remember in the Basic Input section, where we used din to read a line of text from the console? din is really a kind of File object, so we're doing the same thing here, except we're reading from a file on disk instead of from the user.

The important thing here is the while loop. Let's have a closer look at it:

while(input.eof() == false)
	names ~= input.readLine();

The condition is input.eof() == false. The .eof() method of File means "end of file." So we're going to use this loop to read data while we're not at the end of the file. In other words, we're going to read all the data out of the file. The body of the loop does just that—it reads a line from the file, and appends it to the end of our list of names.

At the end of our program, we write out the list of names, using the familiar foreach loop.

A second, very similar kind of loop is the do-while loop. In a while loop, the condition comes at the beginning of the loop, but in a do-while loop, the condition comes at the end. The reason this is significant is that with a while loop, if the condition is false the very first time, the loop body will never be run, so while loops can run their bodies 0 or more times. But with a do-while loop, the body is run at least once before the condition is checked. do-while loops look like this:

do
{
	code
} while(condition)

One case where this kind of loop comes up a lot is when you're trying to get valid input from the user. Usually you ask the user to enter something in, then you check it to see if it's valid, and if it's not, you keep asking them until they give you something good. This is what a do-while loop is for: it'll run the input code at least once, and keep running it until the condition is met.

import std.stdio;
import std.cstream;

void main()
{
	bool valid = false;
	bool yes;

	do
	{
		writef("Delete all your data?  y/n: ");

		char[] response = din.readLine();

		if(response.length > 0)
		{
			if(response[0] == 'y')
			{
				yes = true;
				valid = true;
			}
			else if(response[0] == 'n')
			{
				yes = false;
				valid = true;
			}
		}
	} while(valid == false)
}

This code uses the valid variable to keep track of whether what the user entered was valid or not. It'll be set to true once they do. Until then, though, the do-while loop will keep looping, asking for user input.