Presentation is loading. Please wait.

Presentation is loading. Please wait.

Adapted from Pearson Addison-Wesley Addison Wesley is an imprint of Chapter 4: Selection Structures: if Statement Adapted from Problem Solving & Program.

Similar presentations


Presentation on theme: "Adapted from Pearson Addison-Wesley Addison Wesley is an imprint of Chapter 4: Selection Structures: if Statement Adapted from Problem Solving & Program."— Presentation transcript:

1 Adapted from Pearson Addison-Wesley Addison Wesley is an imprint of Chapter 4: Selection Structures: if Statement Adapted from Problem Solving & Program Design in C Sixth Edition By Jeri R. Hanly & Elliot B. Koffman

2 1-2 © 2010 Pearson Addison-Wesley. All rights reserved. 1-2 Review Constant and variable names … Example CANNOT start with a number2i CAN contain a number elsewhereh2o CANNOT contain any arithmetic operators... r*s+t CANNOT contain any other punctuation marks... #@x% £ !!a CAN contain or begin with an underscore _height_ CANNOT be a C keyworddouble CANNOT contain a spaceim stupid CAN be of mixed casesXSquared

3 1-3 © 2010 Pearson Addison-Wesley. All rights reserved. 1-3 Review #include int main() { int a,b,c,d,e; a = 10; b = 4.3; c = 4.8; d = 'A'; e = 4.3 + 4.8; printf("a = %d\n", a); printf("b = %d\n", b); printf("c = %d\n", c); printf("d = %d\n", d); printf("e = %d\n", e); printf("b+c = %d\n", b+c); printf("4.3 + 4.8 as integer type would yield %d \n", 4.3 + 4.8); return (0); } WHAT WILL BE ON THE SCREEN?

4 1-4 © 2010 Pearson Addison-Wesley. All rights reserved. 1-4 Review a = 10 b = 4 c = 4 d = 65 e = 9 b+c = 8 4.3 + 4.8 as integer type would yield 858993459 #include int main() { int a,b,c,d,e; a = 10; b = 4.3; c = 4.8; d = 'A'; e = 4.3 + 4.8; printf("a = %d\n", a); printf("b = %d\n", b); printf("c = %d\n", c); printf("d = %d\n", d); printf("e = %d\n", e); printf("b+c = %d\n", b+c); printf("4.3 + 4.8 as integer type would yield %d \n", 4.3 + 4.8); return (0); }

5 1-5 © 2010 Pearson Addison-Wesley. All rights reserved. 1-5 Review Integer division int rslt; rslt = 17 / 5; rslt = 3 rslt = 7 / 4;rslt = 1 Modulus /remainder rslt = 17 % 5;rslt = 2 rslt = 7 % 4;rslt = 3

6 1-6 © 2010 Pearson Addison-Wesley. All rights reserved. 1-6 Review #include #define PI 3.14 int main() { int i, p1, p2, sin_number, log_number; double x,y,z; printf("Enter an integer:" ); scanf("%d",&i); printf("Absolute value of %d= %5d\n",i,abs(i)); printf("Enter any three numbers. First for square root, others for floor and ceil:" ); scanf("%lf %lf %lf", &x, &y, &z); printf("square root of %f= %7.4lf\n",x,sqrt(x)); printf("ceils of %f and %f are %7.0f and %7.0f\n", y,z,ceil(y),ceil(z)); printf("floors of %f and %f are %7.0f and %7.0f\n", y,z,floor(y),floor(z)); /*Power function*/ printf("Enter two integers for power function: "); scanf("%d %d", &p1, &p2); printf("%d to the power %d= %f \n", p1, p2, pow(p1,p2)); /*Trigonometric and logaritmic functions */ printf("Enter two integers for sinus, in degrees, and log functions: "); scanf("%d %d", &sin_number, &log_number); printf("sin(%d) = %7.4f \n", sin_number, sin(PI*sin_number/180)); printf("log(%d) = %7.4f \n", log_number, log10(log_number)); system ("pause"); return (0); }

7 1-7 © 2010 Pearson Addison-Wesley. All rights reserved. 1-7 Figure 4.1 Evaluation Tree and Step-by- Step Evaluation for !flag || (y + z >= x - z)

8 1-8 © 2010 Pearson Addison-Wesley. All rights reserved. 1-8 if Statement Single selection structure  One Alternative if (condition) statement; Double selection structure  Two Alternatives if (condition) first statement; else second statement;

9 1-9 © 2010 Pearson Addison-Wesley. All rights reserved. 1-9 Figure 4.4 Flowcharts of if Statements with (a) Two Alternatives and (b) One Alternative if (rest_heart_rate > 56) printf(“Your heart is in excellent condition”) else printf(“Keep up your exercise program”) if (x !=0.0) product = product *x;

10 1-10 © 2010 Pearson Addison-Wesley. All rights reserved. 1-10 Figure 4.5 if Statement to Order x and y

11 1-11 © 2010 Pearson Addison-Wesley. All rights reserved. 1-11 Nested If double salary, tax; scanf(“%lf” &salary); if (salary < 0.0) tax = -1.0; else if (salary < 15000.00)/* first range*/ tax = 0.15 * salary; else if (salary < 30000.00)/* second range*/ tax = (salary - 15000.00) * 0.18 + 2250.00; else if (salary < 50000.00)/* third range*/ tax = (salary - 30000.00) * 0.22 + 5400.00; else if (salary < 80000.00)/* fourth range*/ tax = (salary - 50000.00) * 0.27 + 11000.00; else if (salary <= 150000.00)/* fifth range*/ tax = (salary - 80000.00) * 0.33 + 21600.00; else tax = -1.0;

12 1-12 © 2010 Pearson Addison-Wesley. All rights reserved. 1-12 Nested if vs Sequence of “if” statements if (x>0) num_of_pos = num_of_pos + 1; else if (x<0) num_of_neg = num_of_neg + 1; else num_of_zero = num_of_zero + 1; if (x>0) num_of_pos = num_of_pos + 1; if (x<0) num_of_neg = num_of_neg + 1; if (x==0) num_of_zero = num_of_zero + 1; versus Both are logically equivalent but sequence is less readable and less efficient

13 1-13 © 2010 Pearson Addison-Wesley. All rights reserved. 1-13 Figure 4.11 Flowchart of Road Sign Decision Process

14 1-14 © 2010 Pearson Addison-Wesley. All rights reserved. 1-14 Road Sign Decision if (road_status == ‘S’) if (temp > 0 ){ printf(“Wet roads ahead\n”); }else { printf(“Icy roads ahead\n”); } else printf(“Drive carefully\n”);


Download ppt "Adapted from Pearson Addison-Wesley Addison Wesley is an imprint of Chapter 4: Selection Structures: if Statement Adapted from Problem Solving & Program."

Similar presentations


Ads by Google