control statements i: loop. control statements used to specify the order in which computations will...

18
Control Statements I: Loop

Upload: osborne-smith

Post on 19-Jan-2018

212 views

Category:

Documents


0 download

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 endfor

TRANSCRIPT

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

Control Statements I: Loop

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

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

Page 3: 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 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

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

Flow chart for “for … do …”i = v1

Statements

i > v2 ? Yes

No

i = i + inc

Statements after loop

Page 5: 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 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

Page 6: 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 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

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

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

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

Flow chart for “while … do …”Start

Statements

Condition satisfied ?

Yes

No

Statements after loop

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

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

Flow chart for “repeat … until …”Start

Statements

Condition satisfied?

Yes

No

Statements after loop

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

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

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

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:

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

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

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

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

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

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

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

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

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

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

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

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]