Control Statements Excercises






1)
2)
3)
4)
5)
6)
7)
8)
9)
10)
11)


Excercises 12) a do-while loop 13) a while loop with a nested if-then branch 14) an if-then-else branch with a nested if-then-else-branch 15) an if-then branch with a nested while loop 16) a do-while loop with a nested do-while loop 17) an if-then-else branch with a nested do-while loop 18) an if-then branch with a nested if-then branch 19) a while loop with a nested while loop 20) a do-while loop with a nested if-then-esle branch Excercises

If Statements 21) if (i > 50) System.out.println("yes"); 22) if ((i > 20) && (i < 40)) System.out.println("yes"); 23) if (i > 70) System.out.println("yes"); else System.out.println("no"); 24) if (i >= 20) System.out.println("cool"); if (j < i) System.out.println("uncool"); 25) if (i % j == 0) System.out.println("yes"); else System.out.println("no"); if (j % i) System.out.println("cool"); else System.out.println("uncool"); 26) if ((i % 2 == 0) && (j % 2 == 0)) { System.out.println("case1"); } else if ((i % 2 == 0) && (j % 2 == 1)) { System.out.println("case2"); } else if ((i % 2 == 1) && (j % 2 == 0)) { System.out.println("case3"); } else if ((i % 2 == 1) && (j % 2 == 1)) { System.out.println("case4"); } 27) if (i % 2 == 0) { if (j % 2 == 0) System.out.println("case1"); else System.out.println("case2"); } else { if (j % 2 == 0) System.out.println("case3"); else System.out.println("case4"); } 28) if (i == 1) { System.out.println("2"); } else if (i == 2) { System.out.println("4"); } else if (i == 3) { System.out.println("6"); } else if (i == 4) { System.out.println("8"); } 29) if (i == 1) { System.out.println("2"); } else if (i == 2) { System.out.println("4"); } else if (i == 3) { System.out.println("6"); } else if (i == 4) { System.out.println("8"); } else { System.out.println(i); } 30) if ((i >= 1) && (i <= 4)) System.out.println((2*i)); else System.out.println(i); 31) if (p) System.out.println("yes"); else { if (i < j) System.out.println("yes"); else System.out.println("no"); } 32) if (p) System.out.println("yes"); else { if (q) System.out.println("yes"); else System.out.println("no"); } 33) if (q) { if (p) System.out.println("no"); else System.out.println("yes"); } else System.out.println("no"); 34) if (q) System.out.println("yes"); else { if (r) System.out.println("no"); else System.out.println("yes"); } 35) if (p) { if (q) System.out.println("a"); else { if (r) System.out.println("b"); else System.out.println("c"); } } else { if (r) System.out.println("d"); else System.out.println("e"); } 36) if (p) { if (q) { if (r) System.out.println("odd"); else System.out.println("even"); } else { if (r) System.out.println("even"); else System.out.println("odd"); } } else { if (q) { if (r) System.out.println("even"); else System.out.println("odd"); } else { if (r) System.out.println("odd"); else System.out.println("even"); } } 37) if (q) { if (p) System.out.println("c"); else { if (r) System.out.println("e"); else { System.out.println("d"); } } else { if () System.out.println("c"); else { if () System.out.println("e"); else { System.out.println("d"); } } 38) if (p == q) System.out.println("yes"); else { System.out.println("no"); 39) if (p == q) System.out.println("no"); else { System.out.println("yes"); 40) System.out.println("yes"); Excercises Switch Statements 41) switch (flavor) { case MINT_CHIP: System.out.println("yipee"); break; case STRAWBERRY: System.out.println("woo hoo"); break; case COOKIE_DOUGH: System.out.println("all right"); break; } 42) switch (flavor) { case MINT_CHIP: case CHOCLOATE: System.out.println("yipee"); } 43) switch (flavor) { case CHOCOLATE: case VANILLA: case STRAWBERRY: case MINT_CHIP: case COOKIE_DOUGH: break; default: System.out.println("hey, no fair"); } 44) switch (flavor) { case VANILLA: System.out.println("score"); case MINT_CHIP: System.out.println("yipee"); break; case STRAWBERRY: System.out.println("woo hoo"); break; case COOKIE_DOUGH: System.out.println("all right"); break; default: System.out.println("darn"); } 45) switch (i) { case 1: System.out.println("a"); break; case 2: System.out.println("b"); break; case 3: System.out.println("c"); break; case 4: System.out.println("d"); break; case 4: } 46) switch (i) { case 1: System.out.println("l"); break; case 2: System.out.println("m"); break; case 3: case 4: System.out.println("n"); break; case 5: System.out.println("o"); break; } 47) switch (i) { case 1: System.out.println("a"); break; case 2: case 3: System.out.println("b"); break; case 4: System.out.println("c"); case 5: System.out.println("d"); default: System.out.println("f"); } 48) switch (i) { case 3: case 4: System.out.println("c"); case 2: System.out.println("b"); break; case 1: System.out.println("a"); break; case 5: System.out.println("d"); default: System.out.println("f"); } 49) switch (i) { case 6: System.out.println("d"); break; case 3: case 4: System.out.println("b"); case 2: System.out.println("a"); break; case 1: case 5: System.out.println("c"); default: System.out.println("f"); } 50) switch (i) { case 1: case 2: case 3: case 4: System.out.println((2*i)); break((2*i)); default: System.out.println(i); } Excercises While and Do While 51) i = 1; // priming read while (i <= 5) { System.out.print("*"); i += 1; } 52) i = 1; // priming read do { System.out.print("*"); i += 1; } while (i <= 5); 53) 10 54) 9 55) 10 56) 11 57) 10 58) 100 59) 99 60) 45 61)
i = 5;
while (i > 0) {
    j = i;
    while (j > 0) {
        System.out.print("*");
        --j;
    }
    --i;
    System.out.println();
}
    or    
i = 5;
do {
    j = i;
    do {
        System.out.print("*");
       --j;
    } while (j > 0);
    --i;
    System.out.println();
} while (i > 0);
62) i = 5; while (i > 0) { j = 5 - i; while (j > 0) { System.out.print(" "); --j; } k = i; while (k > 0) { System.out.print("*"); --k; } --i; System.out.println(); } no. you will run into trouble on the first line becaue you will have to print at least one space before the stars. However, you can do most of it as follows. i = 5; do { j = 5 - i; while (j > 0) { System.out.print(" "); --j; } k = i; do { System.out.print("*"); --k; } while (k > 0); --i; System.out.println(); } while (i > 0); 63) 64) 65) * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Excercises