Skip to content

Commit

Permalink
Update while and add tag system (#666)
Browse files Browse the repository at this point in the history
* Update while loops concept

* Add tag system
  • Loading branch information
meatball133 authored Aug 7, 2024
1 parent 85685cf commit cd29253
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 46 deletions.
4 changes: 2 additions & 2 deletions concepts/return/about.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Return

In Crystal, the last expression in a method returns its value.
Although sometimes you need to return a value before the last expression.
For this, you can use the `return` keyword, which will return the value of the expression that follows it.
However, sometimes you need to return a value before the last expression.
For this, you can use the `return` keyword, which will return the value of the following expression.
Code written after a `return` keyword will not be executed.

```crystal
Expand Down
4 changes: 2 additions & 2 deletions concepts/return/introduction.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Return

In Crystal, the last expression in a method returns its value.
Although sometimes you need to return a value before the last expression.
For this, you can use the `return` keyword, which will return the value of the expression that follows it.
However, sometimes you need to return a value before the last expression.
For this, you can use the `return` keyword, which will return the value of the following expression.
Code written after a `return` keyword will not be executed.

```crystal
Expand Down
24 changes: 12 additions & 12 deletions concepts/while-loops/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ A loop is a control structure that allows code to be executed repeatedly based o
## While Loops

A while loop is a control structure that allows code to be executed repeatedly based on a given condition.
The code within a while loop will continue to execute while the condition evaluates to truthy.
While loops are often used when the number of iterations is not known beforehand but can be used in any situation where a loop is needed.
The code within a while loop will continue executing while the condition evaluates truthy.
While loops are often used when the number of iterations is unknown beforehand, they can be used in any situation where a loop is needed.

The condition is evaluated before the code within the loop is executed, which means that if the condition is false the code within the loop will never be executed.
The condition is evaluated before the code within the loop is executed, which means that if the condition is false, the code within the loop will never be executed.

```crystal
i = 0
Expand Down Expand Up @@ -56,11 +56,11 @@ end

### Infinite loops

A common error with while loops is getting into an "infinite" loop: a loop that never exits.
The condition of an infinite loop _never_ evaluates to a falsey value.
A common error with while loops is entering an "infinite loop," which never exits.
An infinite loop can be caused by a condition _never_ evaluates to truthy or falsey.
This is often due to programmer error; for example, forgetting to increment the loop variable.
But sometimes an infinite loop is the clearest way to implement the logic of the program.
Or it is truly necessary to loop forever; for example, an HTTP server waits for an incoming connection, handles it, then waits for the next connection.
But sometimes, an infinite loop is the clearest way to implement the logic of the program.
Or it is vital to loop forever; for example, an HTTP server waits for an incoming connection, handles it, and then waits for the next connection.

## Break

Expand All @@ -85,7 +85,7 @@ end
## Next

The `next` keyword can be used to skip to the next iteration of a loop.
This can be convenient when you want to skip certain iterations of a loop.
This can be convenient when you want to skip specific iterations of a loop.

```crystal
i = 0
Expand All @@ -106,10 +106,10 @@ end

The type of the variable can be different under looping.
In the following example, the type of `a` is `Int32 | String` because it can be `Int32` or `String` under the first part of the loop.
The variable `a` will only carry the type `Int32 | String` under the first part of the loop, since when the loop starts it holds `Int32` and under the 2nd iteration it holds `String`.
Exactly what this multiple type system is, will be explained in later concepts.
But since we are sure that under the 2nd part of the loop it will only hold `String`, the type of `a` is `String` under the 2nd part of the loop.
It will never hold `Int32` and `String` at the same time in runtime.
The variable `a` will only carry the type `Int32 | String` in the first part of the loop since when the loop starts, it holds `Int32`, and in the second iteration, it holds a `String`.
This multiple-type system will be explained in detail later.
But since we are sure that the second part of the loop will only hold `String`, the type of `a' is `String` under the second part of the loop.
At runtime, it will never simultaneously hold `Int32` and `String`.

```crystal
i = 0
Expand Down
24 changes: 12 additions & 12 deletions concepts/while-loops/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ A loop is a control structure that allows code to be executed repeatedly based o
## While Loops

A while loop is a control structure that allows code to be executed repeatedly based on a given condition.
The code within a while loop will continue to execute while the condition evaluates to truthy.
While loops are often used when the number of iterations is not known beforehand but can be used in any situation where a loop is needed.
The code within a while loop will continue executing while the condition evaluates truthy.
While loops are often used when the number of iterations is unknown beforehand, they can be used in any situation where a loop is needed.

The condition is evaluated before the code within the loop is executed, which means that if the condition is false the code within the loop will never be executed.
The condition is evaluated before the code within the loop is executed, which means that if the condition is false, the code within the loop will never be executed.

```crystal
i = 0
Expand Down Expand Up @@ -56,11 +56,11 @@ end

### Infinite loops

A common error with while loops is getting into an "infinite" loop: a loop that never exits.
The condition of an infinite loop _never_ evaluates to a falsey value.
A common error with while loops is entering an "infinite loop," which never exits.
An infinite loop can be caused by a condition _never_ evaluates to truthy or falsey.
This is often due to programmer error; for example, forgetting to increment the loop variable.
But sometimes an infinite loop is the clearest way to implement the logic of the program.
Or it is truly necessary to loop forever; for example, an HTTP server waits for an incoming connection, handles it, then waits for the next connection.
But sometimes, an infinite loop is the clearest way to implement the logic of the program.
Or it is vital to loop forever; for example, an HTTP server waits for an incoming connection, handles it, and then waits for the next connection.

## Break

Expand All @@ -85,7 +85,7 @@ end
## Next

The `next` keyword can be used to skip to the next iteration of a loop.
This can be convenient when you want to skip certain iterations of a loop.
This can be convenient when you want to skip specific iterations of a loop.

```crystal
i = 0
Expand All @@ -106,10 +106,10 @@ end

The type of the variable can be different under looping.
In the following example, the type of `a` is `Int32 | String` because it can be `Int32` or `String` under the first part of the loop.
The variable `a` will only carry the type `Int32 | String` under the first part of the loop, since when the loop starts it holds `Int32` and under the 2nd iteration it holds `String`.
Exactly what this multiple type system is, will be explained in later concepts.
But since we are sure that under the 2nd part of the loop it will only hold `String`, the type of `a` is `String` under the 2nd part of the loop.
It will never hold `Int32` and `String` at the same time in runtime.
The variable `a` will only carry the type `Int32 | String` in the first part of the loop since when the loop starts, it holds `Int32`, and in the second iteration, it holds a `String`.
This multiple-type system will be explained in detail later.
But since we are sure that the second part of the loop will only hold `String`, the type of `a' is `String` under the second part of the loop.
At runtime, it will never simultaneously hold `Int32` and `String`.

```crystal
i = 0
Expand Down
28 changes: 14 additions & 14 deletions exercises/concept/interest-is-interesting/.docs/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ A loop is a control structure that allows code to be executed repeatedly based o
## While Loops

A while loop is a control structure that allows code to be executed repeatedly based on a given condition.
The code within a while loop will continue to execute while the condition evaluates to truthy.
While loops are often used when the number of iterations is not known beforehand but can be used in any situation where a loop is needed.
The code within a while loop will continue executing while the condition evaluates truthy.
While loops are often used when the number of iterations is unknown beforehand, they can be used in any situation where a loop is needed.

The condition is evaluated before the code within the loop is executed, which means that if the condition is false the code within the loop will never be executed.
The condition is evaluated before the code within the loop is executed, which means that if the condition is false, the code within the loop will never be executed.

```crystal
i = 0
Expand Down Expand Up @@ -56,11 +56,11 @@ end

### Infinite loops

A common error with while loops is getting into an "infinite" loop: a loop that never exits.
The condition of an infinite loop _never_ evaluates to a falsey value.
A common error with while loops is entering an "infinite loop," which never exits.
An infinite loop can be caused by a condition _never_ evaluates to truthy or falsey.
This is often due to programmer error; for example, forgetting to increment the loop variable.
But sometimes an infinite loop is the clearest way to implement the logic of the program.
Or it is truly necessary to loop forever; for example, an HTTP server waits for an incoming connection, handles it, then waits for the next connection.
But sometimes, an infinite loop is the clearest way to implement the logic of the program.
Or it is vital to loop forever; for example, an HTTP server waits for an incoming connection, handles it, and then waits for the next connection.

## Break

Expand All @@ -85,7 +85,7 @@ end
## Next

The `next` keyword can be used to skip to the next iteration of a loop.
This can be convenient when you want to skip certain iterations of a loop.
This can be convenient when you want to skip specific iterations of a loop.

```crystal
i = 0
Expand All @@ -106,10 +106,10 @@ end

The type of the variable can be different under looping.
In the following example, the type of `a` is `Int32 | String` because it can be `Int32` or `String` under the first part of the loop.
The variable `a` will only carry the type `Int32 | String` under the first part of the loop, since when the loop starts it holds `Int32` and under the 2nd iteration it holds `String`.
Exactly what this multiple type system is, will be explained in later concepts.
But since we are sure that under the 2nd part of the loop it will only hold `String`, the type of `a` is `String` under the 2nd part of the loop.
It will never hold `Int32` and `String` at the same time in runtime.
The variable `a` will only carry the type `Int32 | String` in the first part of the loop since when the loop starts, it holds `Int32`, and in the second iteration, it holds a `String`.
This multiple-type system will be explained in detail later.
But since we are sure that the second part of the loop will only hold `String`, the type of `a' is `String` under the second part of the loop.
At runtime, it will never simultaneously hold `Int32` and `String`.

```crystal
i = 0
Expand All @@ -127,8 +127,8 @@ end
# Return

In Crystal, the last expression in a method returns its value.
Although sometimes you need to return a value before the last expression.
For this, you can use the `return` keyword, which will return the value of the expression that follows it.
However, sometimes you need to return a value before the last expression.
For this, you can use the `return` keyword, which will return the value of the following expression.
Code written after a `return` keyword will not be executed.

```crystal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ require "spec"
require "../src/*"

describe SavingsAccount do
describe "interest_rate" do
describe "interest_rate", tags: "task_id=1" do
it "Returns minimum interest rate" do
SavingsAccount.interest_rate(0.0).should eq(0.5)
end
Expand Down Expand Up @@ -52,7 +52,7 @@ describe SavingsAccount do
end
end

describe "interest" do
describe "interest", tags: "task_id=2" do
it "Returns intrest on negative balance" do
SavingsAccount.interest(-10000.0).should be_close(-321.3, 0.001)
end
Expand All @@ -70,7 +70,7 @@ describe SavingsAccount do
end
end

describe "annual_balance_update" do
describe "annual_balance_update", tags: "task_id=3" do
it "Returns zero for empty start balance" do
SavingsAccount.annual_balance_update(0.0).should be_close(0.0, 0.001)
end
Expand All @@ -96,7 +96,7 @@ describe SavingsAccount do
end
end

describe "years_before_desired_balance" do
describe "years_before_desired_balance", tags: "task_id=4" do
it "Returns years before desired balance on small start balance" do
SavingsAccount.years_before_desired_balance(100.0, 125.80).should eq(47)
end
Expand Down

0 comments on commit cd29253

Please sign in to comment.