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