IDL 5220 Week 1 - Programming Basics



  1. Definition of a Program
    A series of commands that manipulate the values of variables to solve a problem.
  2. IDL
    IDL, Interactive Data Language, is a powerful array-based language. Although procedures can be compiled, it is generally an interpreted language. Interpreted languages are slightly slower than compiled ones such as C++, but much easier to debug. They are also far less structured as they do not require stuff like variable declarations. IDL programs can be written using any text editor such as vi or emacs. Programs should be given a filename ending in .pro. To enter IDL, just type idl at a command prompt. Since it is interpreted, you can enter a command or a series of commands at an IDL prompt instead of writing an entire program in some cases. To run a program, type .run program_name at an IDL prompt. You do not need to include the .pro from the program's name. The same command is used for compiling a procedure. The procedure is then run simply by typing its name at an IDL prompt, along with any required arguments.
  3. IDL Program Structure
    Programs must have "end" as their last line. There is no need to declare variables. IDL is dynamically typed: an operation on a variable can change that variable's type.
  4. Primitives
  5. Variables
  6. Maths and Expressions
  7. Input/Output
  8. Blocks:
    Blocks can be used in place of a single command. Blocks consist of a BEGIN, a series of commands, and an END.
  9. Conditionals
    Conditionals and Loops are the two fundamental building blocks of programs. Conditionals execute a command based on the result of an expression.
             /-\        |-| 
    P--------|B|---B----|S|-----Q
             \_/        |_|  |
              |___!B_________|
    
    If B then S
  10. Loops
    Loops repeat a series of commands while a certain condition is true. Loops have the structure {I} while B do S {I and !B}. I is the loop invariant that must be true before, during, and after the loop, and B is the condition. While B is true, the loop continues. When B is made false, the program exits the loop.
            ____________________
            |                  |
           /-\        |-|      |
    ---I---|B|---B----|S|----I-|
           \_/        |_|
            |
            |_____!B______--- >
    
  11. Random #s
    A random number between 0 and 1 with a uniform distribution can be generated by executing the following statement:
    RANDOMU(seed)
    So in order to set X to a random number between 1 and 100, you would type:
    X = fix(randomu(seed)*100)+1
    randomu(seed)*100 is done first since it is in parenthesis and results in a floating point random number between 0 and 100. fix converts this to an integer by truncation, meaning it is now between 0 and 99, inclusive. Adding 1 makes the number between 1 and 100, inclusive.
  12. Help and ?

Homework 1: Guess My Number. Write a program that generates a random number (integer) between 1 and 100 and then asks the user to guess what the number is. The program will tell the user if the number is higher or lower than the guess and keep asking the user to guess until the user gets the number right. It will then tell the user how many tries it took to get the number. Save your program as last_name_HW1.pro and e-mail it as an attachment to me.