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 If Else Statement

d

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

   /* check the boolean condition */ 
   if( a < 20 ) { 
      /* if condition is true then print the following */ 
      writefln("a is less than 20" ); 
   } else { 
      /* if condition is false then print the following */
      writefln("a is not less than 20" ); 
   } 
   writefln("value of a is : %d", a); 

   return 0; 
}

D If Statement

d

import std.stdio;
 
int main () { 
   /* local variable definition */ 
   int a = 10; 
  
   /* check the boolean condition using if statement */ 
   if( a < 20 ) { 
      /* if condition is true then print the following */ 
      writefln("a is less than 20" ); 
   } 
   writefln("value of a is : %d", a); 
  
   return 0;
}

D Continue Statement

d

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

   /* do loop execution */
   do {
      if( a == 15) {
         /* skip the iteration */
         a = a + 1;
         continue;
      }
      writefln("value of a: %d", a);
      a++;
     
   } while( a < 20 );
 
   return 0;
}

D Break Statement

d

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

   /* while loop execution */
   while( a < 20 ) {
      writefln("value of a: %d", a);
      a++;
      
      if( a > 15) {
         /* terminate the loop using break statement */
         break;
      }
   }
 
   return 0;
}

D Nested Loop

d

import std.stdio;
 
int main () {
   /* local variable definition */
   int i, j;
   
   for(i = 2; i<100; i++) {
      for(j = 2; j <= (i/j); j++)
        if(!(i%j)) break; // if factor found, not prime
      if(j > (i/j)) writefln("%d is prime", i);
   }
 
   return 0;
}

D Dowhile Loop

d

import std.stdio;

int main () {
   /* local variable definition */
   int a = 10;

   /* do loop execution */
   do{
      writefln("value of a: %d", a);
      a = a + 1;
   }while( a < 20 );
 
   return 0;
}

D For Loop

d

import std.stdio;

int main () {
   /* for loop execution */
   for( int a = 10; a < 20; a = a + 1 ) {
      writefln("value of a: %d", a);
   }

   return 0;
}

D While Loop

d

import std.stdio;

int main () { 
   /* local variable definition */ 
   int a = 10;  
   
   /* while loop execution */ 
   while( a < 20 ) {
      writefln("value of a: %d", a); 
      a++; 
   }
  
   return 0; 
}

D Operator Precedence

d

import std.stdio;

int main(string[] args) { 
   int a = 20; 
   int b = 10; 
   int c = 15; 
   int d = 5; 
   int e;
   
   e = (a + b) * c / d;      // ( 30 * 15 ) / 5 
   writefln("Value of (a + b) * c / d is : %d\n",  e ); 
   
   e = ((a + b) * c) / d;    // (30 * 15 ) / 5 
   writefln("Value of ((a + b) * c) / d is  : %d\n" ,  e );  
   
   e = (a + b) * (c / d);   // (30) * (15/5) 
   writefln("Value of (a + b) * (c / d) is  : %d\n",  e );
   
   e = a + (b * c) / d;     //  20 + (150/5) 
   writefln("Value of a + (b * c) / d is  : %d\n" ,  e ); 
  
   return 0;
}

D SizeOf Operator

d

import std.stdio;

int main(string[] args) { 
   int a = 4; 
   short b; 
   double c; 
   int* ptr;

   /* example of sizeof operator */ 
   writefln("Line 1 - Size of variable a = %d\n", a.sizeof ); 
   writefln("Line 2 - Size of variable b = %d\n", b.sizeof ); 
   writefln("Line 3 - Size of variable c= %d\n", c.sizeof );  
  
   /* example of & and * operators */ 
   ptr = &a; /* 'ptr' now contains the address of 'a'*/ 
   writefln("value of a is  %d\n", a); 
   writefln("*ptr is %d.\n", *ptr);  
   
   /* example of ternary operator */ 
   a = 10; 
   b = (a == 1) ? 20: 30; 
   writefln( "Value of b is %d\n", b ); 
   
   b = (a == 10) ? 20: 30; 
   writefln( "Value of b is %d\n", b ); 
   return 0; 
} 

Advertisements
Loading...

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