1 / 28

ecs30 Winter 2012: Programming and Problem Solving # 07: Chapters 2~ 5

ecs30 Winter 2012: Programming and Problem Solving # 07: Chapters 2~ 5. Dr. S. Felix Wu Computer Science Department University of California, Davis http://www.cs.ucdavis.edu/~wu/ wu@cs.ucdavis.edu. decision. F. T. if (condition) { <statement> <statement> (optional ) }

amara
Download Presentation

ecs30 Winter 2012: Programming and Problem Solving # 07: Chapters 2~ 5

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. ecs30 Winter 2012:Programming and Problem Solving#07: Chapters 2~5 Dr. S. Felix Wu Computer Science Department University of California, Davis http://www.cs.ucdavis.edu/~wu/ wu@cs.ucdavis.edu ecs30 WInter 2012 Lecture #07

  2. decision F T if (condition) { <statement> <statement> (optional) } else { <statement> <statement> (optional) } <other statements> ecs30 WInter 2012 Lecture #07

  3. What is the “condition”? ecs30 WInter 2012 Lecture #06

  4. OR, AND, XOR X Y (X || Y) (X && Y) (X Y) T T T T F T F T F T F T T F T F F F F F ecs30 WInter 2012 Lecture #06

  5. Brainstorming… What is this program segment doing? Assuming: int x, y, temp; ecs30 WInter 2012 Lecture #06

  6. void Compare_and_swap(double *xp, *yp) { double temp; if (*xp > *yp) { temp = *yp; *yp = *xp; *xp = temp; } } ecs30 WInter 2012 Lecture #06

  7. ecs30 WInter 2012 Lecture #06

  8. if ((!flag) || ((y+z) >= (x-z))) { // true } else { // false } ecs30 WInter 2012 Lecture #06

  9. printf and fprintf printf("Name: %s\n", yourname); fprintf(stdout, "Name: %s\n", yourname); %./a.out > out.xyz fprintf(stderr, "Name: %s\n", yourname); ecs30 WInter 2012 Lecture #06

  10. “conditional” flow control ecs30 WInter 2012 Lecture #06

  11. ecs30 WInter 2012 Lecture #06

  12. ecs30 WInter 2012 Lecture #06

  13. Conditional Swapping Assuming: int x, y, temp; ecs30 WInter 2012 Lecture #06

  14. if ((!flag) || ((y+z) >= (x-z))) { // true } else { // false } ecs30 WInter 2012 Lecture #06

  15. T/F if (x == 0) {// TRUE} else {//FALSE} • What should be “x”? (In what ranges of x, the condition will be true?) ecs30 WInter 2012 Lecture #06

  16. T/F if (x == 1) {// TRUE} else {//FALSE} • What should be “x”? (In what ranges of x, the condition will be true?) • ONLY (x == 1) (0 for 32 bits) 00000000 00000000 00000000 00000001 ecs30 WInter 2012 Lecture #06

  17. T/F if (x == 1) {// TRUE} else {//FALSE} • What should be “x”? if (x) {// TRUE} else {//FALSE} 00000010 00010000 00000000 00000000 !(00000000 00000000 00000000 00000000) if (x = 0) {// TRUE} else {//FALSE} (00000000 00000000 00000000 00000000) Always false! ecs30 WInter 2012 Lecture #06

  18. Logical Negation if (!x) {// TRUE} else {//FALSE} • What should be the value of x? if (x == 0) {// TRUE} else {//FALSE} ecs30 WInter 2012 Lecture #06

  19. Logical/Bitwise • Logical: (if statements) • X = {false or true} {zero or otherwise} • Bitwise: we operate on every bit! 00000000 00000000 00000000 00000001 & 10101010 00000000 00000000 10101011 00000000 00000000 00000000 00000001 ecs30 WInter 2012 Lecture #06

  20. Special Symbols(Appendix C) Logical • ||, &&, >, <, >=, <=, ==, != • ! Bitwise • |, &, ^, <<, >>, ~ ecs30 WInter 2012 Lecture #06

  21. if and ? printf(“%d\n”, (x>y) ? x : y); if (x > y) { printf(“%d\n”, x); } else { printf(“%d\n”, y); } ecs30 WInter 2012 Lecture #06

  22. Brainstorming… ecs30 WInter 2012 Lecture #06

  23. Brainstorming… if ((x>= min) && (x <= max)) { // TRUE } ecs30 WInter 2012 Lecture #06

  24. Brainstorming… ecs30 WInter 2012 Lecture #06

  25. Brainstorming… if ((x < z) || (x > y)) { // TRUE } ecs30 WInter 2012 Lecture #06

  26. char class; switch(class) { case ‘B’: case ‘b’: printf(“Battleship\n”); break; case ‘C’: case ‘C’: printf(“Cruiser\n”); break; default: printf(“Unknown class %c\n”, class); } ecs30 WInter 2012 Lecture #06

  27. int class; switch(class) { case 0: case 1: printf(“Battleship\n”); break; case 2: printf(“Cruiser\n”); break; default: printf(“Unknown class %d\n”, class); } ecs30 WInter 2012 Lecture #06

  28. decision F T decision ecs30 WInter 2012 Lecture #07

More Related