參考自d程序設計語言---個人博客http://my.oschina.net/u/218155/blog?fromerr=SwOkb7Sw fllow mejava
D語言的語句的語法和c語言和java基本一致,只是有些特別的東西
函數
if else while for do while 之類的同c,java一致,再也不贅述。spa
看看一些特別的.net
定義內部變量,在函數內部定義內部變量不能從新聲明能夠複製設計
int ib = 12; { //int ib = 12;//error ib = 112; }
靜態條件編譯,有點c中的相似#ifdef #endifcode
只是static if裏面的值只能是全局的,肯定值的變量orm
//static if(ib > 100){//error static if(ShowStatic.max > ShowStatic.min){ writeln("staic 1"); }else{ writeln("static 2"); }
final switch強制覆蓋全部分支blog
final switch(s1){ case ShowStatic.min: writeln("min"); break; case ShowStatic.max: writeln("max"); break; }
用with定義結構的變量get
point p; with(p){ x = 1; y = 2; }
退出時執行的scope(success) scope(faild) 他們採用棧的方式退出,下面代碼會輸出sucess2,sucess1博客
scope(success){ writeln("sucess1"); } scope(success){ writeln("sucess2"); }
import std.stdio; int a = 1; enum ShowStatic {min,max}; struct point { int x,y; } void main() { a = 2; //定義內部變量 writeln(a); { int a = 1; writeln(a); } writeln(a); int ib = 12; { //int ib = 12;//error ib = 112; } writeln(ib); if( ib > 100){ writeln("ib > 100"); }else{ writeln("ib <= 100"); } //static if(ib > 100){//error static if(ShowStatic.max > ShowStatic.min){ writeln("staic 1"); }else{ writeln("static 2"); } switch(a){ case 1: writeln("1 case"); break; case 2: writeln("2 case"); break; default: writeln("other case"); break; } enum ShowStatic s1 = ShowStatic.min; final switch(s1){ case ShowStatic.min: writeln("min"); break; case ShowStatic.max: writeln("max"); break; } auto i = 1; do{writeln(i);i++;}while(i<3); i = 1; while(i<3){writeln(i);i++;} for(i = 1;i<3;i++)writeln(i); foreach(elem;1..3)writeln(elem); int a[] = [1,2,3]; foreach(ref elem;a){ elem *= 2; } writeln(a); auto arr = ["k":1,"d":2]; foreach(string k,ref int v;arr){ writeln(k,",",v); v *= 2; } writeln(arr);; point p; with(p){ x = 1; y = 2; } try{ }catch(Exception e){ } finally{ } //mixin //mixin(import("mixin.d")); scope(success){ writeln("sucess1"); } scope(success){ writeln("sucess2"); } }