Presentation is loading. Please wait.

Presentation is loading. Please wait.

SELECTION II CSC 171 FALL 2004 LECTURE 9. Sequential statements start end Block{…}

Similar presentations


Presentation on theme: "SELECTION II CSC 171 FALL 2004 LECTURE 9. Sequential statements start end Block{…}"— Presentation transcript:

1 SELECTION II CSC 171 FALL 2004 LECTURE 9

2 Sequential statements start end Block{…}

3 Simple Branching start end A; If (test){ A;} test

4 Branching start end A; B; If (test){ A;} else { B;} test

5 Structured Programming Using structures as “building blocks”

6

7 start end

8 start end

9

10

11

12 if (maritalStatus == single) { if (income <= 21450.0) { taxRate =.15; } else { taxRate =.27; } } else { if (income <= 86500.0) { taxRate =.28; } else { taxRate =.31; }

13 if ((maritalStatus == single) && (income <= 21450.0)) taxRate =.15; if ((maritalStatus == single) && (income > 21450.0)) taxRate =.27; if ((maritalStatus != single) && (income <= 86500.0)) taxRate =.28; if ((maritalStatus != single) && (income > 86500.0)) taxRate =.31;

14 T F F T F T

15 double rich; String effct; if (richt >= 8.0) effct = “most structures fall”; else if (richt >= 7.0) effct = “many buildings destroyed”; else if (richt >= 6.0) effct = “some buildings damaged”; else if (richt >= 4.5) effct = “some damage to poor construction”; else if (richt >= 3.5) effct = “no damage”; else if (richt >= 0) effct = “not felt much”;

16 int gNum; String gName; if (gNum == 1) gName = “freshman”; if (gNum == 2) gName = “sophomore”; if (gNum == 3) gName = “junior”; if (gNum == 4) gName = “senior”; else gName = “unmatriculated”;

17 int gNum; String gName; if (gNum == 1) gName = “freshman”; if (gNum == 2) gName = “sophomore”; if (gNum == 3) gName = “junior”; if (gNum == 4) gName = “senior”; else gName = “unmatriculated”;

18 Switch Statement Test cases must be constant Test cases must be integers Test is “==“

19 T F F T default break

20 int gNum; String gName; switch(gNum) { case 1: gName = “freshman”; break; case 2: gName = “sophomore”; break; case 3: gName = “junior”; break; case 4: gName = “senior”; break; default : gName = “unmatriculated”; break; }

21 int gNum; String gName; if (gNum == 1) gName = “freshman”; if (gNum == 2) gName = “sophomore”; if (gNum == 3) gName = “junior”; if (gNum == 4) gName = “senior”; else gName = “unmatriculated”;

22 int gNum; String gName; switch(gNum) { case 1: gName = “freshman”; case 2: gName = “sophomore”; case 3: gName = “junior”; case 4: gName = “senior”; default : gName = “unmatriculated”; }// everyone is unmatriculated

23 The Selection operator Semantics (3 operands) ? : Usually, Shorthand for: if ( ) x = y; else x = z; Which can be written as x = ? y : z ;

24 Absolute Value of x int x, absoluteX; x = myGetInt();// some user input absoluteX = (x >= 0) ? x : -x; Instead of if (x >=0) absoluteX = x; else absoluteX = -x;

25 SimpleTaxes double income, taxRate; income = myGetd();// user input taxRate = (income >= 120000.0) ? 0.31 : 0.40 ; Instead of if (income >= 120000.0) taxRate =.31; else taxRate =.41;


Download ppt "SELECTION II CSC 171 FALL 2004 LECTURE 9. Sequential statements start end Block{…}"

Similar presentations


Ads by Google