using system; class doloop { public static void main() { string mychoice; do { // print a menu console.writeline("my address book/n"); console.writeline("a - add new address"); console.writeline("d - delete address"); console.writeline("m - modify address"); console.writeline("v - view addresses"); console.writeline("q - quit/n"); console.writeline("choice (a,d,m,v,or q): ");
// retrieve the user's choice mychoice = console.readline(); // make a decision based on the user's choice switch(mychoice) { case "a": case "a": console.writeline("you wish to add an address."); break; case "d": case "d": console.writeline("you wish to delete an address."); break; case "m": case "m": console.writeline("you wish to modify an address."); break; case "v": case "v": console.writeline("you wish to view the address list."); break; case "q": case "q": console.writeline("bye."); break; default: console.writeline("{0} is not a valid choice", mychoice); }
// pause to allow the user to see the results console.write("press any key to continue..."); console.readline(); console.writeline(); } while (mychoice != "q" && mychoice != "q"); // keep going until the user wants to quit } }
说明
1.清单 4-2 演示了"do"循环的例子。 "do" 循环的格式是: do { <语句> } while (<布尔表达式>);其中的语句可以是任何合法的c#语句,布尔表达式同以前的规定一样,其返回值要么为true,要么为false。
using system; class forloop { public static void main() { for (int i=0; i < 20; i++) { if (i == 10) break; if (i % 2 == 0) continue; console.write("{0} ", i); } console.writeline(); } }