Skip to content

Commit 438b3ed

Browse files
Simplify concept about if-statements (#2508)
* Deprecate concept exercise `blackjack` * Rename concept to `if-else-statements` * Remove `if` statements from `numbers` concept * Remove introduction.md.tpl from `blackjack` * Add if-else-statements concept to cars-assemble
1 parent 5eb6aa8 commit 438b3ed

File tree

16 files changed

+242
-252
lines changed

16 files changed

+242
-252
lines changed

concepts/conditionals-if/.meta/config.json

Lines changed: 0 additions & 7 deletions
This file was deleted.

concepts/conditionals-if/about.md

Lines changed: 0 additions & 69 deletions
This file was deleted.

concepts/conditionals-if/introduction.md

Lines changed: 0 additions & 50 deletions
This file was deleted.

concepts/conditionals-if/links.json

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"blurb": "If-else statements can be used to perform conditional logic.",
3+
"authors": ["TalesDias", "sanderploegsma"],
4+
"contributors": []
5+
}

concepts/if-else-statements/about.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# About
2+
3+
## The _if-then_ statement
4+
5+
The most basic control flow statement in Java is the [_if-then_ statement][if-statement].
6+
This statement is used to only execute a section of code if a particular condition is `true`.
7+
An _if-then_ statement is defined using the `if` clause:
8+
9+
```java
10+
class Car {
11+
void drive() {
12+
// the "if" clause: the car needs to have fuel left to drive
13+
if (fuel > 0) {
14+
// the "then" clause: the car drives, consuming fuel
15+
fuel--;
16+
}
17+
}
18+
}
19+
```
20+
21+
In the above example, if the car is out of fuel, calling the `Car.drive` method will do nothing.
22+
23+
## The _if-then-else_ statement
24+
25+
The _if-then-else_ statement provides an alternative path of execution for when the condition in the `if` clause evaluates to `false`.
26+
This alternative path of execution follows an `if` clause and is defined using the `else` clause:
27+
28+
```java
29+
class Car {
30+
void drive() {
31+
if (fuel > 0) {
32+
fuel--;
33+
} else {
34+
stop();
35+
}
36+
}
37+
}
38+
```
39+
40+
In the above example, if the car is out of fuel, calling the `Car.drive` method will call another method to stop the car.
41+
42+
The _if-then-else_ statement also supports multiple conditions by using the `else if` clause:
43+
44+
```java
45+
class Car {
46+
void drive() {
47+
if (fuel > 5) {
48+
fuel--;
49+
} else if (fuel > 0) {
50+
turnOnFuelLight();
51+
fuel--;
52+
} else {
53+
stop();
54+
}
55+
}
56+
}
57+
```
58+
59+
In the above example, driving the car when the fuel is less then or equal to `5` will drive the car, but it will turn on the fuel light.
60+
When the fuel reaches `0`, the car will stop driving.
61+
62+
[if-statement]: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Introduction
2+
3+
## The _if-then_ statement
4+
5+
The most basic control flow statement in Java is the _if-then_ statement.
6+
This statement is used to only execute a section of code if a particular condition is `true`.
7+
An _if-then_ statement is defined using the `if` clause:
8+
9+
```java
10+
class Car {
11+
void drive() {
12+
// the "if" clause: the car needs to have fuel left to drive
13+
if (fuel > 0) {
14+
// the "then" clause: the car drives, consuming fuel
15+
fuel--;
16+
}
17+
}
18+
}
19+
```
20+
21+
In the above example, if the car is out of fuel, calling the `Car.drive` method will do nothing.
22+
23+
## The _if-then-else_ statement
24+
25+
The _if-then-else_ statement provides an alternative path of execution for when the condition in the `if` clause evaluates to `false`.
26+
This alternative path of execution follows an `if` clause and is defined using the `else` clause:
27+
28+
```java
29+
class Car {
30+
void drive() {
31+
if (fuel > 0) {
32+
fuel--;
33+
} else {
34+
stop();
35+
}
36+
}
37+
}
38+
```
39+
40+
In the above example, if the car is out of fuel, calling the `Car.drive` method will call another method to stop the car.
41+
42+
The _if-then-else_ statement also supports multiple conditions by using the `else if` clause:
43+
44+
```java
45+
class Car {
46+
void drive() {
47+
if (fuel > 5) {
48+
fuel--;
49+
} else if (fuel > 0) {
50+
turnOnFuelLight();
51+
fuel--;
52+
} else {
53+
stop();
54+
}
55+
}
56+
}
57+
```
58+
59+
In the above example, driving the car when the fuel is less then or equal to `5` will drive the car, but it will turn on the fuel light.
60+
When the fuel reaches `0`, the car will stop driving.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[
2+
{
3+
"url": "https://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html",
4+
"description": "Java tutorial on if-then-else statements"
5+
},
6+
{
7+
"url": "https://www.javatpoint.com/java-if-else",
8+
"description": "Example if-then-else statements with flow charts"
9+
}
10+
]

concepts/numbers/.meta/config.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
{
22
"blurb": "Java includes various numeric types including integer and floating-point numbers.",
3-
"authors": [
4-
"TalesDias"
5-
],
6-
"contributors": []
3+
"authors": ["TalesDias"],
4+
"contributors": ["sanderploegsma"]
75
}

concepts/numbers/about.md

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,6 @@ double fromInt = i;
6262
int fromDouble = (int)d;
6363
```
6464

65-
An `if` statement can be used to conditionally execute code. The condition of an `if` statement must be of type `boolean`. Java has no concept of _truthy_ values.
66-
67-
```java
68-
int x = 6;
69-
70-
if (x == 5){
71-
// Execute logic if x equals 5
72-
} else if (x > 7){
73-
// Execute logic if x greater than 7
74-
} else{
75-
// Execute logic in all other cases
76-
}
77-
```
78-
7965
[arithmetic-operators]: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html
8066
[comparison-operators]: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html
8167
[type-casting]: https://www.programiz.com/java-programming/typecasting

0 commit comments

Comments
 (0)