D Programming – Arrays

One of the concepts I love is arrays. I really like putting values inside one container. Feels like cheating and magic at the same time. It can store values and even variables, it can also be one-dimensional or multi-dimensional, and it just solves many problems.

Below is the code for declaring and accessing arrays:

import std.stdio;

int main(){
	int numbers[4] = [5,1,3,10];
	int sum = 0;
	
	for(int i = 0; i<numbers.length; i++){
		sum+= numbers[i];
	}
	
	writeln("The sum is ", sum);
	
	return 0;
}

Here is the output:

 

d-arrays

 

I hope you like this post.

Don’t forget to leave a comment.

Thank you very much.

Advertisements

D – Programming (Strings)

In programming, Strings are group of characters and they are not those strands of fiber that we tie knots or tie somebody’s neck. I often introduce students to this concept by making them count the letters of their name and resite them in reverse. Quite really fun at times. 🙂

Below is a code that shows string initialization and concatenation:

import std.stdio;

int main(){
	string fname = "Mark";
	string lname = "Hay";
	
	writeln("Hello ", fname, " ", lname);
	
	return 0;
}

Here is the output:

d-strings

 

Don’t forget to leave a comment below.

Thank you very much.

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.

D Programming – Arithmetic Operations Basics

One of the most used syntax in a language are the arithmetic operations. Operations like addition, subtraction, multiplication, division, and modulus are the basics. Below is a program I created that shows the basic syntax. I first declared two variables named x and y.  Then I performed the arithmetic operations using each variables in the following lines.

The code is below:

import std.stdio;

int main(){
	int x = 5, y = 2;
	writeln("x = ", x, " and y = ", y);
	writeln("x + y = ", x + y);
	writeln("x - y = ", x - y);
	writeln("x * y = ", x * y);
	writeln("x / y = ", x / y);
	writeln("x % y = ", x % y);
	
	return 0;
}

 

Run the Program.

The output should be same as below:

 

D-arith

 

Thank you for reading this short post.

Don’t forget to leave a comment.

D – Basic Output

In this tutorial, I will be introducing you to a language called D. We will be creating a simple program that outputs “Hello World” on the screen.

First, you should download D here: http://dlang.org/download.html

 

Then, install the software. Installation is very easy because all you need to do is read and follow the directions.

 

After installation, create a new folder in My Documents named tutorials.

While inside the tutorials folder, create a new text document and rename it output.d.

Open the file with your favorite text editor (I love to use Notepad++).

Type the codes same as below:

d1

 

 

Open your command prompt.

Next, change your directory using the cd command.

type dmd then the name of your file

type the name of the exe file that was generated.

our output should be the same as below:

 

d2

 

 

Thank you for reading this post.

Don’t forget to leave a comment.