Copyright HébergementWebs.com - License GPL

Euphoria - Quick Guide

Euphoria tutorial   2020-11-21 01:31:39

Euphoria - Quick Guide Euphoria - Overview Euphoria means E nd- U ser P programming with H bjets O ierarchiques for R obust I nterpreted A pplications. The first incarnation of Euphoria was created by Robert Craig on an Atari Mega-ST and it was first released in 1993. It is now maintained by Rapid Deployment Software. This is a free, simple, flexible, easy to learn and interpreted but extremely fast 32 bit high level programming language for DOS, Windows, Linux, FreeBSD and more. Euphoria is used to develop Windows GUI programs, high speed DOS games, and Windows Linux / FreeBSD X programs. Euphoria can also be used for CGI (web) programming. Euphoria Features Here is the listof the main Euphoria features - This is a simple, flexible and powerful language definition that is easy to learn and use. It supports dynamic storage allocation, which means that variables grow or shrink without the programmer having to worry about allocating and freeing Memory. It takes care of the garbage collector automatically. It "s extremely faster than conventional interpreters such as Perl and Python. Euphoria programs run on Linux, FreeBSD, 32-bit Windows and any DOS environment. Euphoria programs are not subject to any 640 KB memory limit. It provides a Euphoria-To translator -C optimizer that you can use to translate your Euphoria program into C, then compile it with a C compiler to get an executable (.exe) file. This can increase the speed of your program by 2 to 5.me. The underlying hardware is completely hidden, which means programs are unaware of word length, the underlying representation of values ​​at the level of the bit, byte order, etc. Euphoria installation comes complete with full screen source debugger, runtime profiler and full multi-file editor screen. It supports runtime error handling, indexing and type checking. This is a completely free open source language. Platform Requirements Euphoria is available on Windows, Linux, FreeBSD and OSX. Here is the minimum version required with the following platforms - WIN32 version - You need Windows 95 or any later version of Windows. It works well on XP and Vista. Li versionnux - You need a reasonably up-to-date Linux distribution, which has libc6 or later. For example, Red Hat 5.2 or later works fine. FreeBSD Version - You need a reasonably up-to-date FreeBSD distribution. Mac OS X version - You need any reasonably up-to-date Intel Mac. Limitations of Euphoria Here are some important limitations of Euphoria - Although Euphoria is simple, fast and flexible enough for programmers; it does not provide call support for many important features. For example, network programming. Euphoria was invented in 1993, and you still won"t find any books written about this language. There is also not much documentation available for the language. But these days the language is getting tVery quickly popular and you can hope to have some good utilities and books available for the language very soon. Euphoria Licensing This product is free and open source, and has benefited from contributions from many people. You have all the royalty-free rights to distribute any Euphoria programs that you develop. Icon files, such as euphoria.ico and binaries available in euphoria bin, can be distributed with or without your modifications. You can hide or link your program and distribute the resulting files royalty free. Some 3 additional rd parts lega Some restrictions may apply when using the Euphoria-To-C translator. The generous Open Source License allows Euphoria to use for personal and commercial purposesales. Unlike many other open source licenses, your changes don"t have to be open source. Euphoria - Environment This chapter describes the installation of Euphoria on different platforms. You can follow the steps to install Euphoria on Linux, FreeBSD, and 32-bit Windows. You can therefore choose the steps according to your working environment. Linux, free BSD installation The official website provides the .tar.gz file on which to install Euphoria your Linux or BSD operating system . You can download your latest version of Euphoria from its official website - Download Euphoria . Once you have the .tar.gz file, here are three easy steps to install Euphoria on your free Linux or BSD machine - Step 1 - Installation of filesers Unzip the downloaded file euphoria-4.0b2.tar.gz into a directory where you want to install Euphoria. If you want to install it in the / home directory as follows, then - $ cp euphoria-4.0b2.tar.gz / home $ cd / home $ gunzip euphoria- 4.0b2.tar.gz $ tar -xvf euphoria-4.0b2.tar This creates a directory hierarchy in the /home/euphoria-4.0b2 directory as follows - $ ls -l -rw-r - r-- 1 1001 1001 2485 Aug 17 06:15 Jamfile -rw-r - r-- 1 1001 1001 5172 Aug 20 12:37 Jamrules -rw-r - r-- 1 1001 1001 1185 Aug 13 06:21 License.txt drwxr-xr-x 2 1001 1001 4096 Aug 31 10:07 bin drwxr-xr-x 7 1001 1001 4096 Aug 31 10:07 demo -rw-r - r-- 1,1001 1001366 March 18 9:02 am file_id.diz drwxr-xr-x 4 1001 1001 4096 Aug 31 10:07 include -rw-r - r-- 1,1001 1001 1161 March 18 09:02 installu.doc drwxr-xr-x 4 1001 1001 4096 Aug 31 10:07 s ource drwxr-xr-x 19 1001 1001 4096 7Sep 12:09 drwxr-xr-x tests 2 1001 1001 4096 Aug 31 10:07 tutorial NOTE - Euphoria file name -4.0b2.tar.gz depends on the latest version available. We are using version 4.0b2 of the language for this tutorial. Step 2 - Configure Path After installing Euphoria, you need to set paths correctly so that your shell can find the required Euphoria binaries and utilities. Before proceeding, you need to configure the following three important environment variables: Set the PATH environment variable to the point / home / euphoria-4.0b2 / bin. Set the EUDIR environment variable to point to /home/euphoria-4.0b2. Set the EUINC environment variable to point to /home/euphoria-4.0b2/include. These variables can be set as follows - $ export PATH = $ PATH: /home/euphoria-4.0b2/bin $ export EUDIR = / home / euphoria-4.0b2 $ export EUINC = / home / euphoria-4.0b2 / include NOTE - The above commands used to set environment variables may differ depending on your shell. We used the bash shell to run these commands to set the variables. Step 3 - Confirming the installation Confirm whether you have installed Euphoria successfully or not. Run the following command - $ eui -version If you get the following result, then that means you have successfully installed Euphoria; otherwise you have to go back and check all the steps again. $ eui -version Euphoria Interpreter 4.0.0 beta 2 (r2670) for Linux using system memory $ Here it is, Euphoria Programming Environment is ready on your UNIX machine,and you can start writing complex programs with just a few simple steps. WIN32 and DOS installation Official The website provides an .exe file to install Euphoria on your WIN32 or DOS operating system. You can download your latest version of E Support from its official website - Download Euphoria . Once you have the .exe file, here are three easy steps to install the Euphoria programming language on your WIN32 or DOS machine - Step 1 - Install files Double click on the downloaded .exe installer to install all files. We downloaded the euphoria-40b2.exe file for installation. The name of the euphoria-40b2.exe file depends on the latest version available. We are using version 4 beta 2 of the language. By default Euphoria would be installed in the directory C: euphoria-40b2 but you can also select the desired location. Step 2 - Reboot the machine Reboot your machine to complete the installation. Step 3 - Confirmation of installation Confirm whether you have installed Euphoria successfully or not. Run the following command - c:> eui -version If you get the following result, it means that you have successfully installed Euphoria; otherwise you have to go back and check all the steps again. c:> eui -version Euphoria Interpreter 4.0.0 beta 2 (r2670) for Windows with Managed Memory c:> There you go, Euphoria Programming Environment is ready on your WIN32 machine, and you can start writing complex programs in a few simple steps. InterpretersEuphoria Depending on the platform you are using, Euphoria has several interpreters - The main interpreter is eui . On Windows platforms, you have two choices. If you run eui , a console window is created. If you run euiw , no console is created, which makes it suitable for GUI applications. Euphoria doesn"t care about the choice of file extensions. By convention, however; console-based applications come with the .ex extension. GUI based applications have the extension .exw and include files have the extension .e . Euphoria - Basic Syntax The Euphoria language has many similarities to Perl, C and Java. However, there are definite differences between the languages. This chapter is designed for you familiarize quickly with the syntax expected in Euphoria. This tutorial assumes you are working with Linux and all examples were written on the Linux platform. But we observe that there is no noticeable difference in the syntax of the program under Linux and WIN32. So you can follow the same steps on WIN32. First Euphoria program Let"s write a simple Euphoria program in a. Type the following source code into the test.ex file and save it. #! / Home / euphoria-4.0b2 / bin / eui met (1, "Hello, Euphoria! N ") Let"s say the Euphoria interpreter is available in the / home / directory euphoria-4.0b2 / bin / . Now run this program as follows - $ chmod + x test.ex # This is to make the file executable $ ./test.ex This produces the following result - Hello Euphoria! This uses a built-in met () function that takes two arguments. The first argument indicates the file name or device number, and the second argument indicates a string that you want to print. Here 1 indicates the STDOUT device. Euphoria identifiers A Euphoria identifier is a name used to identify a variable, function, class, module, or other object. An identifier begins with a letter A through Z or a through z, then followed by letters, numbers or underscores. Euphoria does not allow punctuation characters such as @, $ and% in identifiers. Euphoria is a case sensitive programming language. Thus, Manpower and manpower are two different identifiers in Euphoria. For example, valid identifiers are - n color26 ShellSort quick_sort a_very_long_indentifier Reserved words The following listshows the reserved words in Euphoria. These reserved words cannot be used as constants or variables or any other identifying name. Euphoria keywords contain lowercase letters only. and exit override like export procedure pause fallthru public by to retry case function return constant global routine continue goto switch do if then else ifdef to elsedef include type elsif label until elsifdef loop while end namespace with entry not without enum or xor Expressions Euphoria allows you to calculate results by forming expressions. However, in Euphoria you can perform calculations on entire sequences of data with a single expression. You can handle a sequence as you would a single number. It can be copied, passed to a subroutine, or calculated as a unit. For example - {1,2,3} + 5 This is an expression that adds the sequence {1, 2, 3} and atom 5 to obtain the resulting sequence {6, 7, 8}. You will learn the sequences in the following chapters. Code blocks One of the first caveats programmers come across when learning Euphoria is thecauses there to be no braces to indicate code blocks for procedure and function definitions or flow control. Code blocks are designated by associated keywords. The following example shows if ... then ... end if block - if condition then code block ends here if Multiline instructions Instructions in Euphoria usually end with a new line. Euphoria, however, allows you to write a single instruction over several lines. For example - total = item_one + item_two + item_three Escape characters Escape characters can be entered using a back-sabrer. For example - The following table is a listof escape or nonprinting characters that can be represented with backslash notation. Backslash notation Deion nNouvelle r Transport return t Tab Slash reverse Double quote Quotation mark simple Comments in Euphoria All comments are ignored by the compiler and have no effect on execution speed. It is advisable to use more comments in your program to make it more readable. There are three forms of comment text - Comments start with two dashes and extend to at the end of the current line. The multiline format comment is kept inside /*...*/, even if it happens on a di linefferent. You can use a special comment starting with the two character sequence “#!" only on the first line of the program. Examples #! / home / euphoria-4.0b2 / bin / eui - First comment puts (1, "Hello, Euphoria! n ") - second comment / * This is a comment that This produces the following output - Hello Euphoria! Note - You can use a special comment starting with “#! ". This informs the Linux shell that your file should be executed by the Euphoria Interpreter. Euphoria - Variables Variables are nothing more than memory locations reserved for storing values. This means that when you create a variable, you are reserving space in memory. Depending on the data type of a variable, the interpreter allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different types of data to variables, you can store integers, decimals, or characters in those variables. Euphoria data types are explained in different chapters. These memory locations are called variables because their value can be changed during their lifetime. Variable declaration Euphoria variables must be explicitly declared to reserve memory space. Thus, the declaration of a variable is mandatory before assigning a value to a variable. Variable declarations have a type name followed by a listof declared variables. For example - integer x, y, z sequence a, b, x When you declare a variable, you name the variable andyou define what sort of values ​​can legally be assigned to the variable while your program is running. Simply declaring a variable does not assign it any value. If you try to read it before giving it a value, Euphoria will throw a runtime error because the "variable xyz has never been given a value " . Assignment Values ​​ The equal sign (=) is used to assign values ​​to variables. The variable can be assigned as follows - Variable_Name = Variable_Value For example - #! / Home / euphoria / bin / eui - Here is the declaration of the variables. counter integer integer miles sequence name counter = 100 - An integer assignment miles = 1000,0 - A floating point name = "John " - A string (sequence) printf (1, "Counter value% dn ", counter) printf (1, " Value of miles% fn ", miles) printf (1, " Vatheir name% sn ", {name}) Here, 100, 1000,0 and " John "are the values ​​assigned to the counter variables , miles and name , respectively. This program produces the following output - Counter value 100 Miles value 1000.000000 nom Jean To avoid forgetting to initialize a variable, and also because this can make the code easier to read, you can combine declaration and assignment - integer n = 5 This is equivalent to the following - integer nn = 5 Identifier scope The scope of an identifier is a deion of what the code can access it. Code in the same scope of an identifier can access this identifier and code that is not in the same scope as the identifier cannot access it. The scope of a variable depends on location andthe way it is declared. If declared in a for, while, or switch loop, its scope starts at the declaration and ends at the respective end statement. In an if statement, the scope starts at the statement and ends either at the next else, elsif statement or end if . If a variable is declared in a routine, the scope of the variable begins at the declaration and ends at the end statement of the routine. This is known as a private variable. If a variable is declared outside of a routine, its scope starts at the declaration and ends and at the end of the file in which it is declared. This is known as a module variable. The scope of a constant that has no scope modifier, starts at declaration and ends and ends at filein which it is declared. The scope of an enumeration which has no scope modifier, begins at declaration and ends and at the end of the file in which it is declared. The scope of all procedures, functions and types , which have no scope modifier, starts at the start of the source file and ends at the end of the source file in which they are declared. Constants, enumerations, module variables, procedures, functions and types, which do not have a scope modifier, are called locals . However, these identifiers can have a scope modifier preceding their declaration, which causes their scope to extend beyond the file in which they are declared. If the global keyword precedes the declaration, the scope of these identifiers extends to whole application. They are accessible par code anywhere in the application files. If the public keyword precedes the declaration, the scope extends to any file that explicitly includes the file in which the identifier is declared, or to any file that includes a file which in turn public includes the file containing the public declaration. If the export keyword precedes the declaration, the scope only extends to any file that directly includes the file in which the identifier is declared. When you include a Euphoria file in another file, only identifiers declared using a scope modifier are accessible to the file making include . The other declarations in the included file are invisible for the file doing include. Euphoria - Constants Constants are also variables that receive an initial value thiscan never change in the life of the program. Euphoria allows you to define constants using the constant keyword as follows - constant MAX = 100 constant Upper = MAX - 10, Lower = 5 constant name_list= { "Fred ", "George ", "Larry "} Th The result of any expression can be assigned to a constant, even one involving calls to functions previously defined, but once the assignment is made, the value of the constant variable is "locked ". Constants cannot be declared in a subroutine. The scope of a constant which has no scope modifier, begins at the declaration and ends and at the end of the file in which it is declared. Examples #! / home / euphoria-4.0b2 / bin / eui constant MAX = 100 constant Upper = MAX - 10, Lower = 5 printf (1, "Value of MAX% d n ", MAX) printf(1, "Value of% d upper n ", upper) printf (1, "Value of% d lower n ", lower) MAX = MAX + 1 printf (1, "Value of MAX% dn ", MAX) This produces the following error - ./test. ex: 10 :: cannot change the value of a constant MAX = MAX + 1 ^ Press Enter If you delete the last two lines of the example then this produces the following result - MAX value 100 Upper value 90 Value lower 5 The enums A value enumeration is a special type of constant where the first default is the number 1 and each element after that is incremented ted by 1. Enumerations can only take numeric values. Enumerations cannot be declared in a subroutine. The scope of an enumeration that has no scope modifier, begins at the declarationand ends and at the end of the file in which it is declared. Examples #! / home / euphoria-4.0b2 / bin / eui enum ONE, TWO, THREE, FOUR printf (1, "Value of ONE% dn ", ONE) printf (1, "Value of TWO% dn ", TWO ) printf (1, "Value of THREE% dn ", THREE) printf (1, "Value of FOUR% dn ", FOUR) This will produce the following output - Value of ONE 1 Value of TWO 2 Value of THREE 3 Value of FOUR 4 You can change the value of an element by assigning it a numeric value . Subsequent values ​​are always the previous value plus one, unless they are also assigned a default value. #! / Home / euphoria-4.0b2 / bin / eui enum ONE, TWO, THREE, ABC = 10, XYZ printf (1, "Value of ONE% dn ", ONE) printf (1, "Value of TWO% dn ", TWO) printf (1, " Value of THREE% dn ", THREE)printf (1, "Value of ABC% dn ", ABC) printf (1, "Value of XYZ% dn ", XYZ) This produces the following output - Value of ONE 1 Value of TWO 2 Value of THREE 3 Value of ABC 10 Value of XYZ 11 Sequences use integer indices, but with enum you can write code like this - enum X, Y sequence point = {0,0} point [X] = 3 point [Y] = 4 Euphoria - Data types The data stored in memory can be of several types. For example, a person"s age is stored as a numeric value, and their address is stored as alphanumeric characters. Euphoria has standard types which are used to define the operations possible on them and the storage method for each of them. Euphoria has the following four standard data types - integer atom sequence object Understanding atoms and sequences is key to understanding Euphoria. Integers Euphoria integer data types store numeric values. They are declared and defined as follows - integer var1, var2 var1 = 1 var2 = 100 Variables declared with type integer must be atoms with integer values ​​between -1073741824 and +1073741823 inclusive. You can perform exact calculations on larger integer values, up to about 15 decimal digits, but declare them as an atom, rather than an integer. Atoms All data objects in Euphoria are either atoms or sequences. An atom is a single numeric value. Atoms can have any integer or double-precision floating point that goes read. Euphoria"s atoms are declared and defined as follows - atom var1, var2, var3 var1 = 1000 var2 = 198.6121324234 var3 = "E " Atoms can range from approximately -1e300 to + 1e300 with 15 decimal digits of precision . An individual character is an atom which must be entered using single quotes. For example, all of the following statements are legal - - Next is equivalent to atom 66 - the ASCII code for B char = "B " - Next is equivalent to the sequence {66} sentence = "B " Sequences A sequence is a collection of numeric values ​​accessible through their index. All data objects in Euphoria are either atoms or sequences. The sequence index starts at 1 unlike the other programming languages ​​where the array index starts at 0. Euphoria sequences are declared and defined as follows - sequence var1, var2, var3, var4 var1 = {2,3, 5, 7, 11, 13, 17, 19} var2 = {1, 2, {3, 3, 3}, 4, {5, {6}}} var3 = {{ "zara ", "ali "}, 52389, 97.25} var4 = {} - the sequence of 0 elements A string character is just a sequence of characters that can be entered with quotes doubles. For example, all of the following statements are legal - word = "word " phrase = "ABCDEFG " Strings can be handled and operated like all other sequences. For example, the above string is fully equivalent to the sequence - sentence = {65, 66, 67, 68, 69, 70, 71} You will learn more about the sequence in Euphoria - Sequences . Objects This is a super data type in Euphoria that can take any value including atoms, sequences or integers . Euphoria objects are declared and defined as follows - object var1, var2, var3 var1 = {2, 3, 5, 7, 11, 13, 17, 19} var2 = 100 var3 = "E " An object can have any of the following values ​​- a sequence an atom an integer an integer used as the file number a sequence of strings or a single character atom Euphoria - Operators Euphoria provides a rich set of operators for manipulate variables. We can Arithmetic operators Relational operators Logical operators Assignment operators Operators arithmetic Arithmetic operators are used in mathematical expressions of the same way they are used in Algebra. The tableThe following u lists the arithmetic operators. Suppose the entire variable A contains 10 and variable B contains 20, then - Show examples Operator Deion Example + Addition - Adds values ​​of each next to operator A + B will give 30 - Subtraction - Subtracts the right operand from the left operand A - B will give -10 * Multiplication - Multiplies the values ​​on either side of operator A * B will give 200 / Division - Divide the left operand by l "right operand B / A will give 2 + More unary - This has no impact on the value of the variable. + B gives 20 - Minus unary - This creates a negative value of the given variable. -B gives -20 Le Re Relational operators The following relational operators are supported by the Euphoria language. Suppose variable A is 10 and variable B 20 then - Show examples Operator Deion Example = Check if the value of two operands is equal or not , if so then the condition becomes true. (A = B) is not true. ! = Checks if the value of two operands is equal or not, if the values ​​are not equal then the condition becomes true. (A! = B) is true. > Checks if the value of the left operand is greater than the valueeur of the right operand, if so, the condition becomes true. (p> B) is not true. < Checks if the value of the left operand is less than the value of the right operand, if so , the condition becomes true. (A >= Checks if the value of the left operand is greater than or equal to the value of the right operand, if so then the condition becomes true. (p> = B) is not true. <= Checks if the value of the left operand is less than or equal to the value of the right operand , if so then the condition becomes true. (A <= B) is true. The logical operators The following table lists the logical operators. Suppose Boolean variables A contains 1 and variable B contains 0, then - Show examples Operator Deion Example and Logical AND operator called. If both operands are nonzero, the condition becomes true. (A and B) is false. or Logical OR operator called. If one of the two operands is different from zero, then the condition becomes true. (A or B) is true. xor Logical XOR operation called or. The condition is true if either of them is true, if both operands are true or false then the condition becomes false. (A xor B) is true. not Called logical NOT operator that cancels the result. Using this operator, true becomes false and false becomes true no (B) is true. You can also apply these operators to numbers other than 1 or 0. The convention is: zero means false and different from zero means true . The assignment operators There are the following assignment operators supported by Euphoria language - Show examples Operator Deion Example = Simple assignment operator, assigns values ​​of the right operands to the left operand C = A + B will assign the value of A + B to C + = Add l "AND assignment operator, It adds the right operand to the left operand and assigns the result to the left operand C + = A is equivalent to C = C + A - = Subtract AND assignment operator, It subtr acts the right operand from the left operand and assigns the result to the "opande left C - = A is equivalent to C = C - A * = Multiply operator d AND assignment, it multiplies the right operand by the left operand and assigns the result to the left operand C * = A is equivalent to C = C * A / = Assignment operator for C / = A is equivalent to C = C / A & = Concatenation operator C & = {2 } is the same as C = {C} & {2} Note - The equals symbol "= " used in a d statement assignment is not an operator, it is only part of the syntax. Operators Ther There are a few other operators supported by Euphoria Language. The "& " operator Two objects can be concatenated using the "&" operator. The result is a sequence of onelength equal to the sum of the lengths of the concatenated objects. For example - #! / Home / euphoria-4.0b2 / bin / eui sequence a, b, ca = {1, 2, 3} b = {4} c = {1, 2, 3} & {4} printf (1, "Value of c [1]% dn ", c [1]) printf (1, " Value of c [2]% dn ", c [2]) printf (1, " Value of c [3]% dn ", c [3]) printf (1, " Value of c [4]% dn ", c [4]) This produces the following output - Value of c [1] 1 Value of c [2] 2 Value of c [3] 3 Value of c [4] 4 Priority of Euphoria operators The precedence of operators determines the grouping of terms in an expression. This affects the way an expression is evaluated. Some operators have a higher priority than others; for example, the multiplication operator has higher priority than the addition operator. For example, x = 7 + 3 * 2 Here, x is assigned to 13, and non to 20 because the * operator has a higher priority than +. Therefore, it starts with 3 * 2 first and then adds to 7. Here the operators with the highest priority appear at the top of the table , those with the lowest appear at the bottom. In an expression, operators with higher precedence are evaluated first. Category Operator Associativity Postfix function calls / type Unary + -! pas Right to left Multiplicative * / Left to right Addendum + - From left to right Concatenation & From left to right Relational >> = << = Left to right Equality =! = Left to right logical AND and Left to right > logical OR or Left to right Logical XOR xor Left to right Comma , Left to right Euphoria - Branching Branching is the most important aspect of any programming language. While writing your program, you may come across a situation where you need to make a decision or you need to select an option from the many options offered. The following diagram shows a simple scenario where a program must take one of two paths depending on the given condition. Euphoria provides three types of decision-making statements (branching or conditional) - if statement switch statement ifdef statement Let"s see the declarations in detail - Euphoria - Types of loops The loop is yet another most important aspect of any programming language. While writing your program, you may come across a situation where you have to execute the same instruction multiple times and sometimes an infinite number of times. There are several ways to specify the duration of the process, and how to stop or modify it. An iterative block can be informally called a loop, and each execution of code in a loop is called an iteration of the loop. The following diagram shows a simple logic flow of a loop - Euphoria offers three types of loop statements - while declaration loop up to statement > for declaration All declarations below above offer you a flexibiity and ease of use based on different situations. Let"s see them in detail one by one - Euphoria - Flow control The program execution flow refers to the order in which the program instructions are executed. By default, the instructions are executed one after the other. However; more than once the work order must be changed from the default order for the task to be completed. Euphoria has a number of flow control statements that you can use to organize the order of execution of the statements. The instruction exit Exiting a loop is done with the keyword exit . This causes the stream to immediately exit the current loop and start over with the first instruction after the loop ends. Syntax The syntax of an exit statement is as follows - exit [ "> The exit statement ends the last innermost loop until "a the name or number of the label is specified. A special form of exit N is exit 0 . This leaves all levels looping, regardless of depth. Monitoring continues after the outermost loop block. Likewise, exit -1 exits the second outermost loop, and so on. Example #! / Home / euphoria-4.0b2 / bin / eui integer b for a = 1 to 16 do printf (1, "value of a% dn ", a) if a = 10 then b = end of output if end for printf (1, "value of b% dn ", b) This produces the following result - value of a value 1 of a value 2 of a 3 value of a value 4 of a value 5 of a value 6 of a value 7 of a value 8 of a value 9 of a value of 10 of b 10 L break The break statement works just like the exit statement, but applies to if statements or switch statements rather than looping statements of any kind. Syntax The syntax of the break statement is as follows - break [ "Label Name " ] [Number] The break statement ends the last if or switch block until an optional label name or number is specified. A special form of break N is break 0 . This leaves the outermost switching block regardless of the depth. Checking continues after the outermost block. Likewise, break -1 breaks the second if or switch block, and so on. Example #! / Home / euphoria-4.0b2 / bin / eui integer a, b sequence s = { "E ", "u ", "p "} if s [1] = "E " athen a = 3 if s [2] = "u " then b = 1 if s [3] = "p " then break 0 - leave the highest if the block ends if a = 2 otherwise b = 4 end if otherwise a = 0 b = 0 end if printf (1, "value of a% dn ", a) printf (1, "value of b% dn ", b) This produces the following result - value of a value 3 of b 1 The continue The continue statement continues the execution of the loop to which it applies by moving to the next iteration and skipping the remainder of an iteration. Going to the next iteration means testing a condition variable index and checking if it is still within range. Syntax The syntax of the continue statement is as follows - continue [ "Labe l Name "] [Number] The continue statement would iterate through the last innermost loop until a name or nuoptional label merit is specified. A special form of continue N is continue 0 . This reiterates the outermost loop, regardless of the depth. Likewise, continue -1 starts from the second outermost loop, and so on. Example #! / Home / euphoria-4.0b2 / bin / eui for a = 3 to 6 do printf (1, "value of a% dn ", a) if a = 4 then put (1, "(2) n ") continue if printf (1, " value of a% dn ", a * a) end for This would produce the following result: value of a value 3 of a value 9 of a value 4 (2) of a value 5 of a value 25 of a value 6 of a value of 36 The retry The retry statement continues execution of the loop to which it applies by going to the next iteration and skipping the remainder of an iteration. Syntax The syntax of the retry statement is as follows- retry [ "> The retry instruction tries to Perform the current iteration of the loop to which it applies. The statement branches out to the first statement in the designated loop without testing or incrementing the index of the for loop. A special form of retry N is retry 0 . This attempts to perform the outermost loop, regardless of the depth. Likewise, retry -1 sounds the second outermost loop, and so on. Normally, a subblock that contains a retry statement also contains another flow control keyword such as exit, continue, or break. Otherwise, the iteration would run endlessly. Example #! / Home / euphoria-4.0b2 / bin / eui integer errors = 0 integer files_to_open = 10 for i = 1 to length (files_to_open) do fh = open (files_to_open [i], "rb ") if fh = -1 then if p> 5 then exit else errors + = 1 retry end if end if file_handles [i] = fh end for Since retry don"t change the value of i and try to open the same file again, there must be some way to break the loop, which the exit statement provides. The goto instruction The goto instruction asks the computer to resume code execution at a labeled location. The place to resume execution is called the target of the statement. It is limited to being in the current routine, or in the current file if it is outside any routine. Sy ntax The syntax of the goto statement is as follows - goto "Label Name " The target of a goto statement can be any accessible label statement - label "Label Name " The label names must beent be double between constant strings. Characters that are not allowed in Euphoria IDs may appear in a tag name because it is a regular string. Example #! / Home / euphoria -4.0b2 / bin / eui integer a = 0 label "FIRST " printf (1, "value of a% dn ", a) a + = 10 if a This produces the following result - value of a value 0 of a value of 10 of a value of 20 of a value of 30 of a 40 End value of a 50 Euphoria - Short circuit evaluation When a condition is tested by si, elsif, until , or en using the operators and or or , a short circuit evaluation is used. For example - if has 0 then - the code block endsmine si If a if a 0 then - code block ends if if a In general, whenever you have a condition of the following form - A and B Where A and B can be any two expressions, Euphoria takes a shortcut when A is false and immediately returns the overall result false, without even looking at expression B. Likewise, whenever you have a condition of the following form - A or B Where A is true, Euphoria ignores the evaluation of expression B, and declares that the result is true. The short-circuit evaluation ofand and or takes place only for the if, elsif, until and while conditions. It is not used in other contexts. For example - x = 1 or {1,2,3,4,5} - x must be set to {1,1,1,1,1} If a short circuit was used here, you would set x to 1, and not even look at {1,2,3,4,5}, which would be wrong. Thus, shorting can be used in if, elsif, until, or while conditions, because you only need to care if the result is true or false, and conditions are needed to produce a atom accordingly. > Euphoria - Sequences A sequence is represented by a listof objects in curly braces {}, separated by commas. A sequence can contain both atoms and other sequences. For example - {2, 3, 5, 7, 11, 13, 17, 19} {1, 2, {3, 3, 3}, 4, {5, {6}}} {{ "Zara ", "Ayan "}, 52389, 97.25} {} - the sequence with 0 elements Only one element of a sequence can be selected by giving the element number in square brackets. Element numbers start at 1. For example, if x contains {5, 7.2, 9, 0.5, 13} then x [2] is 7.2. Suppose x [2] contains {11,22,33}, now if you ask for x [2] you get {11.22.33} and if you ask for x [2] [3], you get atom 33. Example #! / home / euphoria-4.0b2 / bin / eui sequence xx = {1, 2, 3, 4} for a = 1 to length (x) do printf (1, "value of x [% d] =% dn ", {a, x [a]}) end for Here, length () is the built-in function that returns the length of the sequence. The example above produces the following result - value of x [1] = 1 value of x [2] = 2 value of x [3] = 3 value of x [4] = 4 String A string is just a sequence of characters. It can be entered from one of the twox ways - (a) Using double quotes - "ABCDEF G " ​​ (b) Using raw string notation - - Using back quotes` ABCDEFG` or - Using three double quotes " "ABCDEFG " " You can try the following example to understand the concept - #! / home / euphoria-4.0b2 / bin / eui sequence xx = "ABCD " for a = 1 to length (x) do printf (1, "value of x [% d] =% sn ", {a , x [a]}) end for This produces the following result - value of x [1] = A value of x [2 ] = B-value of x [3] = C-value of x [4] = D Arrays of strings An array of strings can be implemented using sequences as follows - #! / home / euphoria-4.0b2 / bin / eui sequence x = { "Hello ", " World ", " Euphoria ", " ", " Last One "} for a = 1 to length (x) do printf (1, " value of x [% d] =% sn ", {a, x [a]}) end for This produces the following result - value of x [1 ] = Hello value of x [2] = World value of x [3] = Euphoria value of x [4] = value of x [5] = Last One Euphoria Structures A structure can be implemented using sequences as follows - #! / Home / euphoria-4.0b2 / bin / eui sequence employee = {{ "John ", " Smith "}, 45000, 27, 185.5} printf (1, " First name =% s, Last name =% sn ", {employee [1] [1], employee [1] [2]} ) This produces the following result - First Name = John, Last Name = Smith There are different operations that can be performed directly on the sequences. Let"s see them in detail - Urinary operation When applied to a sequenceThat is, a unary operator is actually applied to each element of the sequence to give a sequence of results of the same length. #! / home / euphoria-4.0b2 / bin / eui sequence xx = - {1, 2, 3, 4} for a = 1 to length (x) do printf (1, "value of x [% d] =% dn ", {a, x [a]}) end for This produces the following result - value of x [1] = - 1 value of x [2] = -2 value of x [3] = -3 value of x [4] = -4 Arithmetic operations Almost all arithmetic operations can be performed on sequences as follows - #! / home / euphoria-4.0b2 / bin / eui sequence x, y, a, b, cx = {1, 2, 3} y = {10, 20, 30} a = x + y met (1, "Value of a = { ") for i = 1 to length (a) do printf (1, "% d, ", a [i]) end for met (1, "} n ") b = x - y put (1, "Value of b = {") for i = 1 at length (a) do printf (1, "% d, ", b [i]) end for put (1, "} n ") c = x * 3 met (1, "Value of c = {") for i = 1 to length (c) do printf (1, "% d, ", c [i]) end for put (1, "} n ") This produces the following result - Value of a = {11,22,33,} Value of b = {-9, -18, -27,} Value of c = {3,6,9,} Line options command A user can pass command line options to a Euphoria and it is accessed as a sequence using the command_line () function as follows - #! / home / euphoria-4.0b2 / bin / eui sequence xx = command_line () printf (1, "Name of interpreter:% sn ", {x [1]}) printf (1, "Name of :% sn ", {x [2]}) printf (1, " First argument:% sn ", {x [3]}) printf (1, " Second argument:% sn ", {x [4]}) Here printf () is Euphoria "s built-in function. Now if you run this as follows - $ eui test.ex " one " two " This produces the following result - interprets it: /home/euphoria-4.0b2/bin/eui Name of: test.ex First argument: one second Argument: two Euphoria - Date and time Euphoria has a library routine that returns the date and time to your program. The date () method The date () method returns a sequence value made up of eight atom elements. The followi ng example explains it in detail - #! / home / euphoria-4.0b2 / bin / eui integer curr_year, curr_day, curr_day_of_year, curr_hour, curr_minute, curr_second sequence system_date, word_week, word_month, notation, curr_day_of_week, curr_month word_week = { "Sunday ", "Monday ", "Wednesday ", "Tuesday ", "Thursday ", "Friday ", "Saturday "} word_month = { "January ", "February ", "March ", "April ", "May " , "Juln ", " July ", " August ", " September ", " October ", " November ", " December "} - Get the current system date. system_date = date () - Now take the individual items curr_year = system_date [1] + 1900 curr_month = word_month [system_date [2]] curr_day = system_date [3] curr_hour = system_date [4] curr_minute = system_date [5] curr_second = system_date [6] curr_day_of_week = word_week [system_date [7]] curr_day_of_year = system_date [8] if curr_p> = 12 then notation = "pm " else notation = "am " end if if curr_p> 12 then curr_hour = curr_hour - 12 end if if curr_hour = 0 then curr_hour = 12 end if met (1, "nHello Euphoria! nn ") printf (1, "Today is% s,% s% d,% d. n ", {curr_day_of_week, curr_month, curr_day, curr_year}) printf (1, " Time is% .2d:%. 2d:%. 2d% sn ", {curr_hour, curr_minute, curr_second, notation}) printf (1, "It "s% 3d days in the current year. N ", {curr_day_of_year}) This produces the following result on your standard screen - Hello Euphoria! It is Friday, January 22, 2010. It is 2 h 54 min 58 s. It is 22 days in the current year. The time () method The time () method returns an atomic value, representing the number of seconds elapsed since a fixed point in time. The following example explains it in detail - #! / Home / euphoria-4.0b2 / bin / eui constant ITERATIONS = 100000000 integer p atom t0, t1, loop_overhead t0 = time () for i = 1 to ITERATIONS do - time an empty loop ends for loop_overhead = time () - t0 printf (1, "Loop overhead:% dn ", loop_overhead) t0 = time () for i = 1 to ITERATIONS do p = power (2, 20) end for t1 = (time () - (t0 + loop_overhead) ) / ITERATIONS printf (1, "Time (in seconds) for a power call:% dn ", t1) This produces the following output - Loop overload: 1 Time (in seconds) for a power supply call: 0 Methods linked to date and time Euphoria provides a listof methods that help you manipulate the date and time. These methods are listed in Euphoria Library Routines . Euphoria - Procedures A procedure is a reusable group of code that can be called from anywhere in your program. This eliminates the need to write the same code over and over again. It helps programmers write modular code. Like any other advanced programming language, Euphoria also supports all the features needed to write modular code using procedures. You must have seen procedures such as printf () and length () in previous chapters. We use these procedures over and over again, but they were only written"once in Euphoria"s nucleus. Euphoria also allows you to write your own procedures. This section explains how to write your own procedure in Euphoria. Procedure definition Before using a procedure, you must define it. The most common way to define a procedure in Euphoria is to use the procedure keyword, followed by a unique procedure name, a listof parameters (which can be empty) and a block of instructions that ends with end of procedure instruction . The basic syntax is as follows - instructions procedure procedurename (parameter-list) .......... end procedure Example A simple procedure called sayHello which takes no parameters is defined here - procedure sayHello () met (1, "Hello there ") end of the procedure Calling a procedure To call a procedure somewhere later in the, just write the name of that procedure as follows - #! / home / euphoria-4.0b2 / bin / eui procedure sayHello () met (1, "Hello there ") end procedure - Call the procedure defined above. sayHello () This produces the following result - Hello there Procedure parameters Till now you have seen the procedure without parameters. But there is a possibility to pass different parameters when calling a procedure. These passed parameters can be captured in the procedure and any manipulation can be performed on these parameters. A procedure can take multiple parameters separated by commas. Example Let"s make a small modification in our sayHello procedure. This time you need two parameters - #! / Home / euphoria-4.0b2 / bin / eui procedure sayHello (name of sequence, age of atom) printf (1, "% sa% d years. ", {Name, age}) end procedure - Call the procedure defined above. sayHello ( "zara ", 8) This produces the following result - zara is 8 years old. Euphoria - Functions Euphoria functions are like procedures, but they return a value and can be used in an expression. This chapter explains how to write your own functions in Euphoria. Function definition Before using a function, we need to define it. The most common way to define a function in Euphoria is to use the keyword function , followed by a unique function name, a listof parameters (which can be empty) and a block of instructions that ends with end of function instruction . The basic syntax is displayed here - function functionname instructions (parameter-list) .......... return [Euphoria Object] end function Example One simple function ca lled sayHello which takes no parameters is defined here - function sayHello () met (1, "Hello there ") return 1 end function Call a function To call a function somewhere later in the, you just need to write the name of that function as follows - #! / home / euphoria-4.0b2 / bin / eui function sayHello () met (1, "Hello there ") return 1 end function - Calls the function defined above. sayHello () This produces the following result - Hello there Function parameters Till now we have seen the function without parameters. But there is a possibility to pass different parameters when calling a function.tion. These passed parameters can be captured in the function and any manipulation can be performed on these parameters. A function can take multiple parameters separated by commas. Example Let"s make a small modification in our sayHello function. This time it takes two parameters - #! / Home / euphoria-4.0b2 / bin / eui function sayHello (sequence name, atom age) printf (1, "% sa% d years. ", {Name, age}) return 1 end function - Call the function defined above. sayHello ( "zara ", 8) This produces the following result - zara is 8 years old. The return A Euphoria function must have a return statement before the close end function . Any Euphoria item can be returned. You can, in fact, have several return values, by returning a sequof objects. For example return {x_pos, y_pos} If you have nothing to return, just return 1 or 0. The return value 1 indicates success and 0 indicates failure Euphoria - File I / O Using the Euphoria programming language, you can write programs that read and modify data files on your floppy drive or hard drive, or create new files as the output form. You can even access your computer"s peripherals such as printer and modem. This chapter describes all the basic I / O functions available in Euphoria. For more information on more functions, please refer to the standard Euphoria documentation. Screen display The easiest way to produce output is to use put () statement where you can pass n "any string to displayon the screen. There is another printf () method which can also be used in case you need to format a string using dynamic values. These methods convert expressions you pass to them to a string and write the result to standard output as follows - #! / home / euphoria-4.0b2 / bin / eui met (1, "Euphoria is such a great language, isn "t it? ") This produces the following output on your standard screen - Euphoria really is a great language, isn"t it, isn"t it? Opening and Closing Files Euphoria provides the basic methods needed to manipulate files by default. You can do most file manipulations using the following methods - open () close () printf () gets () getc () The open Method Before you can read or write a file, you must open it using the method open () built into Euphoria. This function creates a file owner which is used to call other support methods associated with it. Syntax integer file_num = open (file_name, access_mode) The above method returns -1 in case of "error opening given filename. Here are the parameters - filename - The filename argument is a string value containing the name of the file you want to access. access_mode - The access_mode determines the mode in which the file should be opened. For example, read, write append, etc. A full listof possible values ​​for file open modes is given in the following table - S.No Modes et deion 1 r Open a read-only text file. File pointer is placed at the beginning of the file. 2 rb Open a file read-only in binary format. The file pointer is placed at the beginning of the file. 3 w Opens a text file for writing only. Overwrites the file if the file exists. If the file does not exist, create a new file to write. 4 wb Open a file for writing only in binary format. Replaces the file if the file exists. If the file does not exist, create a new file to write. 5 u Open a file for reading and writing. The file pointer is placed at the beginningt of the file. 6 ub Open a file for reading and writing in binary format. The file pointer is placed at the start of the file. 7 a Opens a file to add. The file pointer is at the end of the file if the file exists (append mode). If the file doesn "t exist, it creates a new file to write. 8 ab Open a file to add to binary format. The file pointer is at the end of the file if the file exists (append mode). If the file doesn "t exist, it creates a new file to write. Example The following example creates a new text file in the current directory of your Linux system - #! / home / euphoria-4.0b2 / bin / eui integer file_num constant ERROR = 2 constant STDOUT = 1 file_num = open ( "myfile, txt ", "w ") if file_num = -1 then met (ERROR, "couldn "t open myfile n ") else met (STDOUT, "File opened successfully n ") end if If the file opens successfully, then "myfile.txt " is created in your current directory and produces the following result - Opening the file successful The close () method The close () method dumps any unwritten information and closes the file, after which no reading or writing can no longer be performed on the file. Euphoria automatically closes a file when a file "s reference object is reassigned to another file. It is recommended to use the close () method to close a file. Syntax close (file_num); Here, the file value received when opening a file is passed as a parameter. Example The following example creates a file like abovethen close it before the existing program - #! / home / euphoria-4.0b2 / bin / eui integer file_num constant ERROR = 2 constant STDOUT = 1 file_num = open ( "myfile. txt ", "w ") if file_num = -1 then met (ERROR, "couldn "t open myfile n ") else met (STDOUT, "File open successfully n ") end if if file_num = -1 then met (ERROR, "No need to close the file n ") else close (file_num) met (STDOUT, "File closed successfully n ") end if This produces the following result - Opening the file successful File closed successfully Reading and writing files Euphoria provides a set of access methods to make our lives easier when reading or writing to "a file in text mode or in binary mode. Let"s see how to use the printf () and gets () methods to read and write files. The printf () Method The printf () method writes any string to an open file. Syntax printf (fn, st, x) Here are the parameters - fn - Deor of file received from the open () method. st - Format a string where the decimal or atom is formatted using% d and the string or sequence is formatted using% s. x - If x is a sequence, then the format specifiers of st are matched to the corresponding elements of x. If x is an atom, then normally st contains only one format specifier and it is applied to x. However; if st contains multiple format specifiers, then each is applied to the same x value. Example The following example opens a file and writes a person"s name and age to that file - #! / home / euphoria-4.0b2 / bin / eui integer file_num constant ERROR = 2 constant STDOUT = 1 file_num = open (" myfile.txt ", " w ") if file_num = -1 then met (ERROR, "couldn "t open myfile n ") else met (STDOUT, "File open successfully n ") end if printf (file_num, "My name is% s and my age is% dn ", {" Zara ", 8}) if file_num = -1 then put (ERROR, " No need to close file n ") else close (file_num) put (STDOUT, " File closed successfully n ") end if The above example creates the file myfile.txt . Writes the given content to this file and eventually exits. If you open this file it would have the following content - My name is Zara and my age is 8 The gets () Method The gets () method reads a string from an open file. Syntax gets (file_num) Here, the parameter passed ist the deion of the file returned by the opend () method. This method starts reading from the beginning of the file line by line. The characters have values ​​from 0 to 255. The -1 atom is returned at the end of the file. Example Let"s take a myfichier.txt file that is already created. #! / ho me / euphoria-4.0b2 / bin / eui integer file_num subject line constant ERROR = 2 constant STDOUT = 1 file_num = open ( "myfile.txt ", "r ") if file_num = - 1 then put (ERROR, "could not open my file n ") else put (STDOUT, "File opened successfully n ") end if line = gets (file_num) printf (STDOUT, " Read content:% sn ", {line}) if file_num = -1 then met (ERROR, " No need to close file n ") else close (file_num) put (STDOUT, " File successfully closed n ") end if This produces the following result - File opened successfulRead the content: My name is Zara and the age is 8 File Closed Successfully Euphoria provides a listof many methods that help you manipulate files. These methods are listed in the Euphoria Library Routines .