staticvoidWhileStatement(string[] args) { int i = 0; while (i < args.Length) { Console.WriteLine(args[i]); i++; } }
do 陳述式
1 2 3 4 5 6 7 8 9
staticvoidDoStatement(string[] args) { string s; do { s = Console.ReadLine(); Console.WriteLine(s); } while (!string.IsNullOrEmpty(s)); }
for 陳述式
1 2 3 4 5 6 7
staticvoidForStatement(string[] args) { for (int i = 0; i < args.Length; i++) { Console.WriteLine(args[i]); } }
foreach 陳述式
1 2 3 4 5 6 7
staticvoidForEachStatement(string[] args) { foreach (string s in args) { Console.WriteLine(s); } }
跳躍陳述式
可用來轉移控制項。
break 陳述式
1 2 3 4 5 6 7 8 9 10
staticvoidBreakStatement(string[] args) { while (true) { string s = Console.ReadLine(); if (string.IsNullOrEmpty(s)) break; Console.WriteLine(s); } }
continue 陳述式
1 2 3 4 5 6 7 8 9
staticvoidContinueStatement(string[] args) { for (int i = 0; i < args.Length; i++) { if (args[i].StartsWith("/")) continue; Console.WriteLine(args[i]); } }
goto 陳述式
1 2 3 4 5 6 7 8 9 10
staticvoidGoToStatement(string[] args) { int i = 0; goto check; loop: Console.WriteLine(args[i++]); check: if (i < args.Length) goto loop; }
return 陳述式
1 2 3 4 5 6 7 8 9
staticintAdd(int a, int b) { return a + b; } staticvoidReturnStatement(string[] args) { Console.WriteLine(Add(1, 2)); return; }
yield 陳述式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
static System.Collections.Generic.IEnumerable<int> Range(int start, int end) { for (int i = start; i < end; i++) { yieldreturn i; } yieldbreak; } staticvoidYieldStatement(string[] args) { foreach (int i inRange(-10,10)) { Console.WriteLine(i); } }