Java vs. C#

Loop Constructs


Java
 
while
do-while
for
 

while statement public static void main(String[] args) { int i = 0; while (i < args.length) { System.out.println(args[i]); i++; } }
do statement static void main() { int i; do { i++; } while (i != 5); }
for statement static void main(String[] args) { for (int i = 0; i < args.length; i++) System.out.println(args[i]); }
Java has for each statement since 1.5 public static void main(String[] args) { for (String arg : args) System.out.println(arg); }
break statement public static void main(String[] args) { int i = 0; while (true) { if (i == args.length) break; System.out.println(args[i++]); } }
continue statement public static void main(String[] args) { int i = 0; while (true) { System.out.println(args[i++]); if (i < args.length) continue; break; } }
class Test { public static void main(String[] args) { double[][] values = {{1.2, 2.3, 3.4, 4.5}, {5.6, 6.7, 7.8, 8.9} }; for(int i = 0; i < values.length; i++) for(int j = 0; j < values[0].length; j++) System.out.print(values[i][j]+" "); } } print: 1.2 2.3 3.4 4.5 5.6 6.7 7.8 8.9
class Test { public static void main(String[] args) { String[][] table = {{"red", "blue", "green"}, {"Monday", "Wednesday", "Friday"} }; done: for(int i = 0; i < args.length; i++) { String str = args[i]; int row, colm; for (row = 0; row <= 1; ++row) { for (colm = 0; colm <= 2; ++colm) { if (str.equals(table[row][colm])) { System.out.println("Found "+str+ " at ["+row+"]["+colm+"]"); break done;//java goto out of date } } } System.out.println(str +" not found"); } } } >java Test green Found green at [0][2] >java Test Sunday Sunday not found

C#
 
while
do-while
for
foreach 

while statement static void Main(string[] args) { int i = 0; while (i < args.Length) { Console.WriteLine(args[i]); i++; } }
do statement static void Main() { int i; do { i++; }while (i != 5); }
for statement static void Main(string[] args) { for (int i = 0; i < args.Length; i++) Console.WriteLine(args[i]); }
foreach statement//short hand for for statement static void Main(string[] args) { foreach (string s in args) Console.WriteLine(s); }
break statement static void Main(string[] args) { int i = 0; while (true) { if (i == args.Length) break; Console.WriteLine(args[i++]); } }
continue statement static void Main(string[] args) { int i = 0; while (true) { Console.WriteLine(args[i++]); if (i < args.Length) continue; break; } }
class Test { static void Main() { double[,] values = { {1.2, 2.3, 3.4, 4.5}, {5.6, 6.7, 7.8, 8.9} }; foreach (double elementValue in values) Console.Write("{0} ", elementValue); Console.WriteLine(); } } print: 1.2 2.3 3.4 4.5 5.6 6.7 7.8 8.9
class Test { static void Main(string[] args) { string[,] table = {{"red", "blue", "green"}, {"Monday", "Wednesday", "Friday"} }; foreach (string str in args) { int row, colm; for (row = 0; row <= 1; ++row) { for (colm = 0; colm <= 2; ++colm) { if (str == table[row,colm]) { goto done; } } } Console.WriteLine("{0} not found", str); continue; done: Console.WriteLine("Found {0} at [{1}][{2}]", str, row, colm); } } } >Test green Found green at [0][2] >Test Sunday Sunday not found