This preview shows pages 1–4. Sign up to view the full content.

CIS 15AG Answers Chapter 5: Selection – Making Decisions 28. Predict the output if (a > 0) if (a < 100) { r = 10; if (a < 50) r = 20; } else r = 30; else r = 40; printf("%d", r); 29. Predict the output r = 30; if (a > 0) { if (b > 0) r = 50; } else r = 80; printf("%d", r); 30. Predict the output r = 30; if (a > 0) if (b > 0) r = 50; else r = 80; printf("%d", r); 1 a Output 50 10 -50 40 25 20 100 30 a b Output 10 10 50 10 -2 30 -2 10 80 -2 -2 80 a b Output 10 10 50 10 -2 80 -2 10 30 -2 -2 30

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CIS 15AG Answers Chapter 5: Selection – Making Decisions 31. What happens when = is used instead of == ? r = 30; if (a == b) if (a > 0) r = 50; else r = 80; printf("%d", r); r = 30; if (a = b) if (a > 0) r = 50; else r = 80; printf("%d", r); 32. Rewrite the following fragment of code using proper indentation, then predict the output. x = 10; y = 20; z = 30; if (x > y){x = y; z += 50;} else if(x < 25){x += 5; y -= 5; z *= 5;} else z = 0; printf("%d %d %d", x, y, z); 2 a b Output 5 5 50 5 -5 30 -5 -5 80 a b Output 5 5 50 5 -5 80 -5 -5 80 Unexpected results! b is assigned to a first, then we check if a is true! The above fragment of code can be rewritten as shown below: r = 30; a = b; if (a) // != 0 if (a > 0) r = 50; else r = 80; printf("%d", r); "The indentation emphasizes the logical structure of the program. Although C compilers do not care about how a program looks, proper indentation and spacing are critical to in making programs easy for people to read . We recommend writing one statement per line, and using blanks around operators to clarify grouping." The C Programming Language – Brian W. Kernighan, Dennis M. Ritchie
CIS 15AG Answers Chapter 5: Selection – Making Decisions x = 10; y = 20; z = 30; if (x > y) // False { x = y; z += 50; } else if (x < 25) // True { x += 5; // 15 y -= 5; // 15 z *= 5; // 150 } else z = 0; z++; // 151 printf("%d %d %d", x, y, z); 33. What is the value of c after executing the following code?

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.