CHAPTER 5

Logical Control

A quick references to some common logical control structures supported in Julian.

If-else branching

  int b = 0;
  if(a==1){
    b = a * 100;
  } else if(a==2){
    b = a * 100;
  } else {
    b = 0;
  }

Switch branching

  string a = "Beta";
  string s = "None";
  switch( a ){
  case "Alpha":
    s = "One";
    break;
  case "Beta":
    s = "Two";
    break;
  default:
    s = "Unknown";
  }

Switch branching supports int, char, string and Enum in case clauses. It uses break statement to break out of the switch structure. The continue statement may also be used to jump out, if the switch statement is found inside a loop structure.

  int sum = 0;
  for(int i=0; i<4; i+=1){
    switch( i ){
    case 1:
      sum += 100;
      break;
    case 2:
      continue;
    case 3:
      sum += 300;
    default:
      sum += 1;
    }
  }

While loop

  int i = 0;
  while(i<5){
    i+=1;
    if(i>3){
      break;
    }
  }

Do-while loop

  int[] arr = new int[]{100,200,300,500};
  int sum = 0;
  int i = 0;
  do{
    if(i==2){
      i+=1;
      continue;
    }
    sum += arr[i];
    i+=1;
  } while(i<4);

For loop

  int[] arr = new int[]{100,200,300,400};
  int sum = 0;
  for(int i=0; i<4; i+=1){
    sum += arr[i];
    if(sum>500){
      break;
    }
  }

Foreach loop

Certain built-in types, such as String and Array, supports foreach loop, a.k.a fast-for. A user-define type can also be used this way, as long as it implements IIterable, which is already implemented by some system classes including List and Map.

  int[] arr = new int[]{100,200,300,400};
  int sum = 0;
  for(int i : arr){
    sum += i;
  }

These control structures, with the exception of switch, do not require a body to be enclosed by curly brackets. A single statement can be used instead, although this is not recommended.

// Bubble sorting
int[] number=new int[]{95,45,15,78,84,51,24,12};
int temp=0;
for(int i=0;i<7;i++)
  for(int j=0;j<7-i;j++)
    if(number[j]>number[j+1]) {
      temp = number[j];
      number[j] = number[j+1];
      number[j+1] = temp;
    }