(FRONT) FRONT (2016)

Java control flow

Conditional Statements (if, else, else if)

Java, JavaScript, C#:


   1:      if (condition) {
   2:          ...
   3:      } else {
   4:          ...
   5:      }
   6:      else if (condition) {
   7:          ...
   8:      }
   9:  
  10:  

Looping Statements (for, while, do-while)

Java, JavaScript, C#:


   1:      for (initialization; condition; increment) {
   2:          ...
   3:      }
   4:  

   1:      while (condition) {
   2:          ...
   3:      }
   4:  

   1:      do {
   2:          ...
   3:      } while (condition);
   4:  

Enhanced For Loop (for-each in Java, foreach in C#)

Java:


   1:      for (type element : collection) {
   2:          ...
   3:      }
   4:      for (type variableName : arrayName) {
   5:         ...
   6:      }
   7:  

JavaScript:


   1:      for (variable of iterable) {
   2:          ...
   3:      }
   4:      for (key in object) {
   5:          ...
   6:      }
   7:  

C#:


   1:      foreach(type element in collection) {
   2:         ...
   3:      }
   4:  

switch, break, continue Statements

Java, JavaScript, C#:


   1:  switch (expression) {
   2:    case x:
   3:      // code block
   4:      break;
   5:    case y:
   6:      // code block
   7:      break;
   8:    default:
   9:      // code block
  10:      break;
  11:  }
  12:  

goto (Labelled Break)

C#:


   1:      goto label;
   2:  

Java, JavaScript Does not have a goto statement.

try-catch

Java, C#:


   1:      try {
   2:          ...
   3:      } catch (Exception e) {
   4:          ...
   5:      }
   6:  

Javascript


   1:      try {
   2:          ...
   3:      } catch (e) {
   4:          ...
   5:      }
   6:  

try-finally

Java, JavaScript, C#:


   1:      try {
   2:          ...
   3:      } finally {
   4:          ...
   5:      }
   6:  

switch Statement with Pattern Matching

C#


   1:  public State PerformOperation(string command) =>
   2:     command switch
   3:     {
   4:         "SystemTest" => RunDiagnostics(),
   5:         "Start" => StartSystem(),
   6:         "Stop" => StopSystem(),
   7:         "Reset" => ResetToReady(),
   8:         _ => throw new ArgumentException("Invalid string value for command", nameof(command)),
   9:     };
  10:  
  11:  

Java, JavaScript Does not have a switch expression.

try-finally

Java, JavaScript, C#: The finally block is executed after the try block and any catch blocks.


   1:  try {
   2:    // Code that might throw an exception
   3:  } finally {
   4:    // Code that will always execute, regardless of exceptions
   5:  }

Null-Coalescing Operator (??)

Java does not have a direct equivalent to the null-coalescing operator (??). You would typically use a ternary if statement or a more verbose approach to handle nulls.


JavaScript,C#:

   1:  let myVar = null;
   2:  let result = myVar ?? "Default Value"; // result will be "Default Value" if myVar is null or undefined

Null-Conditional Operator (?.)

Java does not have a direct equivalent to the null-conditional operator (?.). You would typically use a ternary if statement or a more verbose approach to handle nulls.


JavaScript, C#: Has the optional chaining operator (?.),C#: Has the null-conditional operator (?.)


   1:  let myObject = null;
   2:  let myProperty = myObject ?. myProperty; // myProperty will be undefined if myObject is null or undefined

Ternary Operator (?:)

Java, C#, JavaScript:


   1:      int x = (y > 5) ? 10 : 20; // If y > 5, x = 10; otherwise, x = 20

Labeled break and continue

Java, C#, JavaScript:


   1:  outerLoop: for (let i = 0; i < 10; i++) {
   2:    if (i == 5) {
   3:      break outerLoop;
   4:    }
   5:    console.log("i: " + i);
   6:  }

for loop with break and continue (Enhanced for loop)

Java:


   1:      int[] numbers = {1, 2, 3, 4, 5};
   2:      for (int number : numbers) {
   3:          if (number == 3) {
   4:              continue; // Skip to the next iteration
   5:          }
   6:          if (number == 5) {
   7:              break; // Break out of the loop
   8:          }
   9:      System.out.println(number);
  10:      }

JavaScript:


   1:      let numbers = [1, 2, 3, 4, 5]; 
   2:      for (let number of numbers) { 
   3:          if (number == 3) { 
   4:              continue; // Skip to the next iteration 
   5:          } 
   6:          if (number == 5) { 
   7:              break; // Break out of the loop 
   8:          } 
   9:          console.log(number); 
  10:      }

C#:


   1:      int[] numbers = {1, 2, 3, 4, 5};
   2:      foreach (int number in numbers) {
   3:          if (number == 3) {
   4:              continue; // Skip to the next iteration
   5:          }
   6:          if (number == 5) {
   7:               break; // Break out of the loop
   8:          }
   9:          Console.WriteLine(number);
  10:      }

for-in loop, for-of loop, for-await-of loop

Java, C#: No direct equivalent.


JavaScript:


   1:  const myObject = {a: 1, b: 2, c: 3};
   2:  for (const key in myObject) {
   3:    console.log(key, myObject[key]);
   4:  }
   5:  

   1:  const myArray = [1, 2, 3, 4, 5];
   2:  for (const value of myArray) {
   3:    console.log(value);
   4:  }
   5:  

   1:  async function * myAsyncGenerator() {
   2:    yield 1;
   3:    yield 2;
   4:    yield 3;
   5:  }
   6:  async function main() {
   7:    for await(const value of myAsyncGenerator()) {
   8:      console.log(value);
   9:    }
  10:  }
  11:  main();
  12:  
  13:  



Java context:



Comments ( )
Link to this page: http://www.vb-net.com/Java/Index05.htm
< THANKS ME>