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.

D Programming – Input

Good Day, I was to talking to little kid this morning and he was asking me what color is the best. Every time I said a color he always answered “Really? (color)?”
What’s your favorite color? Yellow
Really? Yello?

haha, his expression made his annoying answer funny.

I adapated that kid to my program here showing basic input of string in D language.

Here is my annoying kid asking for colors program:

import std.stdio;
import std.string;

int main(){
	string input = "";
	
	write("What is your favorite color? ");
	input = strip(stdin.readln());
	
	writeln("Really? ", input, "?");
	return 0;
}

Here is the output:

d-input

 

I hope you like this post. Don’t forget to leave a comment.

Thank you very much.

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 – While Loop

Hello there, below is a program that show a basic while loop. It is very much like the syntax of C++ and Java. It is really good to remember the parts of a loop like the starting value, end statement and the operation of increment or decrement. Enjoy! 🙂

import std.stdio;

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

Here is the output:

d-while

 

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 Programming – If Statement

Most of the time, every programmer has the need to compare on or two values of variables in your program. Same as most of the programming languages, Python also has its syntax for the If Statement. In this article, I created a program that uses an if statement after the variables x, and y are declared. I assigned values 10 and 5 to variables x and y respectively.

The program is below:

import std.stdio;

int main(){
	int x = 10, y = 5;
	
	if(x > y){
		write("x is greater than y");
	}
	return 0;
}

Your output should be the same as here:
x is greater than y

Thank you for reading this post.

Don’t forget to leave a comment.
 

D Programming – Variables

Hi there, I recently enjoyed learning some programming languages which are new to me. I came across with the D Programming Language. I have already put up an article on the codes on the basic console output in D located here: https://arjunaraneta.wordpress.com/2014/09/27/d-basic-output/

You can think of variables as an address in your memory that contains data that you’ll need and use. The syntax for using variables in python in very straightforward.

Here is a basic example of declaring a variable named x with a value of 10. After declaring and assigning a value, the program will output the value of x.

import std.stdio;

int main(){
int x = 10;
writeln("x = ", x);

  return 0;
}

The output should be:

x = 10

On the next example below, I declared two variables named x and y. I assigned a value for each of the variables. After assigning the values, I created another variable named z assigned the sum of x and y. The program will output the result after the addition.

import std.stdio;

int main(){
	int x = 10, y = 5;
	writeln("x + y = ", x + y);
	
	return 0;
}

The output should be:

x + y = 10

Thank you for your time 🙂
Please leave a comment below.
Be free to ask me questions.

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.

Python – GUI (Grid Layout)

In this tutorial, I will be demonstrating Graphical User Interface
Programming using Python.

The codes are below:

 

gui2

 

 

Our output should be the same as below:

 

gui1

 

 

Explanation:

 

These lines:

Label(master, text=”Name:”).grid(row=0)
Label(master, text=”Age:”).grid(row=1)

– one way of declaring one or more labels and giving them their position in the grid

While these lines:

E1 = Entry(master)
E2 = Entry(master)

E1.grid(row=0, column=1)
E2.grid(row=1, column=1)

-another way of declaring one or more textboxes then giving their position in separate lines.

 

Thank you for reading this post.

Don’t forget to leave a comment or suggestion. 🙂