Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

D programming Sample program

d

import std.stdio; 
 
void main(string[] args) { 
   writeln("Hello World!"); 
}

D Programming Basic Program

d

import std.stdio; 
 
void main(string[] args) { 
   writeln("Hello World!"); 
}

D Programming Hello World Program

d

import std.stdio;

void main(string[] args) {
   writeln("Hello World!");
}

king

d

/* Hello World Program in D Programming */
void main(string[ ] args)
{
   writeln("Hello World!");
}adithya
how are you









Compile and Execute D Online

d

import std.stdio;

/* Hello World Program in D Programming */
void main(string[ ] args)
{
   writeln("Hello World!");
}

Compile and Execute D Online

d

import std.stdio;
import std.variant;

struct Vec(T, int N)
{
    this (T v)
    {
        elements = v;
    }
    this (T[] v)
    {
        elements = v[0..N];
    }
    
    @property T[] toArray()
    {
        return elements;
    }
    alias toArray this;
    
    T[N] elements;
}

void test()
{
    alias vec4f = Vec!(float, 4);
    
    assert (vec4f(5).elements[3] == 5);
    assert (vec4f([5, 6, 7, 3, 5, 8, 56]).elements[] == [5, 6, 7, 3]);
    //assert (vec4f( vec4f([1, 2, 3, 4]) ).elements[0] == 1);
}

/* Hello World Program in D Programming */
void main(string[ ] args)
{
   writeln(__LINE__);
   test();
   Vec!(float, 4) v;
}

Compile and Execute D Online

d

import std.stdio;
import std.typecons;

struct Semimap (K,V) {
    private Nullable!(V)[K] stuff;
    
    void set(int key) {
        stuff[key] = Nullable!V();
    }
    
    void set(int key, string value) {
        stuff[key] = value;
    }
    
    auto values() {
        V[] stuffValues;
        foreach(v;stuff.values)
            if(!v.isNull)
                stuffValues ~= v;
        
        return stuffValues;
    }
    
    auto byValue() {
        return stuff.values;
    }
    
    auto keys() {
        return stuff.keys;
    }
    
    auto byKey() {
        return stuff.keys;
    }
    
    auto get(int key){
        return stuff[key];
    }
    
    
    bool hasKey(int key){
        foreach(k;stuff.keys()){
            if(k == key){
                return true;
            }
        }
        return false;
    }
    
    int singCount() {
        int singleCount;
        foreach(v;stuff.values)
            if(v.isNull)
                singleCount++;
        
        return singleCount;
    }
    
    int pairCount() {
        int pairCount;
        foreach(v;stuff.values)
            if(!v.isNull)
                pairCount++;
        
        return pairCount;
    }
}

void main() {
    Semimap!(int,string) sm;
    sm.set(1,"one");
    sm.set(2);
    sm.set(3);
    writeln(sm.singCount);
    writeln(sm.pairCount);
    writeln(sm.values);
    writeln(sm.keys);
    auto val = sm.get(2);
    assert(val.isNull);
    writeln(sm.hasKey(2));
    writeln(sm.hasKey(3));
    foreach(k; sm.byKey)
        writeln("key = ",k);
    foreach(v; sm.byValue)
        if (!v.isNull)
            writeln("value = ",v);
}

D if...else if...else

d

import std.stdio;
 
int main () { 
   /* local variable definition */ 
   int a = 100; 

   /* check the boolean condition */ 
   if( a == 10 ) { 
      /* if condition is true then print the following */ 
      writefln("Value of a is 10" ); 
   } else if( a == 20 ) { 
      /* if else if condition is true */ 
      writefln("Value of a is 20" ); 
   } else if( a == 30 ) { 
      /* if else if condition is true  */ 
      writefln("Value of a is 30" ); 
   } else { 
      /* if none of the conditions is true */ 
      writefln("None of the values is matching" ); 
   } 
   writefln("Exact value of a is: %d", a ); 

   return 0; 
}

D Protected Members

d

import std.stdio;

class Box { 
   protected: 
      double width; 
} 
 
class SmallBox:Box  { // SmallBox is the derived class. 
   public: 
      double getSmallWidth() { 
         return width ; 
      }
	  
      void setSmallWidth( double wid ) {
         width = wid; 
      } 
} 
 
void main( ) { 
   SmallBox box = new SmallBox();  
   
   // set box width using member function 
   box.setSmallWidth(5.0); 
   writeln("Width of box : ", box.getSmallWidth()); 
}

D Private Members

d

import std.stdio;

class Box { 
   public: 
      double length; 

      // Member functions definitions
      double getWidth() { 
         return width ; 
      } 
      void setWidth( double wid ) { 
         width = wid; 
      }

   private: 
      double width; 
}
  
// Main function for the program 
void main( ) { 
   Box box = new Box();
   
   box.length = 10.0; 
   writeln("Length of box : ", box.length);
   
   box.setWidth(10.0);  
   writeln("Width of box : ", box.getWidth()); 
} 

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.