avnc 017
This commit is contained in:
parent
b5daaec781
commit
3d7791fbec
@ -500,5 +500,3 @@ for (int i = 1; i <= 100; i++) {
|
|||||||
Console.WriteLine($"{i} {output}");
|
Console.WriteLine($"{i} {output}");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
---
|
|
||||||
|
749
017_Looping_logic_using_do-while_and_while/017_csharp.md
Normal file
749
017_Looping_logic_using_do-while_and_while/017_csharp.md
Normal file
@ -0,0 +1,749 @@
|
|||||||
|
# do-while and while
|
||||||
|
|
||||||
|
## Introduction
|
||||||
|
|
||||||
|
As we've said several times in previous modules featuring iteration and
|
||||||
|
decision statements, there are several techniques you can use to accomplish
|
||||||
|
similar results. Just like written and spoken languages, in programming
|
||||||
|
languages you can express the same idea in different ways. Even so, each
|
||||||
|
expression may have a nuanced difference in meaning.
|
||||||
|
|
||||||
|
The `do-while` and `while` statements allow us to control the flow of code
|
||||||
|
execution by looping through a block of code until a condition is met. When
|
||||||
|
working with the `foreach` statement, we iterate once for each item in sequence,
|
||||||
|
such as an array. The `for` statement allows us to iterate a pre-determined
|
||||||
|
number of times, and control the process of iteration. The `do-while` and
|
||||||
|
`while` statements allow us to iterate through a block of code with the intent
|
||||||
|
that the logic inside of the code block will affect when we can stop iterating.
|
||||||
|
|
||||||
|
Suppose you want to accept and process user input. You want to continue
|
||||||
|
accepting and processing input until the user presses the `q` key for "quit".
|
||||||
|
You can use the `do-while` and the `while` statements to keep iterating
|
||||||
|
through the logic to accept user input and process it until the user is ready
|
||||||
|
to stop.
|
||||||
|
|
||||||
|
In this module, you'll use the `do-while` statement and the `while` statement
|
||||||
|
to iterate through code block. You'll understand when to choose one over the
|
||||||
|
other. You'll use the continue statement to skip processing the remainder of
|
||||||
|
code in the code block and go directly to the Boolean evaluation of the `while`
|
||||||
|
statement.
|
||||||
|
|
||||||
|
By the end of this module, you will be able to confidently use the `do-while`
|
||||||
|
and `while` statements to add looping logic to your application.
|
||||||
|
|
||||||
|
### Learning objectives
|
||||||
|
|
||||||
|
In this module, you will:
|
||||||
|
|
||||||
|
- Write code that uses the `do-while` statement to iterate through a code block.
|
||||||
|
- Write code that uses the `while` statement to iterate through a code block.
|
||||||
|
- Use the `continue` statement to step directly to the Boolean evaluation.
|
||||||
|
|
||||||
|
#### Prerequisites:
|
||||||
|
|
||||||
|
- Experience using the `if` statement
|
||||||
|
- Experience using `foreach` and `for` iteration statements.
|
||||||
|
- Experience writing Boolean expressions
|
||||||
|
- Experience generating random numbers using the `System.Random` class and the
|
||||||
|
`Random.Next()` method
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Exercise
|
||||||
|
|
||||||
|
### Create do and while iteration loops
|
||||||
|
|
||||||
|
On the surface, the `do-while` and `while` statements are yet another
|
||||||
|
iteration statement that allows you to iterate through a code block and
|
||||||
|
thereby change the flow of execution of your code. However, once we examine
|
||||||
|
how each works, we can better identify the nuances of each iteration statement
|
||||||
|
and when to use them.
|
||||||
|
|
||||||
|
### What is the do-while statement?
|
||||||
|
|
||||||
|
The `do-while` statement executes a statement or a block of statements while a
|
||||||
|
specified Boolean expression evaluates to true. Because that expression is
|
||||||
|
evaluated after each execution of the loop, a do-while loop executes one or
|
||||||
|
more times.
|
||||||
|
|
||||||
|
```cs
|
||||||
|
do {
|
||||||
|
// This code executes at least one time
|
||||||
|
} while (true);
|
||||||
|
```
|
||||||
|
|
||||||
|
The flow of execution starts inside of the curly brace. The code executes at
|
||||||
|
least one time, then the Boolean expression next to the `while` keyword is
|
||||||
|
evaluated. If the Boolean expression returns `true`, the code block is executed
|
||||||
|
again.
|
||||||
|
|
||||||
|
By hard coding the Boolean expression to `true`, we've created an infinite
|
||||||
|
loop (a loop that will never end, at least, not as it's currently written). We
|
||||||
|
would need a way to break out of the loop inside of the code block. We'll
|
||||||
|
discuss the exit criteria of a `do-while` in a bit.
|
||||||
|
|
||||||
|
Okay, now let's prepare our coding environment and begin our examination of
|
||||||
|
code samples that implement a `do-while statement.
|
||||||
|
|
||||||
|
#### Prepare your coding environment
|
||||||
|
|
||||||
|
At the Terminal command prompt, to create a new console application in a
|
||||||
|
specified folder, type dotnet `new console -o ./path_to/TestProject` and then
|
||||||
|
press Enter.
|
||||||
|
|
||||||
|
### Write a do-while statement to break when a certain random number is generated
|
||||||
|
|
||||||
|
Let's write code that will keep generating random numbers between 1 and 10
|
||||||
|
until we generate the number 7. It could take just one iteration to get a 7,
|
||||||
|
or it could take dozens of iterations.
|
||||||
|
|
||||||
|
Type the following code into the code editor.
|
||||||
|
|
||||||
|
```cs
|
||||||
|
Random random = new Random();
|
||||||
|
int current = 0;
|
||||||
|
|
||||||
|
do {
|
||||||
|
current = random.Next(1, 11);
|
||||||
|
Console.WriteLine(current);
|
||||||
|
} while (current != 7);
|
||||||
|
```
|
||||||
|
|
||||||
|
At the Terminal command prompt, to run your code, type `dotnet run` and then
|
||||||
|
press Enter.
|
||||||
|
|
||||||
|
Review your output.
|
||||||
|
|
||||||
|
Since the numbers generated are random, your results will be different. However,
|
||||||
|
the value `7` will be the last number to be printed as the Boolean expression
|
||||||
|
will evaluate to `false` when 7 is generated and the flow of execution will
|
||||||
|
exit the code block.
|
||||||
|
|
||||||
|
Output
|
||||||
|

|
||||||
|
```txt
|
||||||
|
2
|
||||||
|
5
|
||||||
|
8
|
||||||
|
2
|
||||||
|
7
|
||||||
|
```
|
||||||
|
|
||||||
|
Take a minute to review your code.
|
||||||
|
|
||||||
|
A key learning for this first task is that the code block of a `do-while` loop
|
||||||
|
will execute at least once. It could iterate a large number of times, and it
|
||||||
|
is unlikely that we know ahead of time how many iterations there will be.
|
||||||
|
|
||||||
|
It's also important to notice that the code inside of the code block is
|
||||||
|
influencing whether to continue iterating through the code block or not. A
|
||||||
|
code block that influences the exit criteria is a primary reason to select a
|
||||||
|
`do-while` or `while` statements rather than one of the other iteration
|
||||||
|
statements. Both the `foreach` and `for` rely on factors that are external to
|
||||||
|
the code block to determine the number of code block iterations.
|
||||||
|
|
||||||
|
#### Write a while statement that iterates only while a random number is greater than some value
|
||||||
|
|
||||||
|
Now let's take a look at the `while` statement.
|
||||||
|
|
||||||
|
Update your code as follows:
|
||||||
|
|
||||||
|
```cs
|
||||||
|
Random random = new Random();
|
||||||
|
int current = random.Next(1, 11);
|
||||||
|
|
||||||
|
/*
|
||||||
|
do {
|
||||||
|
current = random.Next(1, 11);
|
||||||
|
Console.WriteLine(current);
|
||||||
|
} while (current != 7);
|
||||||
|
*/
|
||||||
|
|
||||||
|
while (current >= 3) {
|
||||||
|
Console.WriteLine(current);
|
||||||
|
current = random.Next(1, 11);
|
||||||
|
}
|
||||||
|
Console.WriteLine($"Last number: {current}");
|
||||||
|
```
|
||||||
|
|
||||||
|
> Note
|
||||||
|
> In this case, we position the while keyword and the Boolean expression
|
||||||
|
before the code block. This changes the meaning of the code and acts as a
|
||||||
|
"gate" to only allow the flow of execution to enter if the Boolean expression
|
||||||
|
evaluates to true.
|
||||||
|
|
||||||
|
Save your code file, and then run your code.
|
||||||
|
|
||||||
|
Enter `dotnet run` from the Terminal command prompt to run your code.
|
||||||
|
|
||||||
|
Review the output values listed.
|
||||||
|
|
||||||
|
Since the numbers are random, so your code will produce a different sequence.
|
||||||
|
|
||||||
|
Output
|
||||||
|

|
||||||
|
```txt
|
||||||
|
9
|
||||||
|
7
|
||||||
|
5
|
||||||
|
Last number: 1
|
||||||
|
```
|
||||||
|
|
||||||
|
Take a minute to review your code.
|
||||||
|
|
||||||
|
At the top code, we use `random` to initialize our `int` variable named
|
||||||
|
`current`. Our next active code line is our `while` statement.
|
||||||
|
|
||||||
|
Our `while` statement will iterate based on the Boolean expression
|
||||||
|
`(current >= 3)`. We don't know what value will be assigned to current, but
|
||||||
|
there are possibilities that affect our while loop:
|
||||||
|
|
||||||
|
- If `current` is initialized to a value greater than or equal to `3`, then the
|
||||||
|
Boolean expression will return `true`, and the flow of execution will enter
|
||||||
|
the code block. Inside the code block, the first thing that we do is write the
|
||||||
|
value of `current` to the console. Next, still inside the code block, we
|
||||||
|
update the value of `current` with a new random value. At this point, control
|
||||||
|
goes back to the top of the `while` statement where the Boolean expression is
|
||||||
|
evaluated. This process continues until the Boolean expression returns `false`
|
||||||
|
and the flow of execution breaks from the code block.
|
||||||
|
- If `current` is initialized (at the top of our code) to a value less than `3`,
|
||||||
|
then the Boolean expression will return `false`, and the code block will never
|
||||||
|
execute.
|
||||||
|
|
||||||
|
The final code line writes the value of `current` to the console. This code
|
||||||
|
runs whether the iteration code block was executed or not, and is our chance
|
||||||
|
to write the final value of `current` to the console.
|
||||||
|
|
||||||
|
#### Use the continue statement to step directly to the Boolean expression
|
||||||
|
|
||||||
|
In certain cases, we want to short-circuit the remainder of the code in the
|
||||||
|
code block and `continue` to the next iteration. We can do that using the
|
||||||
|
continue statement.
|
||||||
|
|
||||||
|
Update your code as follows:
|
||||||
|
|
||||||
|
```cs
|
||||||
|
Random random = new Random();
|
||||||
|
int current = random.Next(1, 11);
|
||||||
|
|
||||||
|
do {
|
||||||
|
current = random.Next(1, 11);
|
||||||
|
if (current >= 8) continue;
|
||||||
|
Console.WriteLine(current);
|
||||||
|
} while (current != 7);
|
||||||
|
|
||||||
|
/*
|
||||||
|
while (current >= 3) {
|
||||||
|
Console.WriteLine(current);
|
||||||
|
current = random.Next(1, 11);
|
||||||
|
}
|
||||||
|
Console.WriteLine($"Last number: {current}");
|
||||||
|
*/
|
||||||
|
```
|
||||||
|
|
||||||
|
Take a minute to review your code.
|
||||||
|
|
||||||
|
Notice that we've switched back to a `do-while`. A `do-while` ensures that the
|
||||||
|
loop will iterate at least once.
|
||||||
|
|
||||||
|
The first thing that we do inside the code block is to assign a new random
|
||||||
|
value to `current`. Next, we check to see if `current` is greater than or
|
||||||
|
equal to `8`. If this expression returns `true`, the `continue` key word will
|
||||||
|
transfer control to the end of the code block and the `while` will evaluate
|
||||||
|
`(current != 7)`. So, the loop will continue to iterate as long as the value
|
||||||
|
of `current` is not equal to `7`.
|
||||||
|
|
||||||
|
The key to this step of the exercise is the line of code containing the
|
||||||
|
`continue` key word:
|
||||||
|
|
||||||
|
```cs
|
||||||
|
if (current >= 8) continue;
|
||||||
|
```
|
||||||
|
|
||||||
|
Since our code that writes the value of `current` to the console is located
|
||||||
|
after the `if (current >= 8) continue;`, our code ensures that a value of
|
||||||
|
`current` that is greater than or equal to `8` will never be written to the
|
||||||
|
output window.
|
||||||
|
|
||||||
|
Let's try it out.
|
||||||
|
|
||||||
|
Save your code file, and then run your code.
|
||||||
|
|
||||||
|
Enter `dotnet run` from the Terminal command prompt to run your code.
|
||||||
|
|
||||||
|
Review the output values listed.
|
||||||
|
|
||||||
|
```txt
|
||||||
|
5
|
||||||
|
1
|
||||||
|
6
|
||||||
|
7
|
||||||
|
```
|
||||||
|
|
||||||
|
You'll likely see different results than what is displayed below. However, you
|
||||||
|
will not see any values `8` or greater in the output window before the code's
|
||||||
|
execution ends with the value `7`.
|
||||||
|
|
||||||
|
Consider the difference between the `continue` and `break` statements.
|
||||||
|
|
||||||
|
As you saw in this last step, the `continue` statement transfers execution to
|
||||||
|
the end of the current iteration. This behavior is different than the behavior
|
||||||
|
we saw with the `break` statement. The break statement terminates the
|
||||||
|
iteration (or `switch`) and transfers control to the statement that follows
|
||||||
|
the terminated statement. If there is no statement after the terminated
|
||||||
|
statement, then control transfers to the end of the file or method.
|
||||||
|
|
||||||
|
### Recap
|
||||||
|
|
||||||
|
There's a few important ideas you should take away from this unit:
|
||||||
|
|
||||||
|
- The `do-while` statement iterates through a code block at least once, and
|
||||||
|
might continue to iterate based on a Boolean expression. The evaluation of the
|
||||||
|
Boolean expression usually depends on some value generated or retrieved inside
|
||||||
|
of the code block.
|
||||||
|
- The `while` statement evaluates a Boolean expression first, and continues to
|
||||||
|
iterate through the code block as long as the Boolean expression evaluates to
|
||||||
|
`true`.
|
||||||
|
- The `continue` keyword to step immediately to the Boolean expression.
|
||||||
|
|
||||||
|
## Exercise
|
||||||
|
|
||||||
|
### Complete a challenge activity using do and while iteration statements
|
||||||
|
|
||||||
|
Code challenges reinforce what you learned and help you gain some confidence
|
||||||
|
before continuing on.
|
||||||
|
|
||||||
|
### Role playing game battle challenge
|
||||||
|
|
||||||
|
In some role playing games, the player's character battles non-player
|
||||||
|
characters, which are usually monsters or the "bad guys". Sometimes, a battle
|
||||||
|
consists of each character generating a random value using dice and that value
|
||||||
|
is subtracted from the opponent's health score. Once either character's health
|
||||||
|
reaches zero, they lose the game.
|
||||||
|
|
||||||
|
In this challenge, we boil down that interaction to its essence. A hero and a
|
||||||
|
monster start with the same health score. During the hero's turn, they
|
||||||
|
generate a random value that is subtracted from the monster's health. If the
|
||||||
|
monster's health is greater than zero, they take their turn and attack the hero.
|
||||||
|
As long as both the hero and the monster have health greater than zero, the
|
||||||
|
battle resumes.
|
||||||
|
|
||||||
|
### Code challenge - write code to implement the game rules
|
||||||
|
|
||||||
|
Here are the rules for the battle game that you need to implement in your code
|
||||||
|
project:
|
||||||
|
|
||||||
|
- You must use either the `do-while` statement or the `while` statement as an
|
||||||
|
outer game loop.
|
||||||
|
- The hero and the monster start with 10 health points.
|
||||||
|
- All attacks are a value between 1 and 10.
|
||||||
|
- The hero attacks first.
|
||||||
|
- Print the amount of health the monster lost and their remaining health.
|
||||||
|
- If the monster's health is greater than 0, it can attack the hero.
|
||||||
|
- Print the amount of health the hero lost and their remaining health.
|
||||||
|
- Continue this sequence of attacking until either the monster's health or
|
||||||
|
hero's health is zero or less.
|
||||||
|
- Print the winner.
|
||||||
|
|
||||||
|
Write your game code that implements each rule.
|
||||||
|
|
||||||
|
Run your application and verify that your output meets the requirements.
|
||||||
|
|
||||||
|
No matter how you do it, your code should produce a similar output:
|
||||||
|
|
||||||
|
```txt
|
||||||
|
Monster was damaged and lost 1 health and now has 9 health.
|
||||||
|
Hero was damaged and lost 1 health and now has 9 health.
|
||||||
|
Monster was damaged and lost 7 health and now has 2 health.
|
||||||
|
Hero was damaged and lost 6 health and now has 3 health.
|
||||||
|
Monster was damaged and lost 9 health and now has -7 health.
|
||||||
|
Hero wins!
|
||||||
|
```
|
||||||
|
|
||||||
|
Since the code uses random numbers and the outcome is different each time,
|
||||||
|
your results are different than the output displayed above. However, you can
|
||||||
|
use this as an example of the output your code should generate.
|
||||||
|
|
||||||
|
```cs
|
||||||
|
const int max_health = 10;
|
||||||
|
int hero = max_health;
|
||||||
|
int monster = max_health;
|
||||||
|
string turn = "Hero";
|
||||||
|
Random random = new Random();
|
||||||
|
|
||||||
|
do {
|
||||||
|
if (turn == "Hero") {
|
||||||
|
int attack = random.Next(1,11);
|
||||||
|
monster -= attack;
|
||||||
|
monster = monster < 0 ? 0 : monster;
|
||||||
|
Console.Write($@"Monster was damaged and lost {attack} health");
|
||||||
|
Console.WriteLine($@" and now has {monster} health");
|
||||||
|
turn = "Monster";
|
||||||
|
} else {
|
||||||
|
int attack = random.Next(1,11);
|
||||||
|
hero -= attack;
|
||||||
|
hero = hero < 0 ? 0 : hero;
|
||||||
|
Console.Write($@"Hero was damaged and lost {attack} health");
|
||||||
|
Console.WriteLine($@" and now has {hero} health");
|
||||||
|
turn = "Hero";
|
||||||
|
}
|
||||||
|
} while (hero > 0 && monster > 0);
|
||||||
|
|
||||||
|
Console.WriteLine(hero > monster ? "Hero wins!" : "Monster wins!");
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Exercise
|
||||||
|
|
||||||
|
### Complete a challenge activity to differentiate between do and while iteration statements
|
||||||
|
|
||||||
|
Code challenges will reinforce what you've learned and help you gain some
|
||||||
|
confidence before continuing on.
|
||||||
|
|
||||||
|
### Examine the difference between do and while statement iterations
|
||||||
|
|
||||||
|
As you have seen, C# supports four types of iteration statements: `for`,
|
||||||
|
`foreach`, `do-while`, and `while`. Microsoft's language reference
|
||||||
|
documentation describes these statements as follows:
|
||||||
|
|
||||||
|
- The `for` statement: executes its body while a specified Boolean expression
|
||||||
|
(the 'condition') evaluates to true.
|
||||||
|
- The `foreach` statement: enumerates the elements of a collection and
|
||||||
|
executes its body for each element of the collection.
|
||||||
|
- The `do-while` statement: conditionally executes its body one or more
|
||||||
|
times.
|
||||||
|
- The `while` statement: conditionally executes its body zero or more
|
||||||
|
times.
|
||||||
|
|
||||||
|
The `for` and `foreach` iterations seem to be clearly differentiated from each
|
||||||
|
other and from the `do-while` and `while` iterations. The definitions for the
|
||||||
|
`do-while` and `while` statements, however, appear to be quite similar.
|
||||||
|
Knowing when to choose between a `do-while` and a `while` seems more arbitrary,
|
||||||
|
and can even be a bit confusing. Some challenge projects may help to make the
|
||||||
|
differences clear.
|
||||||
|
|
||||||
|
In this challenge, you'll be presented with conditions for three separate
|
||||||
|
coding projects. Each project will require you to implement an iteration code
|
||||||
|
block using either a `do-while` or a `while` statement. You'll need to evaluate
|
||||||
|
the specified conditions in order to choose between the `do-while` and `while`
|
||||||
|
statements. You can switch after you start if your first choice isn't working
|
||||||
|
out as well as you had hoped.
|
||||||
|
|
||||||
|
> Note
|
||||||
|
> The conditions for your coding project can be used to help you select
|
||||||
|
between the `do`-while and `while` statements. What you know, or don't know,
|
||||||
|
about the Boolean expression that will be evaluated can sometimes help you to
|
||||||
|
select between the `do-while` and `while` statements. In this challenge
|
||||||
|
exercise, the project conditions include information that will be used to
|
||||||
|
construct the Boolean expression.
|
||||||
|
|
||||||
|
### Manage user input during this challenge
|
||||||
|
|
||||||
|
When using a `Console.ReadLine()` statement to obtain user input, it's common
|
||||||
|
practice to use a nullable type string (designated `string?`) for the input
|
||||||
|
variable and then evaluate the value entered by the user. The following code
|
||||||
|
sample uses a nullable type string to capture user input. The iteration
|
||||||
|
continues while the user-supplied value is null:
|
||||||
|
|
||||||
|
```cs
|
||||||
|
string? readResult;
|
||||||
|
Console.WriteLine("Enter a string:");
|
||||||
|
do {
|
||||||
|
readResult = Console.ReadLine();
|
||||||
|
} while (readResult == null);
|
||||||
|
```
|
||||||
|
|
||||||
|
The Boolean expression evaluated by the `while` statement can be used to
|
||||||
|
ensure user input meets a specified requirement. For example, if a prompt asks
|
||||||
|
the user to enter a string that includes at least three characters, the
|
||||||
|
following code could be used:
|
||||||
|
|
||||||
|
```cs
|
||||||
|
string? readResult;
|
||||||
|
bool validEntry = false;
|
||||||
|
Console.WriteLine("Enter a string containing at least three characters:");
|
||||||
|
do {
|
||||||
|
readResult = Console.ReadLine();
|
||||||
|
if (readResult != null) {
|
||||||
|
if (readResult.Length >= 3) {
|
||||||
|
validEntry = true;
|
||||||
|
} else {
|
||||||
|
Console.WriteLine("Your input is invalid, please try again.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} while (validEntry == false);
|
||||||
|
```
|
||||||
|
|
||||||
|
If you want to use `Console.ReadLine()` input for numeric values, you need to
|
||||||
|
convert the string value to a numeric type.
|
||||||
|
|
||||||
|
The `int.TryParse()` method can be used to convert a string value to an integer.
|
||||||
|
The method uses two parameters, a string that will be evaluated and the name
|
||||||
|
of an integer variable that will be assigned a value. The method returns a
|
||||||
|
Boolean value. The following code sample demonstrates using the `int.TryParse()`
|
||||||
|
method:
|
||||||
|
|
||||||
|
```cs
|
||||||
|
// capture user input in a string variable named readResult
|
||||||
|
|
||||||
|
int numericValue = 0;
|
||||||
|
bool validNumber = false;
|
||||||
|
|
||||||
|
validNumber = int.TryParse(readResult, out numericValue);
|
||||||
|
```
|
||||||
|
|
||||||
|
If the string value assigned to `readResult` represents a valid integer, the
|
||||||
|
value will be assigned to the integer variable named `numericValue`, and `true`
|
||||||
|
will be assigned to the Boolean variable named `validNumber`. If the value
|
||||||
|
assigned to `readResult` doesn't represent a valid integer, `validNumber` will
|
||||||
|
be assigned a value of `false`. For example, if `readResult` is equal to "7",
|
||||||
|
the value 7 will be assigned to numericValue.
|
||||||
|
|
||||||
|
### Code project 1 - write code that validates integer input
|
||||||
|
|
||||||
|
Here are the conditions that your first coding project must implement:
|
||||||
|
|
||||||
|
- Your solution must include either a `do-while` or `while` iteration.
|
||||||
|
|
||||||
|
- Before the iteration block: your solution must use a `Console.WriteLine()`
|
||||||
|
statement to prompt the user for an integer value between 5 and 10.
|
||||||
|
|
||||||
|
- Inside the iteration block:
|
||||||
|
|
||||||
|
- Your solution must use a `Console.ReadLine()` statement to obtain input
|
||||||
|
from the user.
|
||||||
|
- Your solution must ensure that the input is a valid representation of an
|
||||||
|
integer.
|
||||||
|
- If the integer value isn't between 5 and 10, your code must use a
|
||||||
|
`Console.WriteLine()` statement to prompt the user for an integer value
|
||||||
|
between 5 and 10.
|
||||||
|
- Your solution must ensure that the integer value is between 5 and 10
|
||||||
|
before exiting the iteration.
|
||||||
|
|
||||||
|
Below (after) the iteration code block: your solution must use a
|
||||||
|
`Console.WriteLine()` statement to inform the user that their input value has
|
||||||
|
been accepted.
|
||||||
|
|
||||||
|
Write the code that implements each condition for code project 1.
|
||||||
|
|
||||||
|
Run your application and verify that your code validates user input based on
|
||||||
|
the specified requirements.
|
||||||
|
|
||||||
|
For example, when you run your application, it should reject input values such
|
||||||
|
as "two" and "2", but it should accept an input value of "7".
|
||||||
|
|
||||||
|
The example described above, the console output should look similar to the
|
||||||
|
following:
|
||||||
|
|
||||||
|
```txt
|
||||||
|
Enter an integer value between 5 and 10
|
||||||
|
two
|
||||||
|
Sorry, you entered an invalid number, please try again
|
||||||
|
2
|
||||||
|
You entered 2. Please enter a number between 5 and 10.
|
||||||
|
7
|
||||||
|
Your input value (7) has been accepted.
|
||||||
|
```
|
||||||
|
|
||||||
|
### Code project 2 - write code that validates string input
|
||||||
|
|
||||||
|
Here are the conditions that your second coding project must implement:
|
||||||
|
|
||||||
|
- Your solution must include either a `do-while` or `while` iteration.
|
||||||
|
|
||||||
|
- Before the iteration block: your solution must use a `Console.WriteLine()`
|
||||||
|
statement to prompt the user for one of three role names: Administrator,
|
||||||
|
Manager, or User.
|
||||||
|
|
||||||
|
- Inside the iteration block:
|
||||||
|
|
||||||
|
- Your solution must use a `Console.ReadLine()` statement to obtain input
|
||||||
|
from the user.
|
||||||
|
- Your solution must ensure that the value entered matches one of the three
|
||||||
|
role options.
|
||||||
|
- Your solution should use the `Trim()` method on the input value to ignore
|
||||||
|
leading and trailing space characters.
|
||||||
|
- Your solution should use the `ToLower()` method on the input value to
|
||||||
|
ignore case.
|
||||||
|
- If the value entered isn't a match for one of the role options, your code
|
||||||
|
must use a `Console.WriteLine()` statement to prompt the user for a valid
|
||||||
|
entry.
|
||||||
|
|
||||||
|
- Below (after) the iteration code block: Your solution must use a
|
||||||
|
`Console.WriteLine()` statement to inform the user that their input value has
|
||||||
|
been accepted.
|
||||||
|
|
||||||
|
- Write the code that implements each condition for code project 2.
|
||||||
|
|
||||||
|
Run your application and verify that your code validates user input based on
|
||||||
|
the specified requirements.
|
||||||
|
|
||||||
|
For example, when you run your application, it should reject an input value
|
||||||
|
such as "Admin", but it should accept an input value of " administrator ".
|
||||||
|
|
||||||
|
The console output for this example should look similar to the following:
|
||||||
|
|
||||||
|
```txt
|
||||||
|
Enter your role name (Administrator, Manager, or User)
|
||||||
|
Admin
|
||||||
|
The role name that you entered, "Admin" is not valid. Enter your role name (Administrator, Manager, or User)
|
||||||
|
Administrator
|
||||||
|
Your input value (Administrator) has been accepted.
|
||||||
|
```
|
||||||
|
|
||||||
|
### Code project 3 - Write code that processes the contents of a string array
|
||||||
|
|
||||||
|
Here are the conditions that your third coding project must implement:
|
||||||
|
|
||||||
|
- Your solution must use the following string array to represent the input to
|
||||||
|
your coding logic:
|
||||||
|
|
||||||
|
```cs
|
||||||
|
string[] myStrings = new string[2] { "I like pizza. I like roast chicken. I like salad", "I like all three of the menu choices" };
|
||||||
|
```
|
||||||
|
|
||||||
|
- Your solution must declare an integer variable named `periodLocation` that
|
||||||
|
can be used to hold the location of the period character within a string.
|
||||||
|
|
||||||
|
- Your solution must include an outer `foreach` or `for` loop that can be used
|
||||||
|
to process each string element in the array. The string variable that you'll
|
||||||
|
process inside the loops should be named `myString`.
|
||||||
|
|
||||||
|
- In the outer loop, your solution must use the `IndexOf()` method of the
|
||||||
|
`String` class to get the location of the first period character in the
|
||||||
|
`myString` variable. The method call should be similar to:
|
||||||
|
`myString.IndexOf(".")`. If there's no period character in the string, a value
|
||||||
|
of -1 will be returned.
|
||||||
|
|
||||||
|
- Your solution must include an inner `do-while` or `while` loop that can be
|
||||||
|
used to process the `myString` variable.
|
||||||
|
|
||||||
|
- In the inner loop, your solution must extract and display (write to the
|
||||||
|
console) each sentence that is contained in each of the strings that are
|
||||||
|
processed.
|
||||||
|
|
||||||
|
- In the inner loop, your solution must not display the period character.
|
||||||
|
|
||||||
|
- In the inner loop, your solution must use the `Remove()`, `Substring()`, and
|
||||||
|
`TrimStart()` methods to process the string information.
|
||||||
|
|
||||||
|
Write the code that implements each condition listed for code project 3.
|
||||||
|
|
||||||
|
Run your application and verify that your output meets the requirements.
|
||||||
|
|
||||||
|
If your code logic works correctly, your output should look similar to the
|
||||||
|
following:
|
||||||
|
|
||||||
|
```txt
|
||||||
|
I like pizza
|
||||||
|
I like roast chicken
|
||||||
|
I like salad
|
||||||
|
I like all three of the menu choices
|
||||||
|
```
|
||||||
|
|
||||||
|
### Project 1 solution
|
||||||
|
|
||||||
|
```cs
|
||||||
|
string? read_input;
|
||||||
|
int input = 0;
|
||||||
|
bool valid = false;
|
||||||
|
|
||||||
|
Console.Write("Enter a value between 5 and 10: ");
|
||||||
|
do {
|
||||||
|
read_input = Console.ReadLine();
|
||||||
|
valid = int.TryParse(read_input, out input);
|
||||||
|
if (valid && input >= 5 && input <= 10){
|
||||||
|
Console.WriteLine($"Your input value ({input}) has been accepted.");
|
||||||
|
} else if(!valid) {
|
||||||
|
Console.WriteLine("Sorry, you entered an invalid number, please try again");
|
||||||
|
} else {
|
||||||
|
Console.WriteLine($"You entered {input}. Please enter a number between 5 and 10.");
|
||||||
|
valid = !valid;
|
||||||
|
}
|
||||||
|
} while (!valid);
|
||||||
|
```
|
||||||
|
|
||||||
|
### Project 2 solution
|
||||||
|
|
||||||
|
```cs
|
||||||
|
string? read_input;
|
||||||
|
string user_rol = "";
|
||||||
|
bool done = false;
|
||||||
|
string out_msg = "Enter your role name (Administrator, Manager, or User)";
|
||||||
|
|
||||||
|
do {
|
||||||
|
Console.WriteLine(out_msg);
|
||||||
|
read_input = Console.ReadLine();
|
||||||
|
if (read_input != null && read_input.Length > 3){
|
||||||
|
user_rol = read_input.ToLower().Trim();
|
||||||
|
switch (user_rol) {
|
||||||
|
case "administrator":
|
||||||
|
Console.WriteLine($"Your input value ({read_input}) has been accepted");
|
||||||
|
done = true;
|
||||||
|
break;
|
||||||
|
case "manager":
|
||||||
|
Console.WriteLine($"Your input value ({read_input}) has been accepted");
|
||||||
|
done = true;
|
||||||
|
break;
|
||||||
|
case "user":
|
||||||
|
Console.WriteLine($"Your input value ({read_input}) has been accepted");
|
||||||
|
done = true;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
Console.Write("The role name that you entered, ");
|
||||||
|
Console.WriteLine($"\"{read_input}\" is not valid. {out_msg}");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} while (!done);
|
||||||
|
```
|
||||||
|
|
||||||
|
### Project 3 solution
|
||||||
|
|
||||||
|
```cs
|
||||||
|
string[] likes = new string[2] {
|
||||||
|
"I like pizza. I like roast chicken. I like salad",
|
||||||
|
"I like all three of the menu choices"
|
||||||
|
};
|
||||||
|
|
||||||
|
int period_location;
|
||||||
|
|
||||||
|
foreach (string phrase in likes){
|
||||||
|
period_location = phrase.IndexOf(".");
|
||||||
|
string new_phrase = phrase;
|
||||||
|
while (period_location > 0){
|
||||||
|
Console.WriteLine(new_phrase.Substring(0, period_location));
|
||||||
|
new_phrase = new_phrase.Remove(0, period_location);
|
||||||
|
new_phrase = new_phrase.Remove(0, 1).TrimStart();
|
||||||
|
period_location = new_phrase.IndexOf(".");
|
||||||
|
}
|
||||||
|
Console.WriteLine(new_phrase);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Summary
|
||||||
|
|
||||||
|
Your goal was to use the `do-while` and `while` statements to perform
|
||||||
|
iterations. The `do-while` and `while` statements are unique because the body
|
||||||
|
of the code block determines whether the flow of execution should continue or
|
||||||
|
stop.
|
||||||
|
|
||||||
|
Using the `do-while` statement, you executed a block of code once before
|
||||||
|
evaluating a Boolean expression and potentially exiting the iteration. Using
|
||||||
|
the `while` statement, you performed the evaluation of the Boolean expression
|
||||||
|
immediately, and continued to evaluate it to exit the iteration. You used the
|
||||||
|
`continue` statement within the code block to step directly to the Boolean
|
||||||
|
expression.
|
||||||
|
|
||||||
|
You developed a practical application that uses the `do-while` and `continue`
|
||||||
|
statements to simulate a battle in a role playing game. Real-world scenarios
|
||||||
|
involving the `do-while` and `while` iteration statements involve working with
|
||||||
|
streams of data from files, from the internet, or any other scenario where
|
||||||
|
you'll continue to perform an iteration until a condition is met.
|
||||||
|
|
||||||
|
Without `do-while` and `while` iteration statements, it would be difficult to
|
||||||
|
write and maintain iteration code that sets the exit condition within the
|
||||||
|
iteration code block.
|
@ -0,0 +1,25 @@
|
|||||||
|
const int max_health = 10;
|
||||||
|
int hero = max_health;
|
||||||
|
int monster = max_health;
|
||||||
|
string turn = "Hero";
|
||||||
|
Random random = new Random();
|
||||||
|
|
||||||
|
do {
|
||||||
|
if (turn == "Hero") {
|
||||||
|
int attack = random.Next(1,11);
|
||||||
|
monster -= attack;
|
||||||
|
monster = monster < 0 ? 0 : monster;
|
||||||
|
Console.Write($@"Monster was damaged and lost {attack} health");
|
||||||
|
Console.WriteLine($@" and now has {monster} health");
|
||||||
|
turn = "Monster";
|
||||||
|
} else {
|
||||||
|
int attack = random.Next(1,11);
|
||||||
|
hero -= attack;
|
||||||
|
hero = hero < 0 ? 0 : hero;
|
||||||
|
Console.Write($@"Hero was damaged and lost {attack} health");
|
||||||
|
Console.WriteLine($@" and now has {hero} health");
|
||||||
|
turn = "Hero";
|
||||||
|
}
|
||||||
|
} while (hero > 0 && monster > 0);
|
||||||
|
|
||||||
|
Console.WriteLine(hero > monster ? "Hero wins!" : "Monster wins!");
|
@ -0,0 +1,10 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
@ -0,0 +1,47 @@
|
|||||||
|
string? read_input;
|
||||||
|
int input = 0;
|
||||||
|
bool valid = false;
|
||||||
|
|
||||||
|
Console.Write("Enter an integer value between 5 and 10: ");
|
||||||
|
do {
|
||||||
|
read_input = Console.ReadLine();
|
||||||
|
valid = int.TryParse(read_input, out input);
|
||||||
|
if (valid && input >= 5 && input <= 10){
|
||||||
|
Console.WriteLine($"Your input value ({input}) has been accepted.");
|
||||||
|
} else if(!valid) {
|
||||||
|
Console.WriteLine("Sorry, you entered an invalid number, please try again");
|
||||||
|
} else {
|
||||||
|
Console.WriteLine($"You entered {input}. Please enter a number between 5 and 10.");
|
||||||
|
valid = !valid;
|
||||||
|
}
|
||||||
|
} while (!valid);
|
||||||
|
|
||||||
|
// Propoussed Solution
|
||||||
|
// string? readResult;
|
||||||
|
// string valueEntered = "";
|
||||||
|
// int numValue = 0;
|
||||||
|
// bool validNumber = false;
|
||||||
|
//
|
||||||
|
// Console.WriteLine("Enter an integer value between 5 and 10");
|
||||||
|
//
|
||||||
|
// do {
|
||||||
|
// readResult = Console.ReadLine();
|
||||||
|
// if (readResult != null) {
|
||||||
|
// valueEntered = readResult;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// validNumber = int.TryParse(valueEntered, out numValue);
|
||||||
|
//
|
||||||
|
// if (validNumber == true) {
|
||||||
|
// if (numValue <= 5 || numValue >= 10) {
|
||||||
|
// validNumber = false;
|
||||||
|
// Console.WriteLine($"You entered {numValue}. Please enter a number between 5 and 10.");
|
||||||
|
// }
|
||||||
|
// } else {
|
||||||
|
// Console.WriteLine("Sorry, you entered an invalid number, please try again");
|
||||||
|
// }
|
||||||
|
// } while (validNumber == false);
|
||||||
|
//
|
||||||
|
// Console.WriteLine($"Your input value ({numValue}) has been accepted.");
|
||||||
|
//
|
||||||
|
// readResult = Console.ReadLine();
|
@ -0,0 +1,10 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
@ -0,0 +1,51 @@
|
|||||||
|
string? read_input;
|
||||||
|
string user_rol = "";
|
||||||
|
bool done = false;
|
||||||
|
string out_msg = "Enter your role name (Administrator, Manager, or User)";
|
||||||
|
|
||||||
|
do {
|
||||||
|
Console.WriteLine(out_msg);
|
||||||
|
read_input = Console.ReadLine();
|
||||||
|
if (read_input != null && read_input.Length > 3){
|
||||||
|
user_rol = read_input.ToLower().Trim();
|
||||||
|
switch (user_rol) {
|
||||||
|
case "administrator":
|
||||||
|
Console.WriteLine($"Your input value ({read_input}) has been accepted");
|
||||||
|
done = true;
|
||||||
|
break;
|
||||||
|
case "manager":
|
||||||
|
Console.WriteLine($"Your input value ({read_input}) has been accepted");
|
||||||
|
done = true;
|
||||||
|
break;
|
||||||
|
case "user":
|
||||||
|
Console.WriteLine($"Your input value ({read_input}) has been accepted");
|
||||||
|
done = true;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
Console.Write("The role name that you entered, ");
|
||||||
|
Console.WriteLine($"\"{read_input}\" is not valid. {out_msg}");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} while (!done);
|
||||||
|
|
||||||
|
// Propoussed Solution
|
||||||
|
// string? readResult;
|
||||||
|
// string roleName = "";
|
||||||
|
// bool validEntry = false;
|
||||||
|
//
|
||||||
|
// do {
|
||||||
|
// Console.WriteLine("Enter your role name (Administrator, Manager, or User)");
|
||||||
|
// readResult = Console.ReadLine();
|
||||||
|
// if (readResult != null) {
|
||||||
|
// roleName = readResult.Trim();
|
||||||
|
// }
|
||||||
|
// if (roleName.ToLower() == "administrator" || roleName.ToLower() == "manager" || roleName.ToLower() == "user") {
|
||||||
|
// validEntry = true;
|
||||||
|
// } else {
|
||||||
|
// Console.Write($"The role name that you entered, \"{roleName}\" is not valid. ");
|
||||||
|
// }
|
||||||
|
// } while (validEntry == false);
|
||||||
|
//
|
||||||
|
// Console.WriteLine($"Your input value ({roleName}) has been accepted.");
|
||||||
|
// readResult = Console.ReadLine();
|
@ -0,0 +1,10 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
@ -0,0 +1,48 @@
|
|||||||
|
string[] likes = new string[2] {
|
||||||
|
"I like pizza. I like roast chicken. I like salad",
|
||||||
|
"I like all three of the menu choices"
|
||||||
|
};
|
||||||
|
|
||||||
|
int period_location;
|
||||||
|
|
||||||
|
foreach (string phrase in likes){
|
||||||
|
period_location = phrase.IndexOf(".");
|
||||||
|
string new_phrase = phrase;
|
||||||
|
while (period_location > 0){
|
||||||
|
Console.WriteLine(new_phrase.Substring(0, period_location));
|
||||||
|
new_phrase = new_phrase.Remove(0, period_location);
|
||||||
|
new_phrase = new_phrase.Remove(0, 1).TrimStart();
|
||||||
|
period_location = new_phrase.IndexOf(".");
|
||||||
|
}
|
||||||
|
Console.WriteLine(new_phrase);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Propoussed Solution
|
||||||
|
// string[] myStrings = new string[2] {
|
||||||
|
// "I like pizza. I like roast chicken. I like salad",
|
||||||
|
// "I like all three of the menu choices"
|
||||||
|
// };
|
||||||
|
// int stringsCount = myStrings.Length;
|
||||||
|
// string myString = "";
|
||||||
|
// int periodLocation = 0;
|
||||||
|
//
|
||||||
|
// for (int i = 0; i < stringsCount; i++) {
|
||||||
|
// myString = myStrings[i];
|
||||||
|
// periodLocation = myString.IndexOf(".");
|
||||||
|
// string mySentence;
|
||||||
|
// // extract sentences from each string and display them one at a time
|
||||||
|
// while (periodLocation != -1) {
|
||||||
|
// // first sentence is the string value to the left of the period location
|
||||||
|
// mySentence = myString.Remove(periodLocation);
|
||||||
|
// // the remainder of myString is the string value to the right of the location
|
||||||
|
// myString = myString.Substring(periodLocation + 1);
|
||||||
|
// // remove any leading white-space from myString
|
||||||
|
// myString = myString.TrimStart();
|
||||||
|
// // update the comma location and increment the counter
|
||||||
|
// periodLocation = myString.IndexOf(".");
|
||||||
|
//
|
||||||
|
// Console.WriteLine(mySentence);
|
||||||
|
// }
|
||||||
|
// mySentence = myString.Trim();
|
||||||
|
// Console.WriteLine(mySentence);
|
||||||
|
// }
|
@ -0,0 +1,10 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
@ -0,0 +1,31 @@
|
|||||||
|
string? readResult;
|
||||||
|
Console.WriteLine("Enter a string:");
|
||||||
|
do {
|
||||||
|
readResult = Console.ReadLine().Trim();
|
||||||
|
} while (readResult == null);
|
||||||
|
|
||||||
|
Console.WriteLine("\n-----------------------------------\n");
|
||||||
|
|
||||||
|
string? readResult2;
|
||||||
|
bool validEntry = false;
|
||||||
|
Console.WriteLine("Enter a string containing at least three characters:");
|
||||||
|
do {
|
||||||
|
readResult2 = Console.ReadLine();
|
||||||
|
if (readResult2 != null) {
|
||||||
|
if (readResult2.Length >= 3) {
|
||||||
|
validEntry = true;
|
||||||
|
} else {
|
||||||
|
Console.WriteLine("Your input is invalid, please try again.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} while (validEntry == false);
|
||||||
|
|
||||||
|
Console.WriteLine("\n-----------------------------------\n");
|
||||||
|
|
||||||
|
// capture user input in a string variable named readResult
|
||||||
|
|
||||||
|
int numericValue = 0;
|
||||||
|
bool validNumber = false;
|
||||||
|
|
||||||
|
validNumber = int.TryParse(readResult2, out numericValue);
|
||||||
|
Console.WriteLine($"valid num: {validNumber}");
|
@ -0,0 +1,10 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
@ -20,4 +20,5 @@ Following
|
|||||||
- [Evaluate Boolean expressions](/013_eval_boolean_expressions/013_csharp.md)
|
- [Evaluate Boolean expressions](/013_eval_boolean_expressions/013_csharp.md)
|
||||||
- [Control variable scope and logic](/014_scope_and_logic/014_csharp.md)
|
- [Control variable scope and logic](/014_scope_and_logic/014_csharp.md)
|
||||||
- [switch case construct](./015_switch_case/015_csharp.md)
|
- [switch case construct](./015_switch_case/015_csharp.md)
|
||||||
- [switch case construct](/016_Iterate_using_for_statement/016_csharp.md)
|
- [FizzBuzz Challenge](./016_Iterate_using_for_statement/016_csharp.md)
|
||||||
|
- [Looping logic using the do-while and while](./017_Looping_logic_using_do-while_and_while/017_csharp.md)
|
||||||
|
Loading…
Reference in New Issue
Block a user