3_Answers_5_2_if-1

3_Answers_5_2_if-1 - CIS 15AG 1 Answers Chapter 5:...

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

View Full Document Right Arrow Icon
CIS 15AG Answers Chapter 5: Selection – Making Decisions 11. Predict the output: if (a > 0 && b > 0) printf("**"); printf("Done\n"); if (a > 0) printf("*"); if (b > 0) printf("*"); printf("Done\n"); 12. What happens when a semicolon is inadvertently inserted as it is shown below? if (a > 0 && b > 0) ; printf("**"); printf("Done\n"); The above fragment of code can be rewritten as shown below: 13. Predict the output: if (a > 0 && b > 0) printf("**"); printf("Done\n"); if (a > 0 && b > 0) { printf("**"); printf("Done\n"); } Indentation is not sufficient when two or more statements depend on the same condition: braces must be used! 1 a b Output 5 5 **Done 5 -5 Done -5 5 Done -5 -5 Done a b Output 5 5 **Done 5 -5 *Done -5 5 *Done -5 -5 Done a b Output 5 5 **Done 5 -5 **Done -5 5 **Done -5 -5 **Done a b Output 5 5 **Done 5 -5 Done -5 5 Done -5 -5 Done a b Output 5 5 **Done 5 -5 -5 5 -5 -5 Unexpected results! When the condition is true nothing happens! Then the next two statements are executed whether the condition is true or not! if (a > 0 && b > 0) ; printf("**"); printf("Done\n");
Background image of page 1

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

View Full DocumentRight Arrow Icon
CIS 15AG Answers Chapter 5: Selection – Making Decisions 14. Predict the output: if (a > b) a = b; printf("%d %d", a, b); if (a > b) { a = b; printf("%d %d", a, b); } 15. Predict the output: if (a > b) { hold = a; a = b; b = hold } printf("%d %d", a, b); 16. Predict the output: sum = 0; if (a > 0) sum += a; if (b > 0) sum += b; if (c > 0) sum += c; if (d > 0) sum += d; printf("%d", sum); 2 a b Output 10 5 5 5 5 5 5 5 5 10 5 10 a b Output 10 5 5 5 5 5 5 10 a b Output 10 5 5 10 5 5 5 5 5 10 5 10 a b c d Output 10 10 10 10 40 -10 10 10 10 30 10 -10 10 10 30 10 10 -10 10 30 10 10 10 –10 30 -10 -10 10 10 20 -10 10 -10 10 20 -10 10 10 –10 20 -10 -10 -10 10 10 -10 -10 10 –10 10 -10 -10 -10 -10 0
Background image of page 2
CIS 15AG Answers Chapter 5: Selection – Making Decisions 17. Predict the output: count = 0; if (a > 0) count++; if (b > 0) count++; if (c > 0) count++; if (d > 0) count++; printf("%d", count); 18. Write a function that receives four integer values and returns the smallest value, then
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 11

3_Answers_5_2_if-1 - CIS 15AG 1 Answers Chapter 5:...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online