-
-
Notifications
You must be signed in to change notification settings - Fork 689
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
5eb6aa8
commit 438b3ed
Showing
16 changed files
with
242 additions
and
252 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"blurb": "If-else statements can be used to perform conditional logic.", | ||
"authors": ["TalesDias", "sanderploegsma"], | ||
"contributors": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# About | ||
|
||
## The _if-then_ statement | ||
|
||
The most basic control flow statement in Java is the [_if-then_ statement][if-statement]. | ||
This statement is used to only execute a section of code if a particular condition is `true`. | ||
An _if-then_ statement is defined using the `if` clause: | ||
|
||
```java | ||
class Car { | ||
void drive() { | ||
// the "if" clause: the car needs to have fuel left to drive | ||
if (fuel > 0) { | ||
// the "then" clause: the car drives, consuming fuel | ||
fuel--; | ||
} | ||
} | ||
} | ||
``` | ||
|
||
In the above example, if the car is out of fuel, calling the `Car.drive` method will do nothing. | ||
|
||
## The _if-then-else_ statement | ||
|
||
The _if-then-else_ statement provides an alternative path of execution for when the condition in the `if` clause evaluates to `false`. | ||
This alternative path of execution follows an `if` clause and is defined using the `else` clause: | ||
|
||
```java | ||
class Car { | ||
void drive() { | ||
if (fuel > 0) { | ||
fuel--; | ||
} else { | ||
stop(); | ||
} | ||
} | ||
} | ||
``` | ||
|
||
In the above example, if the car is out of fuel, calling the `Car.drive` method will call another method to stop the car. | ||
|
||
The _if-then-else_ statement also supports multiple conditions by using the `else if` clause: | ||
|
||
```java | ||
class Car { | ||
void drive() { | ||
if (fuel > 5) { | ||
fuel--; | ||
} else if (fuel > 0) { | ||
turnOnFuelLight(); | ||
fuel--; | ||
} else { | ||
stop(); | ||
} | ||
} | ||
} | ||
``` | ||
|
||
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. | ||
When the fuel reaches `0`, the car will stop driving. | ||
|
||
[if-statement]: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Introduction | ||
|
||
## The _if-then_ statement | ||
|
||
The most basic control flow statement in Java is the _if-then_ statement. | ||
This statement is used to only execute a section of code if a particular condition is `true`. | ||
An _if-then_ statement is defined using the `if` clause: | ||
|
||
```java | ||
class Car { | ||
void drive() { | ||
// the "if" clause: the car needs to have fuel left to drive | ||
if (fuel > 0) { | ||
// the "then" clause: the car drives, consuming fuel | ||
fuel--; | ||
} | ||
} | ||
} | ||
``` | ||
|
||
In the above example, if the car is out of fuel, calling the `Car.drive` method will do nothing. | ||
|
||
## The _if-then-else_ statement | ||
|
||
The _if-then-else_ statement provides an alternative path of execution for when the condition in the `if` clause evaluates to `false`. | ||
This alternative path of execution follows an `if` clause and is defined using the `else` clause: | ||
|
||
```java | ||
class Car { | ||
void drive() { | ||
if (fuel > 0) { | ||
fuel--; | ||
} else { | ||
stop(); | ||
} | ||
} | ||
} | ||
``` | ||
|
||
In the above example, if the car is out of fuel, calling the `Car.drive` method will call another method to stop the car. | ||
|
||
The _if-then-else_ statement also supports multiple conditions by using the `else if` clause: | ||
|
||
```java | ||
class Car { | ||
void drive() { | ||
if (fuel > 5) { | ||
fuel--; | ||
} else if (fuel > 0) { | ||
turnOnFuelLight(); | ||
fuel--; | ||
} else { | ||
stop(); | ||
} | ||
} | ||
} | ||
``` | ||
|
||
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. | ||
When the fuel reaches `0`, the car will stop driving. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[ | ||
{ | ||
"url": "https://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html", | ||
"description": "Java tutorial on if-then-else statements" | ||
}, | ||
{ | ||
"url": "https://www.javatpoint.com/java-if-else", | ||
"description": "Example if-then-else statements with flow charts" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
{ | ||
"blurb": "Java includes various numeric types including integer and floating-point numbers.", | ||
"authors": [ | ||
"TalesDias" | ||
], | ||
"contributors": [] | ||
"authors": ["TalesDias"], | ||
"contributors": ["sanderploegsma"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.