Presentation is loading. Please wait.

Presentation is loading. Please wait.

Control Statements I: Loop. Control Statements Used to specify the order in which computations will be carried out Three types Branch: if, case, switch.

Similar presentations


Presentation on theme: "Control Statements I: Loop. Control Statements Used to specify the order in which computations will be carried out Three types Branch: if, case, switch."— Presentation transcript:

1 Control Statements I: Loop

2 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

3 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 endfor

4 Flow chart for “for … do …” i = v1 Statements i > v2 ? Yes No i = i + inc Statements after loop

5 Loop within loop ni = 3 nj = 5 nk = 8 A=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

6 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

7 Example: use loop to find the maximum value of an array A=randomn(seed,100) max=0.0 for i=0,99 do begin while(a(i) gt max) do max=a(i) endfor print,max,max(a) end

8 Flow chart for “while … do …” Start Statements Condition satisfied ? Yes No Statements after loop

9 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)

10 Flow chart for “repeat … until …” Start Statements Condition satisfied? Yes No Statements after loop

11 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

12 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:

13 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

14 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 2 8 (256) colors TrueColor (24-bit): each pixel can display one of 2 24 (16,777,216) colors

15 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: 2 8 colors, used mostly with both 8-bit decomposed color: 2 24 colors used mostly with 24-bit

16 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

17 Load predefined color table Syntax: loadct, table_number (0 to 40) To look at the names of the color tables loadct

18 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 file w=20 xr=[-1,1]*w yr=[-1,1]*w plot,xr,yr,/nodata x=0 y=0 r=randomn(seed,100) dx=r(0) dy=r(1) oplot,x+[0,dx],y+[0,dy]


Download ppt "Control Statements I: Loop. Control Statements Used to specify the order in which computations will be carried out Three types Branch: if, case, switch."

Similar presentations


Ads by Google