1 / 14

Chapter 4: Selection Structures: if Statement

Chapter 4: Selection Structures: if Statement. Adapted from Problem Solving & Program Design in C Sixth Edition By Jeri R. Hanly & Elliot B. Koffman. Review. Review. #include <stdio.h> int main() { int a,b,c,d,e; a = 10; b = 4.3; c = 4.8;

amena
Télécharger la présentation

Chapter 4: Selection Structures: if Statement

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 4:Selection Structures: if Statement Adapted from Problem Solving & Program Design in C Sixth Edition By Jeri R. Hanly & Elliot B. Koffman

  2. Review

  3. Review #include <stdio.h> 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. Review a = 10b = 4c = 4d = 65e = 9b+c = 8 4.3 + 4.8 as integer type would yield 858993459 #include <stdio.h> 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. 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. Review #include <stdio.h> #include <stdlib.h> #include <math.h> #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. Figure 4.1 Evaluation Tree and Step-by-Step Evaluation for !flag || (y + z >= x - z)

  8. if Statement • Single selection structure One Alternative if (condition) statement; • Double selection structure  Two Alternatives if (condition) first statement; else second statement;

  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. Figure 4.5 if Statement to Order x and y

  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. 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; Both are logically equivalent but sequence is less readable and less efficient versus 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;

  13. Figure 4.11 Flowchart of Road Sign Decision Process

  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”);

More Related