280 likes | 398 Views
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 ) }
E N D
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
decision F T if (condition) { <statement> <statement> (optional) } else { <statement> <statement> (optional) } <other statements> ecs30 WInter 2012 Lecture #07
What is the “condition”? ecs30 WInter 2012 Lecture #06
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
Brainstorming… What is this program segment doing? Assuming: int x, y, temp; ecs30 WInter 2012 Lecture #06
void Compare_and_swap(double *xp, *yp) { double temp; if (*xp > *yp) { temp = *yp; *yp = *xp; *xp = temp; } } ecs30 WInter 2012 Lecture #06
if ((!flag) || ((y+z) >= (x-z))) { // true } else { // false } ecs30 WInter 2012 Lecture #06
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
“conditional” flow control ecs30 WInter 2012 Lecture #06
Conditional Swapping Assuming: int x, y, temp; ecs30 WInter 2012 Lecture #06
if ((!flag) || ((y+z) >= (x-z))) { // true } else { // false } ecs30 WInter 2012 Lecture #06
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
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
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
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
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
Special Symbols(Appendix C) Logical • ||, &&, >, <, >=, <=, ==, != • ! Bitwise • |, &, ^, <<, >>, ~ ecs30 WInter 2012 Lecture #06
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
Brainstorming… ecs30 WInter 2012 Lecture #06
Brainstorming… if ((x>= min) && (x <= max)) { // TRUE } ecs30 WInter 2012 Lecture #06
Brainstorming… ecs30 WInter 2012 Lecture #06
Brainstorming… if ((x < z) || (x > y)) { // TRUE } ecs30 WInter 2012 Lecture #06
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
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
decision F T decision ecs30 WInter 2012 Lecture #07