Skip to content

Commit

Permalink
Simplify concept about if-statements (#2508)
Browse files Browse the repository at this point in the history
* 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
sanderploegsma authored Oct 5, 2023
1 parent 5eb6aa8 commit 438b3ed
Show file tree
Hide file tree
Showing 16 changed files with 242 additions and 252 deletions.
7 changes: 0 additions & 7 deletions concepts/conditionals-if/.meta/config.json

This file was deleted.

69 changes: 0 additions & 69 deletions concepts/conditionals-if/about.md

This file was deleted.

50 changes: 0 additions & 50 deletions concepts/conditionals-if/introduction.md

This file was deleted.

26 changes: 0 additions & 26 deletions concepts/conditionals-if/links.json

This file was deleted.

5 changes: 5 additions & 0 deletions concepts/if-else-statements/.meta/config.json
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": []
}
62 changes: 62 additions & 0 deletions concepts/if-else-statements/about.md
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
60 changes: 60 additions & 0 deletions concepts/if-else-statements/introduction.md
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.
10 changes: 10 additions & 0 deletions concepts/if-else-statements/links.json
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"
}
]
6 changes: 2 additions & 4 deletions concepts/numbers/.meta/config.json
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"]
}
14 changes: 0 additions & 14 deletions concepts/numbers/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,6 @@ double fromInt = i;
int fromDouble = (int)d;
```

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.

```java
int x = 6;

if (x == 5){
// Execute logic if x equals 5
} else if (x > 7){
// Execute logic if x greater than 7
} else{
// Execute logic in all other cases
}
```

[arithmetic-operators]: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html
[comparison-operators]: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html
[type-casting]: https://www.programiz.com/java-programming/typecasting
Expand Down
16 changes: 0 additions & 16 deletions concepts/numbers/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,3 @@ Java has two types of numeric conversions:
2. Explicit conversions: data could be lost and additional syntax in the form of a _cast_ is required.

As an `int` has less precision than a `double`, converting from an `int` to a `double` is safe and is thus an implicit conversion. However, converting from a `double` to an `int` could mean losing data, so that requires an explicit conversion.

In this exercise you must conditionally execute logic. The most common way to do this in Java is by using an `if/else` statement:

```java
int x = 6;

if (x == 5) {
// Execute logic if x equals 5
} else if (x > 7) {
// Execute logic if x greater than 7
} else {
// Execute logic in all other cases
}
```

The condition of an `if` statement must be of type `boolean`.
Loading

0 comments on commit 438b3ed

Please sign in to comment.