Control Statements Excercises



Back to main excersises page

Control Flow Diagrams

Draw control flow diagrams of the following:

1) an if-then branch 2) an if-then-else branch 3) a while (post-test) loop 4) an if-then-else branch with a nested while loop 5) a while loop with a nested do-while loop 6) a do-while loop with a nested while loop 7) an if-then-else branch with a nested if-then branch 8) an if-then branch with a nested do while loop 9) a while loop with a nested if-then-else branch 10) a do-while-loop with a nested if-then branch 11) an if-then branch with a nested if-then-else branch Answers

Describe the following control flow diagrams:

12)
13)
14)
15)
16)
17)
18)
19)
20)
Answers

Control Statements

Write out code fragments that perform the following tasks. Assume that the following declarations apply, perhaps as local variables in some method: int i, j, k; boolean p, q, r; You may also asume that any output should be printed to the terminal using System.out.print or System.out.println.

If statements

21) Print the word "yes" if i is strictly greater than 50. 22) Print the word "yes" if i is strictly greater than 20 and stricly less than 40. 23) Print the word "yes" if i is strictly greater than 70 and print the word "no" otherwise. 24) Print the word "cool" if i is greater than or equal to 20 and also print the word "uncool" if j is less than i. 25) Print out the word "yes" if i is divisible by j and "no" otherwise. Then print out "cool" if j is divisible by i and "uncool" otherwise. 26) Print out "case1" if both i and j are even. Print "case2" if i is even but j is odd. Print "case3" if i is odd but j is even. Print "case4" if both i and j are odd. Test each case separately. 27) Number 26 above, but using nested statements. (don't test each case separately) 28) For the numbers 1 through 4, print out twice the value of that number if i is equal to it. Take each case one at a time. 29) For the numbers 1 through 4, print out twice the value of that number if i is equal to it. Otherwise, print out the value of i. Take each case one at a time. 30) For the numbers 1 through 4, print out twice the value of that number if i is equal to it. Otherwise, print out the value of i. Be as effecient as you can. 31) Print the word "yes" either when p is true, or when p is false and i is srictly less than j. Otherwise, print "no". Use nesting. 32) Print "yes" for any of the following situations, and "no" for all others: both p and q are true p is true and q is false p is false and q is true 33) Print "yes" for any of the following situations, and "no" for all others: p is false and q is true q is false 34) Print "yes" for any of the following situations, and "no" for all others: all of p, q, and r are true p is true and q and r are false p and r are false q is false and p = !r 35) Print according to the following situations: p is true and q is true ==> print "a" p is true and q is false and r is true ==> print "b" p is true and q is false and r is false ==> print "c" p is false and r is true ==> print "d" p is false and r is false ==> print "e" 36) Print according to the following situations: q is false and p is true ==> print "a" q is false and p is false ==> print "b" q is true and p is true ==> print "c" q is true and p is false and r is false ==> print "d" q is true and p is false and r is true ==> print "e" 37) Print "odd" for any of the following situations, and "even" for all others: p , q, and r are all true p is true and q and r are false q is true and p and r are false r is true and p and q are false 38) Print "yes" for any of the following situations, and "no" for all others: p and q are both true p and q are both false 39) Print "yes" for any of the following situations, and "no" for all others: p is false and q is true p is true and q is false 40) Print "yes" for any of the following situations, and "no" for all others: p is true q is true r is false p and q are both false Be as efficient as you can. Answers

Switch Statements

Assume for problems 47 through 50 that the following declarations and assignments are given: public static final int CHOCOLATE = 1; public static final int VANILLA = 2; public static final int STAWBERRY = 3; public static final int MINT_CHIP = 4; public static final int COOKIE_DOUGH = 5; int flavor; 41) print out "yippee" in case the flavor is mint chip, and "woo hoo" if the flavor is "strawberry, and "all right" if the flavor is cookie dough. 42) print out "yippee" in case the flavor is either chocolate or mint chip. 43) print out "hey, no fair" only in the cases where the flavor is neither chocolate nor vanilla nor strawberry nor mint chip nor cookie dough. 44) print out "yippee" in case the flavor is mint chip, and "woo hoo" if the flavor is strawberry, and "all right" if the flavor is cookie dough. if the flavor is vanilla, print out "score" and then "yippee". otherwise, print out "darn". 45) Print out "a" if i equals 1, "b" if i equals 2, "c" if i equals 3, and "d" if i equals 4. 46) Print out "l" if i equals 1, "m" if i equals 2, "n" if i equals 3 or 4, and "o" if i equals 5. 47) Print out "a" if i equals 1, "b" if i equals 2 or 3, "cdf" if i equals 4, and "df" if i equals 5. Otherwise, print out "f". 48) Print out "adf" if i equals 1, "b" if i equals 2, "cb" if i equals 3 or 4, and "df" if i equals 5. Otherwise, print "f". 49) Print out "cf" if i equals 1 or 5, "a" if i equals 2, "ba" if i equals 3 or 4, and "d" if i equals 6. Otherwise, print "f". 50) For the numbers 1 through 4, print out the value 2*i that number if i is equal to it. Otherwise, print out the value of i. Answers

While and Do While

51) Write a code fragment that will print out five stars in a row using a while loop. 52) Write a code fragment that will print out five stars in a row using a do-while loop. For questions 53 - ???, how many stars get printed by the given code fragments? 53) i = 1; while (i <= 10) { System.out.print("*"); ++i; } 54) i = 1; while (i < 10) { System.out.print("*"); ++i; } 55) i = 0; while (i < 10) { System.out.print("*"); ++i; } 56) i = 0; while (i <= 10) { System.out.print("*"); ++i; } 57) i = 1; while (i <= 10) { System.out.print("*"); ++i; } 58) i = 1; while (i <= 10) { j = 1; while (j <= 10) { System.out.print("*"); ++j; } ++i; } 59) i = 0; while (i <= 10) { j = 1; while (j < 10) { System.out.print("*"); ++j; } ++i; } 60) i = 0; while (i < 5) { j = 1; do { System.out.print("*"); ++j; } while (j < 10); ++i; } 61) Write code fragments, one using while loops and another using do while loops, that will produce the following output: ***** **** *** ** * 62) Write code fragments that will produce the following output: ***** **** *** ** * Can this be done with just do-while loops and no other control statements? 63) Write a code fragment using two while loops that will produce the following output: * * * * * * * * * * * * 64) Write a code fragment using only one while loop that will produce the following output: * * * * * * * * * * * * 65) What ouptput will the following code fragment produce? int line = 1; while (line <= 9) { if (line % 2 == 1) { int starNumber = 1; while (starNumber <= 6) { System.out.print("* "); starNumber += 1; } } else { int starNumber = 1; while (starNumber <= 5) { System.out.print(" *"); starNumber += 1; } } ++line; System.out.println(); } Answers

Answers

Control Flow Diagrams and Control Statements