control statements i: loop. control statements used to specify the order in which computations will...
DESCRIPTION
Loop I: For … do … The for statement executes statement(s) for a specific number of times by incrementing a loop index variable from a start value to an end value for i = v1, v2 do statement for i = v1, v2, inc do statement for i = v1, v2, inc do begin statement(s) endfor Example: sum=0 for i = 1, 100 do begin sum=sum+i endforTRANSCRIPT
Control Statements I: Loop
Control Statements • Used to specify the order in which
computations will be carried out • Three types Branch: if, case, switch Loop: for, while, repeat Exit or jump: return, break, continue, goto
Loop I: For … do …• The for statement executes statement(s) for a
specific number of times by incrementing a loop index variable from a start value to an end value
for i = v1, v2 do statement for i = v1, v2, inc do statement for i = v1, v2, inc do begin statement(s) endforExample: sum=0 for i = 1, 100 do begin sum=sum+i endfor
Flow chart for “for … do …”i = v1
Statements
i > v2 ? Yes
No
i = i + inc
Statements after loop
Loop within loopni = 3nj = 5nk = 8A=findgen(ni,nj,nk)for i =0,ni-1 do begin for j =0,nj-1 do begin for k=0,nk-1 do begin sum=sum+a(i,j,k) endfor endforendfor
Loop II: While … do …• The while statement executes statement(s) while
a specified condition is true. while condition do statement while condition do begin statement(s) endwhile
Example: sum=0 i=1 while(i le 100) do begin sum=sum+i i=i+1 endwhile
Example: use loop to find the maximum value of an array
A=randomn(seed,100)max=0.0for i=0,99 do begin while(a(i) gt max) do max=a(i)endforprint,max,max(a)end
Flow chart for “while … do …”Start
Statements
Condition satisfied ?
Yes
No
Statements after loop
Loop III: Repeat … until …• The repeat statement executes statement(s) until
a specified condition is true. repeat statement until condition repeat begin statement(s) endrep until condition
Example: sum=0 i=1 repeat begin sum=sum+i i=i+1 endrep until(i gt 100)
Flow chart for “repeat … until …”Start
Statements
Condition satisfied?
Yes
No
Statements after loop
Exit or jump• Four ways: return, break, continue, goto• The return statement causes an immediate
exit from the current program unit, and control is returned to the caller.
return, result return
• The break statement causes an immediate exit from a loop (for, while, or repeat), or a branch (case or switch) statement. Control is transferred to the next statement after the end of the loop
Exit or jump (cont.)• The continue statement skip any remaining
statement(s) in the current loop iteration (for, while, or repeat) and jump to the next iteration of a loop.
Example: sum=0 for i = 1, 100 do begin continue sum=sum+i endfor
• The goto statement jumps to a specific location goto, label label:
Graphic devices• Commonly used graphic devices: WIN, MAC, X, PS• Select a graphic device set_plot, device_name (‘X’ or ‘PS’)
• Close a device device, /close device, /close_file
Configure the graphic device• Syntax: device, keywords• Keep windows (for X windows): device, retain=2
• Filename and page setup (for PS only):
device, filename=file_name, $ /portrait (or /landscape), $ /inches, xsize=xsiz, xoffset=xoff, ysize=ysiz, yoffset=yoff
• Select display mode (for PS only) device, bits_per_pixel=8 (or 24) IDL has two display modes PseudoColor (8-bit): each pixel can display one of 28 (256) colors TrueColor (24-bit): each pixel can display one of 224 (16,777,216) colors
Configure the graphic device (cont)
• Select color model device, decomposed=0 (for indexed color, recommended) 1 (for decomposed color) IDL has two color models indexed color: 28 colors, used mostly with both 8-bit decomposed color: 224 colors used mostly with 24-bit
Indexed color and color table• Set a color table tvlct, red, green, blue red, green and blue are byte vectors that can have up
to 256 elements. Each element contains a value between 0 and 255 that specifies the intensity of the corresponding color.
• Example r=[0,255] g=[0,0] b=[0,0] tvlct, r, g, b plot,[0,1],color=0
Load predefined color table• Syntax: loadct, table_number (0 to 40) To look at the names of the color tables loadct
In-class assignment IV• Use three different methods (for, while, repeat) to
calculate the sum of all odd numbers between 1 and 1000
• X=randomn(seed, 10, 10). Use loop to find the maximum value of this array.
• Random walk (Brownian motion): Use color to plot on the screen and on a postscript filew=20xr=[-1,1]*wyr=[-1,1]*wplot,xr,yr,/nodatax=0y=0r=randomn(seed,100)dx=r(0)dy=r(1)oplot,x+[0,dx],y+[0,dy]