ESCI 386 IDL Programming for Advanced Earth Science Applications Lesson 6 Program Control



Similar documents
JavaScript: Control Statements I

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

Sources: On the Web: Slides will be available on:

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Number Representation

Section 1. Inequalities

Unix Scripts and Job Scheduling

Selection Statements

Summary of the MARIE Assembly Language

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Visual Logic Instructions and Assignments

Bash shell programming Part II Control statements

PL / SQL Basics. Chapter 3

Introduction to Python

Two-way selection. Branching and Looping

ESCI 386 Scientific Programming, Analysis and Visualization with Python. Lesson 5 Program Control

Systems I: Computer Organization and Architecture

Conditions & Boolean Expressions

Unix Shell Scripts. Contents. 1 Introduction. Norman Matloff. July 30, Introduction 1. 2 Invoking Shell Scripts 2

Lecture 2 Notes: Flow of Control

Supplemental Worksheet Problems To Accompany: The Pre-Algebra Tutor: Volume 1 Section 9 Order of Operations

Answers to Review Questions Chapter 7

Pseudo code Tutorial and Exercises Teacher s Version

Compsci 101: Test 1. October 2, 2013

Lecture 4: Writing shell scripts

Numeral Systems. The number twenty-five can be represented in many ways: Decimal system (base 10): 25 Roman numerals:

Elementary Number Theory and Methods of Proof. CSE 215, Foundations of Computer Science Stony Brook University

Creating a Simple Macro

Systems Programming & Scripting

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

Chapter 2: Algorithm Discovery and Design. Invitation to Computer Science, C++ Version, Third Edition

Chapter 2 Introduction to Java programming

J a v a Quiz (Unit 3, Test 0 Practice)

BASH Scripting. A bash script may consist of nothing but a series of command lines, e.g. The following helloworld.sh script simply does an echo.

How To Program In Scheme (Prolog)

Introduction to Java

Facebook Twitter YouTube Google Plus Website

6. Control Structures

Python Lists and Loops

Grandstream XML Application Guide Three XML Applications

Stacks. Linear data structures

Selection: Boolean Expressions (2) Precedence chart:

EXTENDED FILE SYSTEM FOR F-SERIES PLC

Programming in postgresql with PL/pgSQL. Procedural Language extension to postgresql

5.1 Radical Notation and Rational Exponents

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

Programming Logic and Design Eighth Edi(on

Chapter 3 Operators and Control Flow

EE 261 Introduction to Logic Circuits. Module #2 Number Systems

MATLAB Programming. Problem 1: Sequential

Using Loops to Repeat Code

Visual Basic Programming. An Introduction

PROG0101 Fundamentals of Programming PROG0101 FUNDAMENTALS OF PROGRAMMING. Chapter 3 Algorithms

CSC 221: Computer Programming I. Fall 2011

Writing Control Structures

Lab 4.4 Secret Messages: Indexing, Arrays, and Iteration

Shell Scripts (1) For example: #!/bin/sh If they do not, the user's current shell will be used. Any Unix command can go in a shell script

Unit 7 The Number System: Multiplying and Dividing Integers

Outline. Conditional Statements. Logical Data in C. Logical Expressions. Relational Examples. Relational Operators

THE UNIVERSITY OF TRINIDAD & TOBAGO

VISUAL GUIDE to. RX Scripting. for Roulette Xtreme - System Designer 2.0

Interactive Data Language Guide. Chris Lowder

Exercise 4 Learning Python language fundamentals

The string of digits in the binary number system represents the quantity

Cosmological Arguments for the Existence of God S. Clarke

CmpSci 187: Programming with Data Structures Spring 2015

23. RATIONAL EXPONENTS

Chapter 8 Selection 8-1

Chapter 13 - The Preprocessor

Instructor Özgür ZEYDAN (PhD) CIV 112 Computer Programming

Passing 1D arrays to functions.

Chapter 3. Cartesian Products and Relations. 3.1 Cartesian Products

Computer Science 281 Binary and Hexadecimal Review

CAHSEE on Target UC Davis, School and University Partnerships

Recognizing PL/SQL Lexical Units. Copyright 2007, Oracle. All rights reserved.

Statements and Control Flow

Software development and programming. Software

ECE 122. Engineering Problem Solving with Java

ALGORITHMS AND FLOWCHARTS. By Miss Reham Tufail

The C Programming Language course syllabus associate level

Programming Exercises

Specimen 2015 am/pm Time allowed: 1hr 30mins

Secrets of printf. 1 Background. 2 Simple Printing. Professor Don Colton. Brigham Young University Hawaii. 2.1 Naturally Special Characters

Today. Binary addition Representing negative numbers. Andrew H. Fagg: Embedded Real- Time Systems: Binary Arithmetic

The Deadly Sins of Algebra

Exercises for Design of Test Cases

=

20 Using Scripts. (Programming without Parts) 20-1

4.8 Thedo while Repetition Statement Thedo while repetition statement

arrays C Programming Language - Arrays

Pemrograman Dasar. Basic Elements Of Java

Oct: 50 8 = 6 (r = 2) 6 8 = 0 (r = 6) Writing the remainders in reverse order we get: (50) 10 = (62) 8

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

An Introduction to Number Theory Prime Numbers and Their Applications.

The Answer to the 14 Most Frequently Asked Modbus Questions

Transcription:

ESCI 386 IDL Programming for Advanced Earth Science Applications Lesson 6 Program Control Reading: Bowman, Chapters 16 CODE BLOCKS A code block consists of several lines of code contained between a BEGIN and an END statement. Code blocks can be used with IF, ELSE, FOR, CASE, and WHILE statements. The END statement takes the form of whatever statement began the block o ENDIF, ENDELSE, ENDFOR, ENDCASE, or ENDWHILE, depending on the type of block. o Writing just END instead of ENDIF, ENDFOR, etc. is legal, but is highly discouraged. IF THEN ELSE STATEMENTS if then else statements have either a single-line form or a block form. The single-line form looks like the following examples: if condition then expression if condition then expression1 else expression2 The block form looks like the following examples: Example #1 if condition then begin expression endif Example #2 if condition then begin expression1 endif else begin expression2 endelse

Example #3 if condition1 then begin expression1 endif else if condition2 then begin expression2 endif else if condition3 then begin exression3 endif else begin expression4 endelse TERNARY OPERATOR IDL has a short-hand way of writing simple, IF-THEN-ELSE statement, using the ternary operator,?:. Then syntax for this is: expression with condition? expression1 : expression2 If condition is true, then expression1 is substituted for condition before the complete expression on the left-hand-side of the? character is executed. If condition is false, then expression2 is substituted for condition before the complete expression on the left-hand-side of the? character is executed. Example 1: z = (a gt b)? a : b will assign the value of a to z if a is greater than b. If not, then b will be assigned to z. o If this were written using IF-THEN-ELSE format it would be if (a gt b) then z = a else z = b Example 2: print, (a gt b)? a : b will print the value of a if a is greater than b. If not, then b will be printed. o If this were written using IF-THEN-ELSE format it would be if (a gt b) then print, a else print, b 2

TRUE AND FALSE VALUES A reminder that IDL doesn t have specific logical variable for true and false. Instead, it uses the following rules: Data Type True False Byte, integer, and long Odd integers Zero or even integers Floating point and complex Non-zero values Zero String Any string with non-zero length Null string (" ") This means that you can use variables as the condition in an IF statement as shown by the examples: a = 3 b = 6 if a then print, hi => hi o this evaluates as true and prints because a is an odd integer. a = 3 b = 6 if b then print, hi => o this evaluates as false and doesn t print because b is an even integer. a = -3.24 b = 0.0 if a then print, hi => hi o this evaluates as true and prints because a is a non-zero floating-point number. a = -3.24 b = 0.0 if b then print, hi => o this evaluates as false and doesn t print because b is floating-point zero. a = idl b = if a then print, hi => hi o this evaluates as true and prints because a is a non-null string. a = -3.24 3

b = 0.0 if b then print, hi => o this evaluates as false and doesn t print because b is a null string. CASE STATEMENT Trying to code a multiple if-then-else statement can get cumbersome. IDL has a CASE statement which is much easier for this type of construct. It looks like: case grade of "A" : QPA = 4.0 "B" : QPA = 3.0 "C" : QPA = 2.0 "D" : QPA = 1.0 else : QPA = 0.0 endcase The CASE statement will test each case until it gets to one that is true, and will then execute that statement, ignoring all cases below it. SWITCH STATEMENT The SWITCH statement is similar to the CASE statement, except that it tests each case until it gets to one that is true, and then executes that statement, AS WELL AS any subsequent statement regardless of whether or not they are true. The example for this, taken from the IDL online documentation, is SWITCH x OF 1: PRINT, 'one' 2: PRINT, 'two' 3: PRINT, 'three' 4: PRINT, 'four' ENDSWITCH which if x = 2 would result in the printing of two, three, and four. 4

If the order in the SWITCH were reversed, such as SWITCH x OF 4: PRINT, 'four' 3: PRINT, 'three' 2: PRINT, 'two' 1: PRINT, 'one' ENDSWITCH the result would be the printing of two and one for x = 2. FOR LOOPS IDL has several different constructs for loops. The block-form of a FOR loop in IDL takes the form of for i = m, to n, optional_increment do begin code within loop Examples: for i = 0, 10 do begin d = 2*i print, d would print 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, and 20. for i = 0, 10, 2 do begin would print 0, 2, 4, 6, 8, and 10. o NOTE: If the code within the loop is only a single line, you can actually write it without using the block form (no begin and end statements). You could instead put everything on a single line, such as for i = 0, 10, 2 do 5

To increment backward through a loop, use a negative increment interval. Example for i = 10, 0, -2 do begin would print 10, 8, 6, 4, 2, 0. WHILE LOOPS WHILE loops in IDL take the form of while ( some condition ) do begin code within loop endwhile Example: i = 0 while (i le 5) do begin i = i + 1 endwhile would print 0, 1, 2, 3, 4, and 5. REPEAT UNTIL LOOPS REPEAT UNTIL loops in IDL take the form of repeat begin code within loop endrep until ( some condition ) 6

Example: i = 0 repeat begin i = i + 1 endrep until (i ge 5) would print 0, 1, 2, 3, and 4. FOR and WHILE loops evaluate the condition or counter at the beginning of the loop. REPEAT UNTIL loops evaluate the condition at the end of the loop. BREAKING OUT OF A LOOP OR IF BLOCK The BREAK procedure will terminate a loop or if-block and carry-on with the rest of the program. The EXIT procedure will terminate the program. 7