Hello, hello, hello.

 

            Okay, now that we’ve gotten Euphoria installed and ready to run; and we’ve played with a few demos, it’s time to write our first program from scratch.

 

            Go ahead and start your favorite editor.  If you’re prompted for a file name, type hello.ex.  If you’ve forgotten how to use Euphoria’s DOS editor, why don’t you go back and review that section now.  Almost every computer textbook I’ve read starts out with a “Hello world” program.  All it does is display a simple message on the screen.  Windows users are going to expect a message box, but we’ll get to that a little down the road.  Right now, we’re just going to use and old-fashioned DOS screen (aka. Console for some of you).  As we move along, you’ll start to see why we’ve started simple.

 

Type the following line into your editor:

 

puts(1,”Hello, world!”)

 

Now save this file as hello.ex and execute it.  If you are running it from Euphoria’s editor, you should see a black screen or window with the phrase “Hello, world!” in the upper left hand corner.  Press <Enter> to return to the editor and I’ll explain what we did. 

 

If you didn’t get the result you expected (like if you got an error or something), double-check that puts is all lower-case; make sure you only have one set of parentheses, that the number one and comma are first inside the parentheses, and that the phrase is in a single set of double-quotes.  Unlike many languages, the error messages in Euphoria will give you a good clue as to what went wrong.  There are no semicolons to end a line as there are in other languages, and for this simple program there are no special files needed to get a message on the screen.  This process of finding typing mistakes in your program is called syntax debugging.

 

Now, what exactly does this simple line mean?  Well, we’ll start with the most obvious.  The text “Hello, world!” is what we call a string literal.  Each character, including the comma, space, and exclamation point, is represented as a number in the computer’s memory.  These numbers are strung together to form a phrase, hence the word string.  A string only refers to numbers grouped together to make text, which is why we use a set of quotation marks to mark the string.  The fact that we put the string right in the command, or statement, as program lines are called is why we say literal.  There are a couple other ways to tell a statement what string to use, but we’ll get to that in a little bit.

 

The 1 tells the statement were to display the text.  This particular statement requires a file pointer.  You can use this same statement to write text to the screen or to a file.  There are three file pointers (i.e., a number used to refer to a line in a list of open files) that are reserved in Euphoria, as in most other languages.  File pointer zero is for standard input.  This usually means the keyboard, but could be a barcode scanner or another device plugged into the keyboard port.  File pointer one is for standard output.  This is usually the monitor, but could also be a Braille pad or projection screen.  File pointer two is for standard error, which is usually the same as standard output.  We’ll talk more about playing with files in the next chapter.  We use puts(1,…) instead of puts(0,…) because it’s very unlikely you will want to display text on your keyboard.  And if you did, the display portion would be assign to file pointer 1, and the keyboard portion to file pointer 0.

 

Let’s change this program just a little bit.  Change your program to look like the following:

 

atom fp

sequence str

 

fp = 1

str = “Hello, world!”

 

puts(fp,str)

 

            Go ahead and save and execute this program now.  You should get the exact same results as before.  If you have any errors, fix them and try again.

 

            In this case, we’ve changed the puts statement to use variables instead of literals.  A variable is simply a name that contains a value.  Euphoria has two main types of variables: atoms and sequences.  An atom is a single number or character of almost any size or decimal value.  For advanced programmers, an atom can be an integer or floating point 32-bit value with up to 14 decimal places of accuracy.  A sequence is a group of numbers or group of groups.  Sequences are very dynamic, unlike arrays in other languages, and can contain atoms, sequences, or both in any combination.

 

            Variables need to be both defined and initialized before they are used.  In Euphoria, to the chagrin of many programmers (and the joy of many debuggers), variables cannot be initialized on the same line as they are defined.  I cannot say atom fp = 1 because it will cause an error.  The reason for this is readability.  If I say atom apples,oranges = 5, it looks like I have 5 apples and 5 oranges.  However, the truth is I have 5 oranges and an unknown (uninitialized) number of apples.  The author of the language prefers clarity to conciseness in this case, and I must say I agree.

 

            Let’s change the program again to the following:

 

atom fp

sequence stra, strb

 

fp = 1

stra = “Hello,”

strb = “world!”

 

puts(fp,stra & strb)

 

     Save and execute the program again.  This time, you should see “Hello,world!” without the space.  The missing space is an example of a logical error because it caused an unexpected result while the program was running, rather than an error when the program started as is the case with a syntax error.  The simplest and most concise way of fixing this error would be to add a space after the comma where we initialize stra.  However, I’m sure you’ve noticed by now that none of these examples except the first one are concise.  This is so you can get a feel for the way a Euphoria program is put together. 

 

            Since we’re just playing around, let’s add a space without changing stra or strb.  Change the puts line to the following, then save and execute the program:

 

puts(fp,stra & “ “ & strb)

 

            Each ampersand (&) in the line is called a concatenation.  This operation, similar to a plus (+) in other languages, joins two sequences into one.  You can use as many concatenations on a single line as you wish, and they will join the sequences together from left to right.  A word of warning to you extreme programmers out there – each concatenation costs something in terms of temporary memory and speed.  If you feel your program is running too slowly, you may wish to examine the number of concatenations occurring in your program and find other ways to get the results you are looking for.  It is usually better to do your concatenations in the initialization section of your program rather than main body of your program.  Of course, this is not always possible or preferred; and that is a decision you can only learn to make with experience.

 

Loopy for you.

 

            Let’s start a new program and call it forloop1.ex.  Type the program below; and if you’re using the Euphoria editor, note how it fills in the syntax after you type for.  You will have to arrow over and down to fill in the missing parameters.

 

sequence name

 

name = “Mike” -- change Mike to your own name

 

for ctr = 1 to 10 by 1 do

  puts(1,”My name is “&name)

end for

 

            Save and execute (we also say run a lot, meaning execute) your program.  Wow!  That was kinda cool, wasn’t it.  Okay, maybe not to the old hats out there; but on the old Tandy TRS-80 computers this simple program did some decent animation for the time.  Change the 10 to 10000 and run it again.  By the way, computer programs never use commas in their numbers; so be sure not to put one in out of habit.

 

            Let’s explain some of the new parts of this program.  On the initialization line, you’ll notice two hyphens (--).  This indicates a comment until the end of the line.  A comment can go either after a statement or on a line by itself, but never before a statement or in the middle of a statement.  Putting the two hyphens at the beginning of a statement will cause that statement to become a comment.  Comments, as we mentioned in the last chapter, are never executed.  Please, use them liberally.  Some languages offer a means of creating comments that spread over several lines.  Euphoria does not.  To create a multi-line comment, you must type two hyphens at the beginning of each line.

 

            The for statement is a type of loop.  Loops repeat an action until some condition is reached.  When a for statement is executed, it defines the variable name (ctr in this case) and initializes it to the first number after the equal sign (one).  It then executes all the statements, in order, until it reaches the end for line.  Then it adds the number after by to the variable and checks if the variable is greater than the number after to.  If not, it performs all the statements between for and end for in order again, and adds and checks again.  Once the variable is greater than the number after to, the loop is finished and the program continues.  It is possible (and usually done) to leave out the by parameter.  Delete by 1 so the for line reads

 

for ctr = 1 to 10000 do 

 

Run the program again and notice that you get the same results.  Change it again to read

 

for ctr = 1 to 10000 by 1000 do

 

and run it.  Notice that now you only have the text repeated 10 times.

 

            As you’ve noticed, the program as is prints the text across the screen and wraps to the next line when it hits the right edge.  This might make a cool effect, but it’s not too conducive to easy reading.  Change the statement inside the for loop to read as follows and run the program again:

 

  puts(1,”My name is “&name&”.\n”)

 

     Adding the “\n” tells Euphoria to add a line feed (the number 10) to the end of the text.  If you change “\n” to “\r” and run the program, you will see only one line of text.  The “\r” is a carriage return (number 13), which tells the computer to continue printing at the beginning of the same line.  It used to be (and on some computers still is) that you would have to use both “\n” and “\r” to continue printing at the beginning of the next line.  Nowadays, on most computers, using only “\n” will give the same result.  There are also a couple other characters that will only print with a backslash in front of them.  They are (perhaps obviously) the backslash “\\”, double-quotes “\””, and single-quotes “\’”.

 

            Just to reinforce the relationship between numbers and characters, lets start one more new program named forloop2.ex.  Type, save, and execute the program below:

 

sequence alphabet

 

alphabet = “”

 

for letter = 65 to 90 do

  alphabet = alphabet & letter

  puts(1,alphabet & “\n”)

end for

 

            You should see a kind of pyramid going down the screen adding one more letter to the end of each line.  Notice that the variable names tell us exactly what we are doing.  There are some variable names that we can’t use in a program, such as for, end, to, or any other word that can be mistaken for a statement.  Because Euphoria is case sensitive, variables named For or FOR are perfectly acceptable.  However, I find such names confusing and consider any program using them to be in poor style.

 

            The number 65 is the number representing the capital letter “A”.  This association of numbers to letters and symbols is known in the PC world as ASCII.  There are 128 standard ASCII codes from 0 to 127 and 128 extended ASCII codes from 128 to 255.

 

            The first statement inside the loop is an assignment.  We are taking the current value of alphabet, concatenating the next letter to the end, and assigning the result back to alphabet.  We could easily define 26 different variables and assign the result to the next one in the list.  But that would be neither concise nor clear.  In contrast to the assignment in the first line inside the loop, the concatenation in the puts statement is called an expression.  The portion of the assignment to the right of the equal sign is also called an expression.  As we get into the next chapter and later start talking about functions, we will be evaluating some rather complex expressions.

 

            There are two more things I would like to mention.  First, the for loop variable, letter in this case, can only be used in an expression.  You cannot assign any value to a for loop variable.  You also cannot use the variable outside the for loop.  Secondly, it is also possible to count backwards in a for loop.  Change the for statement to read

 

for letter = 90 to 65 by –1 do

 

and run the program.  If you left out the by parameter you would get nothing on the screen.  But with the –1, you get the same pyramid from Z to A.

 

Review.

 

            Hopefully you’ve learned a few things from this chapter.  If you’re still not quite comfortable with the ideas of strings, concatenation, puts, variables, and for loops; go back and re-read the chapter.  Play around with the example programs presented here and see what works and what doesn’t.  Euphoria is a safe language, so you won’t blow up your computer by just playing around.  If you get errors, try to figure out why.  If the program doesn’t do what you expect, try a little logical debugging.  If you get totally stuck, or completely don’t understand something from this chapter, send a message to EuForum@topica.com.  The members of our community will be most eager to answer your questions.  Remember:  the only stupid question is the one that isn’t asked.

 

            Do you feel like you’re ready to create your own program?  Try your hand at these.

 

1.      Create a program that displays the entire ASCII table across the screen.  Remember that you can only puts a string, so an assignment may be necessary.  Also, assigning “” to a variable will clear whatever is currently there.

 

2.      Create a program that displays a name and address as you would print it on an address label.  You will need either variables or string literals for the name, address, city, state or province, country, and postal code.

 

 

 

Next chapter….Numbers up!