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 Assignment Operator

d

import std.stdio;

int main(string[] args) {
   int a = 21;
   int c ;

   c =  a; 
   writefln("Line 1 - =  Operator Example, Value of c = %d\n", c );  
   
   c +=  a; 
   writefln("Line 2 - += Operator Example, Value of c = %d\n", c );
   
   c -=  a; 
   writefln("Line 3 - -= Operator Example, Value of c = %d\n", c );
   
   c *=  a; 
   writefln("Line 4 - *= Operator Example, Value of c = %d\n", c ); 
   
   c /=  a; 
   writefln("Line 5 - /= Operator Example, Value of c = %d\n", c );  
   
   c  = 200; 
   c = c % a; 
   writefln("Line 6 - %s= Operator Example, Value of c = %d\n",'\x25', c );
   
   c <<=  2; 
   writefln("Line 7 - <<= Operator Example, Value of c = %d\n", c ); 
   
   c >>=  2; 
   writefln("Line 8 - >>= Operator Example, Value of c = %d\n", c );
   
   c &=  2; 
   writefln("Line 9 - &= Operator Example, Value of c = %d\n", c ); 
   
   c ^=  2; 
   writefln("Line 10 - ^= Operator Example, Value of c = %d\n", c ); 
   
   c |=  2; 
   writefln("Line 11 - |= Operator Example, Value of c = %d\n", c );
   
   return 0; 
}

D Bitwise Operator

d

import std.stdio;

int main(string[] args) {  
   uint a = 60; /* 60 = 0011 1100 */   
   uint b = 13; /* 13 = 0000 1101 */ 
   int c = 0;  
   
   c = a & b;       /* 12 = 0000 1100 */  
   writefln("Line 1 - Value of c is %d\n", c ); 
   
   c = a | b;       /* 61 = 0011 1101 */ 
   writefln("Line 2 - Value of c is %d\n", c );
   
   c = a ^ b;       /* 49 = 0011 0001 */ 
   writefln("Line 3 - Value of c is %d\n", c ); 
   
   c = ~a;          /*-61 = 1100 0011 */ 
   writefln("Line 4 - Value of c is %d\n", c );  
   
   c = a << 2;     /* 240 = 1111 0000 */ 
   writefln("Line 5 - Value of c is %d\n", c );
   
   c = a >> 2;     /* 15 = 0000 1111 */ 
   writefln("Line 6 - Value of c is %d\n", c );
   
   return 0; 
} 

D Logical Operator

d

import std.stdio;

int main(string[] args) {
   int a = 5;
   int b = 20;
   int c ;

   if ( a && b ) {
      writefln("Line 1 - Condition is true\n" );
   }
   if ( a || b ) {
      writefln("Line 2 - Condition is true\n" );
   }
   /* lets change the value of a and b */

   a = 0; 
   b = 10; 

   if ( a && b ) { 
      writefln("Line 3 - Condition is true\n" ); 
   } else { 
      writefln("Line 3 - Condition is not true\n" ); 
   } 
   
   if ( !(a && b) ) { 
      writefln("Line 4 - Condition is true\n" ); 
   } 
   return 0;
}

D Relational Operator

d

import std.stdio;
  
int main(string[] args) { 
   int a = 21; 
   int b = 10; 
   int c ;  
   
   if( a == b ) { 
      writefln("Line 1 - a is equal to b\n" ); 
   } else { 
      writefln("Line 1 - a is not equal to b\n" );
   } 
   
   if ( a < b ) { 
      writefln("Line 2 - a is less than b\n" ); 
   } else { 
      writefln("Line 2 - a is not less than b\n" ); 
   } 
   
   if ( a > b ) { 
      writefln("Line 3 - a is greater than b\n" ); 
   } else { 
      writefln("Line 3 - a is not greater than b\n" ); 
   } 
   
   /* Lets change value of a and b */ 
   a = 5; 
   b = 20; 
   
   if ( a <= b ) { 
      writefln("Line 4 - a is either less than or equal to b\n" ); 
   } 
   if ( b >= a ) { 
      writefln("Line 5 - b is either greater than or equal to b\n" ); 
   } 
   return 0; 
}

D Arithmetic Operator

d

import std.stdio; 
 
int main(string[] args) { 
   int a = 21; 
   int b = 10; 
   int c ;  
   
   c = a + b; 
   writefln("Line 1 - Value of c is %d\n", c ); 
   c = a - b; 
   writefln("Line 2 - Value of c is %d\n", c ); 
   c = a * b; 
   writefln("Line 3 - Value of c is %d\n", c ); 
   c = a / b; 
   writefln("Line 4 - Value of c is %d\n", c ); 
   c = a % b; 
   writefln("Line 5 - Value of c is %d\n", c ); 
   c = a++; 
   writefln("Line 6 - Value of c is %d\n", c ); 
   c = a--; 
   writefln("Line 7 - Value of c is %d\n", c ); 
   char[] buf; 
   stdin.readln(buf); 
   return 0; 
}

D Escape Sequence

d

import std.stdio;
  
int main(string[] args) { 
   writefln("Hello\tWorld%c\n",'\x21'); 
   writefln("Have a good day%c",'\x21'); 
   return 0; 
}

D More Features

d

import std.stdio;
  
enum { 
   A = 1.2f,  // A is 1.2f of type float 
   B,         // B is 2.2f of type float 
   int C = 3, // C is 3 of type int 
   D          // D is 4 of type int 
}
  
int main(string[] args) { 
   writefln("A : %f", A); 
   writefln("B : %f", B); 
   writefln("C : %d", C); 
   writefln("D : %d", D);  
   return 0; 
}

D Base Type Syntax

d

import std.stdio;
  
enum : string { 
   A = "hello", 
   B = "world", 
} 
  
int main(string[] args) { 
   writefln("A : %s", A); 
   writefln("B : %s", B); 
   
   return 0; 
}

D Anonymous Enum

d

import std.stdio; 
 
// Initialized sun with value 1 
enum { sun , mon, tue, wed, thu, fri, sat }; 
 
int main(string[] args) { 
   writefln("Sunday : %d", sun); 
   writefln("Monday : %d", mon); 
   return 0; 
}

D Named Enum

d

import std.stdio;

// Initialized sun with value 1 
enum Days { sun = 1, mon, tue, wed, thu, fri, sat };

int main(string[] args) { 
   writefln("Min : %d", Days.min); 
   writefln("Max : %d", Days.max);
   writefln("Size of: %d", Days.sizeof); 
   return 0; 
}

Advertisements
Loading...

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