Presentation is loading. Please wait.

Presentation is loading. Please wait.

Control Statements II: Branch. Branch Three ways: if, case, switch The if statement executes statement(s) if a specified condition is true if condition.

Similar presentations


Presentation on theme: "Control Statements II: Branch. Branch Three ways: if, case, switch The if statement executes statement(s) if a specified condition is true if condition."— Presentation transcript:

1 Control Statements II: Branch

2 Branch Three ways: if, case, switch The if statement executes statement(s) if a specified condition is true if condition then statement if condition then begin statement(s) endif if condition then statement else statement if condition then begin statement(s) endif else begin statement(s) endelse

3 Branch (if) Begin Else block Then block If Condition TrueFalse Endelse

4 Example: Calculate salary based on hours 0-40 hours/week $10/hour 40-168 hours/week $15/hour for overtime part >168 hours/week error, stop Jump1: Print, ‘Input hours’ Read, nhr If(nhr ge 0 and nhr le 40) then begin s=10.0*nhr Endif else begin if(nhr gt 40 and nhr le 168) then begin nhr_over=nhr-40 s=10.0*40+15.0*nhr_over endif else begin print,’Incorrect hours’ goto, jump1 endelse Endelse Print, ‘Salary = ’, s

5 Branch (cont.) The case statement branches to the first matching case in a list of cases and executes the statement(s) there, then terminates and go to the statement after the “endcase” statement case expression of exp1: exp2: statement exp3: begin statements(s) end else: statement endcase case 1 of cond1: statement cond2: begin statements(s) end else: statement endcase

6 Branch (case) Begin Block 1 Match Case 1? YesNo Endcase Match Case 2? YesNo Match Case 3? YesNo Block 2 Block 3

7 Example: Calculate salary based on hours 0-40 hours/week $10/hour 40-168 hours/week $15/hour for overtime part >168 hours/week error, stop Jump1: Print, ‘Input hours’ Read, nhr Case 1 of (nhr ge 0 and nhr le 40): s=10.0*nhr (nhr gt 40 and nhr le 168): begin nhr_over=nhr-40 s=10.0*40+15.0*nhr_over end else: begin print,’Incorrect hours’ goto,jump1 end Endcase Print, ‘Salary = ’, s

8 Branch (cont.) The switch statement branches to the first matching case in a list of cases and executes the statement(s) there, then goto the second matching case … switch expression of exp1: exp2: statement exp3: begin statement(s) end else: statement endswitch Difference with the case statement: (1) “case” executes only the first matching case, while “switch” executes all matching cases (2) “case” doesn’t allow no matching, but “switch” does

9 Branch (switch) Begin Block 1 Match Case 1? YesNo Endswitch Match Case 2? YesNo Match Case 3? YesNo Block 2 Block 3

10 Seven colors ; black, red, green, blue, cyan, mag, white r=[ 0, 1, 0, 0, 0, 1, 1 ] g=[ 0, 0, 1, 0, 1, 0, 1 ] b=[ 0, 0, 0, 1, 1, 1, 1 ] tvlct, r*255, g*255, b*255

11 Filling line plots with colors: Polyfill Syntax: Polyfill, x, y, color=color (,/line_fill) Example: pro eclipse, x, y, w, h, col, iline n=500 temp=findgen(n+1)/n*2.0*!pi x1=x+w*0.5*sin(temp) y1=y+h*0.5*cos(temp) if(iline eq 0) then begin polyfill,x1,y1,color=col endif else begin polyfill,x1,y1,color=col, /line_fill endelse end plot,[0,1], /nodata eclipse,0.3, 0.3, 0.2, 0.2, 1, 0 eclipse,0.7, 0.7, 0.2, 0.2, 1, 1 end

12 User-defined symbols: Usersym Syntax: Usersym, x, y, color=color, /fill Example: w=1 x=[-1,1,1,-1,-1]*w y=[-1,-1,1,1,-1]*w usersym,x,y,color=1,/fill plot,findgen(10),psym=8

13 In-class assignment IV Use two different methods (if and case) to calculate the saturation vapor pressure (svp) for a given temperature T (in deg C) T> 0 C svp=6.112 e 17.67T/(T+243.5) T< 0 C svp=6.1115e 22.452T/(T+272.55) T 100 C incorrect data Please confirm the Collatz conjecture (hailstone numbers): Take any natural number n. If n is even, divide it by 2 to get n/2. If n is odd, multiply it by 3 and add 1 to obtain 3n+1. Repeat the process indefinitely. No matter what number you start with, you will always eventually reach 1. Please save the numbers and print them out. Then plot the numbers using (a) bar plot, (b) a user-defined symbol


Download ppt "Control Statements II: Branch. Branch Three ways: if, case, switch The if statement executes statement(s) if a specified condition is true if condition."

Similar presentations


Ads by Google