Difference between revisions of "EDT:Writing statements"

From Eclipsepedia

Jump to: navigation, search
(For)
(If)
Line 170: Line 170:
 
       highestIndex int = myNumbers.getSize();
 
       highestIndex int = myNumbers.getSize();
  
       location int = binarySearch(myNumbers, lowestIndex, highestIndex,
+
       location int = binarySearch(myNumbers, lowestIndex, highestIndex, valueOfInterest);
                        valueOfInterest);
+
 
        
 
        
 
       if (location == -1)
 
       if (location == -1)

Revision as of 16:33, 13 February 2012

EGL provides general statements such as if and while, as well as action statements for accessing data sources or invoking external logic.

Contents

General statements

The general statements are case, continue, exit, for, if, move, return, throw, try, use, and while.

Case

The case statement responds to conditions at run time by executing one set of statements rather than another:

  • You can test a criterion value. The following example invokes mySecondFunction:
function test()
   x Int = 3; 
 
   case (x)
      when (1)
         myFirstFunction();
      when (2, 3, 4)
         mySecondFunction();      
      otherwise
         myDefaultFunction();
   end   
end
  • You can test a set of logical expressions. The following example displays only "x passes":
function test()
   x Int = 3;
   y Int = 5;
   z Int = 7;
 
   case
      when (x == 3)
         SysLib.writeStdOut("x passes");
      when (y == 5)
         SysLib.writeStdOut("y passes");
      when (z == 7)
         SysLib.writeStdOut("z passes");
      otherwise
         SysLib.writeStdErr("You will not see this message.");
      end
   end 
end

As shown, no more than one clause ever executes. Control does not “fall through” from one clause to the next.


Continue

The EGL continue statement returns control to the start of a block of code controlled by a for, forEach, or while statement. The statement lets you return to a labeled statement of one of those kinds, or to the nearest embedding statement of one of those kinds.

The following example assumes that you have coded a print function named printReport.

for (i from 1 to 100 by 1)
 
   printReport(myList[i]);
 
   if ((i % 10) != 0)
      continue;
   end // if
 
   printReport(blankLine);
end // for

That code prints the members of a list, inserting a blank line between each group of ten.

Exit

The EGL exit statement exits from a function, program, service, or run unit; or from a block of code controlled by a case, for, forEach, if, or while statement.

For

The for statement runs a set of statements in a loop that repeats until a counter exceeds a specified value.

The example code shows three variations of a for statement that adds the same set of numbers.

program MyTestProgram
 
   function main()
      inputList int[] =[10, 4, 6, 8, 2];
      numberInList int = inputList.getSize();
      sum = 0;
 
      // the first for loop increments by 1.
      for(i int from 1 to numberInList)
         sum = inputList[i] + sum;
      end
 
      SysLib.writeStdOut("sum after the first for loop is " + sum);
      sum = 0;
 
      // the second for loop has the same effect but specifies the increment value. 
      for(i int from 1 to numberInList by 1)
         sum = inputList[i] + sum;
      end
 
      SysLib.writeStdOut("sum after the second for loop is " + sum);
      sum = 0;
 
      // the third for loop starts at the opposite side of the list and decrements by 1
      for(i int from numberInList to 1 decrement by 1)
         sum = inputList[i] + sum;
      end
 
      SysLib.writeStdOut("sum after the third for loop is " + sum);
   end
end

In each case, the variable i was local to the for statement. Here is an alternative:

i int;
 
for(i from 1 to numberInList)
   sum = inputList[i] + sum;
end

After that code ends, the value of i is available and equals numberInList + 1

If

The following code has If statements embedded in other If statements.  

program MyTestProgram
 
   // A binary search finds a value in a sorted (preferably short) list.
   function binarySearch(list int[], lowIndex int, highIndex int, value int) returns(int)
 
      while(true)
         middleIndex int = (lowIndex + highIndex) / 2;
 
         if(list[middleIndex] == value)
            return(middleIndex);
         else
            if(list[middleIndex] >= value)
               highIndex = middleIndex - 1;
            else
               if(list[middleIndex] < value)
                  lowIndex = middleIndex + 1;
               end
            end
         end
 
         if(highIndex < lowIndex)
            return(-1);
         end
      end
   end
 
 
   function main()
 
      myNumbers int[] =[-6, -1, 2, 4, 7, 9 ];
      valueOfInterest int = -1;
 
      lowestIndex int = 1;
      highestIndex int = myNumbers.getSize();
 
      location int = binarySearch(myNumbers, lowestIndex, highestIndex, valueOfInterest);
 
      if (location == -1)
         SysLib.writeStdOut("Did not find " + valueOfInterest + ".");
      else
         SysLib.writeStdOut("The value of interest is " + valueOfInterest +
                            ", at location " + location + ".");
      end
   end
end



Move


Return


Throw


Try


Use


While


Action statements

The action statements are add, call, close, delete, execute, forEach, get, open, prepare, and transfer.

Add

Call

Close

Delete

Execute

ForEach

Get

Open

Prepare

Replace

Transfer