Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "EDT:Writing basic logic"
(New page: == Basic EGL statements == Assign x to y. <source lang="java"> y = x; </source> If-then statement. <source lang="java"> if ( x > 4 ) x = y; end </source> If-then-else statement. <s...) |
|||
Line 1: | Line 1: | ||
== Basic EGL statements == | == Basic EGL statements == | ||
− | Assign x to y | + | EGL's syntax is similar to C, Java, and other languages influenced by ALGOL. |
+ | |||
+ | Assign x to y | ||
<source lang="java"> | <source lang="java"> | ||
y = x; | y = x; | ||
</source> | </source> | ||
− | If-then statement | + | If-then statement |
<source lang="java"> | <source lang="java"> | ||
if ( x > 4 ) | if ( x > 4 ) | ||
Line 13: | Line 15: | ||
</source> | </source> | ||
− | If-then-else statement | + | If-then-else statement |
<source lang="java"> | <source lang="java"> | ||
if ( x > 4 ) | if ( x > 4 ) | ||
Line 22: | Line 24: | ||
</source> | </source> | ||
− | While loop | + | While loop |
<source lang="java"> | <source lang="java"> | ||
while ( x > 0 ) | while ( x > 0 ) | ||
Line 30: | Line 32: | ||
</source> | </source> | ||
− | Return from a function | + | Call a function |
+ | <source lang="java"> | ||
+ | doSomething(); | ||
+ | </source> | ||
+ | |||
+ | Call a function, passing in parameters, and assign the result to a variable | ||
+ | <source lang="java"> | ||
+ | z = doSomethingElse( 1, x, "hello", true ); | ||
+ | </source> | ||
+ | |||
+ | Return from a function | ||
<source lang="java"> | <source lang="java"> | ||
return; | return; | ||
</source> | </source> | ||
− | Return a value from a function | + | Return a value from a function |
<source lang="java"> | <source lang="java"> | ||
return ( x ); | return ( x ); | ||
+ | </source> | ||
+ | |||
+ | Exception handling with the try-onException statement | ||
+ | <source lang="java"> | ||
+ | try | ||
+ | addToAccount( 100 ); | ||
+ | deductFromAccount( 100 ); | ||
+ | onException ( ex AnyException ) | ||
+ | SysLib.writeStderr( "Oh noes! " :: ex.message ); | ||
+ | end | ||
+ | </source> | ||
+ | |||
+ | Handling more than one kind of exception | ||
+ | <source lang="java"> | ||
+ | try | ||
+ | addToAccount( 100 ); | ||
+ | deductFromAccount( 100 ); | ||
+ | onException ( ex1 AddException ) | ||
+ | SysLib.writeStderr( ex1.message ); | ||
+ | onException ( ex2 DeductException ) | ||
+ | SysLib.writeStderr( ex2.message ); | ||
+ | onException ( ex3 UserException ) | ||
+ | SysLib.writeStderr( ex3.message ); | ||
+ | end | ||
</source> | </source> | ||
[[Category:EDT]] | [[Category:EDT]] |
Revision as of 16:00, 10 February 2012
Basic EGL statements
EGL's syntax is similar to C, Java, and other languages influenced by ALGOL.
Assign x to y
y = x;
If-then statement
if ( x > 4 ) x = y; end
If-then-else statement
if ( x > 4 ) x = y; else x = 10; end
While loop
while ( x > 0 ) counter += 1; x -= 1; end
Call a function
doSomething();
Call a function, passing in parameters, and assign the result to a variable
z = doSomethingElse( 1, x, "hello", true );
Return from a function
return;
Return a value from a function
return ( x );
Exception handling with the try-onException statement
try addToAccount( 100 ); deductFromAccount( 100 ); onException ( ex AnyException ) SysLib.writeStderr( "Oh noes! " :: ex.message ); end
Handling more than one kind of exception
try addToAccount( 100 ); deductFromAccount( 100 ); onException ( ex1 AddException ) SysLib.writeStderr( ex1.message ); onException ( ex2 DeductException ) SysLib.writeStderr( ex2.message ); onException ( ex3 UserException ) SysLib.writeStderr( ex3.message ); end