|
| 1 | +Conditionals |
| 2 | +============ |
| 3 | + |
| 4 | +Conditional statements in Python and Java are very similar. In Python we |
| 5 | +have three patterns: |
| 6 | + |
| 7 | +Simple if |
| 8 | +--------- |
| 9 | + |
| 10 | +:: |
| 11 | + |
| 12 | + if condition: |
| 13 | + statement1 |
| 14 | + statement2 |
| 15 | + ... |
| 16 | + |
| 17 | +In Java this same pattern is simply written as: |
| 18 | + |
| 19 | +:: |
| 20 | + |
| 21 | + if (condition) { |
| 22 | + statement1 |
| 23 | + statement2 |
| 24 | + ... |
| 25 | + } |
| 26 | + |
| 27 | +Once again you can see that in Java the curly braces define a block |
| 28 | +rather than indentation. In Java the parenthesis around the condition |
| 29 | +are required because if is technically a function that evaluates to True |
| 30 | +or False. |
| 31 | + |
| 32 | +if else |
| 33 | +------- |
| 34 | + |
| 35 | +:: |
| 36 | + |
| 37 | + if condition: |
| 38 | + statement1 |
| 39 | + statement2 |
| 40 | + ... |
| 41 | + else: |
| 42 | + statement1 |
| 43 | + statement2 |
| 44 | + ... |
| 45 | + |
| 46 | +In Java this is written as: |
| 47 | + |
| 48 | +:: |
| 49 | + |
| 50 | + if (condition) { |
| 51 | + statement1 |
| 52 | + statement2 |
| 53 | + ... |
| 54 | + } else { |
| 55 | + statement1 |
| 56 | + statement2 |
| 57 | + ... |
| 58 | + } |
| 59 | + |
| 60 | +elif |
| 61 | +---- |
| 62 | + |
| 63 | +Java does not have an elif pattern like Python. In Java you can get the |
| 64 | +functionality of an elif statement by nesting if and else. Here is a |
| 65 | +simple example in both Python and Java. |
| 66 | + |
| 67 | +.. activecode:: pyelif |
| 68 | + :language: python |
| 69 | + |
| 70 | + grade = int(input('enter a grade')) |
| 71 | + if grade < 60: |
| 72 | + print 'F' |
| 73 | + elif grade < 70: |
| 74 | + print 'D' |
| 75 | + elif grade < 80: |
| 76 | + print 'C' |
| 77 | + elif grade < 90: |
| 78 | + print 'B' |
| 79 | + else: |
| 80 | + print 'A' |
| 81 | + |
| 82 | +In Java we have a couple of ways to write this |
| 83 | + |
| 84 | +.. activecode:: javaelif |
| 85 | + :language: java |
| 86 | + :sourcefile: ElseIf.java |
| 87 | + |
| 88 | + public class ElseIf { |
| 89 | + public static void main(String args[]) { |
| 90 | + int grade = 85; |
| 91 | + |
| 92 | + if (grade < 60) { |
| 93 | + System.out.println('F'); |
| 94 | + } else { |
| 95 | + if (grade < 70) { |
| 96 | + System.out.println('F'); |
| 97 | + } else { |
| 98 | + if (grade < 80) { |
| 99 | + System.out.println('F'); |
| 100 | + } else { |
| 101 | + if (grade < 90) { |
| 102 | + System.out.println('F'); |
| 103 | + } else { |
| 104 | + System.out.println('F'); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | +We can get even closer to the elif statement by taking advantage of the |
| 113 | +Java rule that a single statement does not need to be enclosed in curly |
| 114 | +braces. Since the if is the only statement used in each else we can get |
| 115 | +away with the following. |
| 116 | + |
| 117 | +.. activecode:: javaelif2 |
| 118 | + :language: java |
| 119 | + :sourcefile: ElseIf.java |
| 120 | + |
| 121 | + public class ElseIf { |
| 122 | + public static void main(String args[]) { |
| 123 | + int grade = 85; |
| 124 | + if (grade < 60) { |
| 125 | + System.out.println('F'); |
| 126 | + } else if (grade < 70) { |
| 127 | + System.out.println('D'); |
| 128 | + } else if (grade < 80) { |
| 129 | + System.out.println('C'); |
| 130 | + } else if (grade < 90) { |
| 131 | + System.out.println('B'); |
| 132 | + } else System.out.println('A'); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | +switch |
| 137 | +------ |
| 138 | + |
| 139 | +Java also supports a ``switch`` statement that acts something like the |
| 140 | +elif statement of Python under certain conditions. To write the grade |
| 141 | +program using a switch statement we would use the following: |
| 142 | + |
| 143 | +.. activecode:: javaswitch |
| 144 | + :language: java |
| 145 | + :sourcefile: SwitchUp.java |
| 146 | + |
| 147 | + public class SwitchUp { |
| 148 | + public static void main(String args[]) { |
| 149 | + int grade = 85; |
| 150 | + |
| 151 | + int tempgrade = grade / 10; |
| 152 | + switch(tempgrade) { |
| 153 | + case 10: |
| 154 | + case 9: |
| 155 | + System.out.println('A'); |
| 156 | + break; |
| 157 | + case 8: |
| 158 | + System.out.println('B'); |
| 159 | + break; |
| 160 | + case 7: |
| 161 | + System.out.println('C'); |
| 162 | + break; |
| 163 | + case 6: |
| 164 | + System.out.println('A'); |
| 165 | + break; |
| 166 | + default: |
| 167 | + System.out.println('F'); |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | +The ``switch`` statement is not used very often, and I recommend you do |
| 173 | +not use it! First, it is not as powerful as the ``else if`` model |
| 174 | +because the switch variable can only be compared for equality with an |
| 175 | +integer or enumerated constant. Second it is very easy to forget to put |
| 176 | +in the ``break`` statement. If the break statement is left out then then |
| 177 | +the next alternative will be automatically executed. For example if the |
| 178 | +grade was 95 and the ``break`` was omitted from the ``case 9:`` |
| 179 | +alternative then the program would print out both A and B. |
| 180 | + |
| 181 | +Boolean Operators |
| 182 | +----------------- |
| 183 | + |
| 184 | +{sub:boolean\_operators} |
| 185 | + |
| 186 | +The conditionals used in the if statement can be boolean variables, |
| 187 | +simple comparisons, and compound boolean expressions. |
| 188 | + |
| 189 | +Java also supports the boolean expression. |
| 190 | +``condition ? trueValue : falseValue`` This expression can be used to |
| 191 | +test a condition as part of an assignment statement. For example |
| 192 | +``a = a % 2 == 0 ? a*a : 3*x -1`` In the previous assignment statement |
| 193 | +the expression ``a%2 ==0`` is first checked. If it is true then a is |
| 194 | +assigned the value ``a * a`` if it is false then a is assigned the value |
| 195 | +of ``3*x-1``. Of course all of this could have been accomplished using a |
| 196 | +regular if else statement, but sometimes the convenience of a single |
| 197 | +statement is too much to resist. |
0 commit comments