Writing Loops and Conditional Statements in IDL - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Writing Loops and Conditional Statements in IDL

Description:

You can save a IDL variable or entire session for later work using the SAVE command ... Only starts if the initial condition is met and then continues until the ... – PowerPoint PPT presentation

Number of Views:313
Avg rating:3.0/5.0
Slides: 23
Provided by: ntsg6
Category:

less

Transcript and Presenter's Notes

Title: Writing Loops and Conditional Statements in IDL


1
Writing Loops and Conditional Statements in IDL
  • Matt Jolly
  • NTSG, Univ. of MT.

2
Outline
  • Review of topics from last time
  • Definition of loops and conditionals
  • Introduction to the IF statement
  • Introduction to the FOR loop

3
Review
  • You can create your own IDL programs in four
    ways Batch files, Main-level programs, Functions
    and Procedures
  • The two most often used are Functions and
    Procedures
  • Functions return a single value, Procedures
    return as many values as desired

4
Todays Quick Tip
  • You can save a IDL variable or entire session for
    later work using the SAVE command
  • SAVE, Filenamemywork.sav
  • Later, you can restore all the IDL variables you
    were working on using the restore command
  • RESTORE, Filenamemywork.sav

5
Program Control Statements
  • Two main categories
  • Decision Does a value meet a certain criteria?
  • Repetition Perform certain operations or
    calculations sequentially

6
Decisions
  • True or False expressions
  • Use the IF statement
  • Multiple decisions use the CASE statement
  • General form
  • IF lttestgt THEN ltstatementgt
  • lttestgt is any boolean comparison that returns
    true or false
  • ltstatementgt is any valid IDL syntax or block of
    syntax

7
Conditional Testing
  • IDL does not use traditional programming style
    comparisons such as gt , lt or
  • It uses the equivalent in text, similar to Perl
  • For example if you wanted to print the variable
    X if variable X is greater than or equal to Y
    then you would type
  • IF X GE Y THEN print, X

8
Comparison Operators
  • EQ Equal To ()
  • LT Less Than (lt)
  • LE Less Than or Equal To (lt)
  • GT Greater Than (gt)
  • GE Greater Than or Equal To (gt)
  • NE Not Equal To (ltgt or !)

9
Multiple Line Statements
  • Statements can be blocked in IDL using the BEGIN
    END commands
  • This tells IDL to execute them as a block, rather
    than individually
  • The syntax for IDL blocked IF statements is then
    an extension of the simpler case
  • IF lttestgt THEN BEGIN
  • ..
  • ENDIF

10
Extending IF with ELSE
  • You can extend the IF statement by specifying an
    alternative set of statements to perform if the
    condition is NOT met
  • IF lttestgt THEN BEGIN
  • Print, X
  • ENDIF ELSE BEGIN
  • Print, Y
  • ENDELSE

11
The CASE statement
  • The case statement is an extension of the IF
    statement for cases where the test is not boolean
  • For a CASE statement there is one input and based
    on the value of that input, there could be any
    number of program responses
  • In fact, all case statements could be written
    with multiple IFs

12
Example CASE Statement
  • CASE myVAR OF
  • 0 x 5
  • 1 BEGIN
  • X 5
  • Y X 2
  • END
  • ELSE x 0
  • ENDCASE
  • Example from Fanning text
  • In this case, if myVar was equal to 0, then the
    program would set X 5

13
Repetition (Loops)
  • Two kinds of repetition fixed count and variable
    count
  • Fixed loops are ALWAYS performed in a program and
    variable loops are similar to IF statements and
    are only done while certain conditions are met
  • Fixed count repetition is usually done with a FOR
    loop
  • Example Loop through all the values in an array
    of a known or determinable size
  • Variable count is done with a WHILE or REPEAT
    control statement

14
FOR Loops
  • FOR loops repeat an operation one or more times
  • This is one of the most useful tools in IDL and
    one you should master quickly
  • You can use FOR loops to cycle through all the
    elements in a data array and perform some
    operations
  • Many loops in IDL are IMPLICIT, such as when we
    multiply an array by a scalar value
  • myArr fltarr(10)
  • myArr myArr 10
  • This is essentially an implicit for loop

15
FOR Loop syntax
  • You must define an index variable and the limits
    of the loop
  • FOR i0,9 DO myArri I

16
Loops can be nested
  • Nested loops are loops INSIDE other loops
  • At NTSG, this a fairly common because it is
    easier to loop through rows and columns of a
    raster dataset
  • They are written the same way
  • FOR row0,10 DO BEGIN
  • FOR col0,10 DO BEGIN
  • Print,myRasterrow,col
  • ENDFOR
  • ENDFOR

17
Multiple Line For Loops
  • FOR loops are extended to multiple lines the same
    way IF statements were with BEGIN and END
    commands
  • FOR i0,9 DO BEGIN
  • myArri I
  • Print,myArri
  • ENDFOR

18
Variable Length Loops
  • Main decision do you want to try looping through
    the data at least once no matter what?
  • If so, use REPEAT. If not, use WHILE
  • For REPEAT, the program statements are executed
    at least once and then tested at the end of the
    list of program statements
  • For WHILE, the condition must be met before the
    loop is started
  • Both cases, the statements are executed WHILE or
    UNTIL a requirement is met

19
WHILE Loops
  • Only starts if the initial condition is met and
    then continues until the condition is not met
  • WHILE (x lt 100) DO BEGIN
  • X X 0.35
  • ENDWHILE

20
REPEAT Loops
  • Always execute the code AT LEAST once
  • REPEAT BEGIN
  • X X 0.35
  • ENDREP UNTIL X GE 100

21
Differences Between WHILE and REPEAT
  • WHILE loop may NEVER function if the condition is
    not met and REPEAT always does it at least once
  • WHILE executes when a condition is met
  • REPEAT executes until a condition is not met

22
The Exercise
  • Use the program we wrote last time to analyze the
    Montana DEM and only display the values greater
    than 2000 meters
Write a Comment
User Comments (0)
About PowerShow.com