Skip to content

Commit ae17feb

Browse files
committed
Fixes issue RunestoneInteractive#60 adds code
1 parent 89faf82 commit ae17feb

1 file changed

Lines changed: 49 additions & 10 deletions

File tree

‎source/ch_5_conditionals.ptx‎

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,66 @@
1414
<section xml:id="simple-if">
1515
<title>Simple if</title>
1616

17-
<pre>if condition: statement1 statement2 ...</pre>
1817
<p>
19-
In Java this same pattern is simply written as:
18+
In Python the simple if statement is written as:
2019
</p>
2120

22-
<pre>if (condition) { statement1 statement2 ... }</pre>
21+
<program interactive="activecode" language="python">
22+
<code>
23+
score = 95
24+
if score &gt;= 90:
25+
print("Excellent work!")
26+
</code>
27+
</program>
28+
<p>
29+
In Java, this same pattern requires two changes: the condition must be in parentheses <code>()</code>, and the code block must be enclosed in curly braces <code>{}</code>.
30+
</p>
31+
<program interactive="activecode" language="java">
32+
<code>
33+
public class SimpleIfExample {
34+
public static void main(String[] args) {
35+
int score = 70;
36+
if (score &lt;= 70) {
37+
System.out.println("Needs work!");
38+
}
39+
}
40+
}
41+
</code>
42+
</program>
2343
<p>
2444
Once again you can see that in Java the curly braces define a block rather than indentation.
25-
In Java the parenthesis around the condition are required because if is technically a function that evaluates to True or False.
45+
In Java the parenthesis around the condition are required because it is technically a function that evaluates to True or False.
2646
</p>
2747
</section>
2848

2949
<section xml:id="if-else">
3050
<title>if else</title>
3151

32-
<pre>if condition: statement1 statement2 ... else: statement1 statement2 ...</pre>
33-
<p>
34-
In Java this is written as:
35-
</p>
36-
37-
<pre>if (condition) { statement1 statement2 ... } else { statement1 statement2 ... }</pre>
52+
<p>The Java equivalent follows the same syntactical rules as before.</p>
53+
<program interactive="activecode" language="python">
54+
<code>
55+
age = 16
56+
if age &gt;= 18:
57+
print("You can vote.")
58+
else:
59+
print("You are not yet eligible to vote.")
60+
</code>
61+
</program>
62+
63+
<program language="java">
64+
<code>
65+
public class IfElseExample {
66+
public static void main(String[] args) {
67+
int age = 16;
68+
if (age &gt;= 18) {
69+
System.out.println("You can vote.");
70+
} else {
71+
System.out.println("You are not yet eligible to vote.");
72+
}
73+
}
74+
}
75+
</code>
76+
</program>
3877
</section>
3978

4079
<section xml:id="elif">

0 commit comments

Comments
 (0)