This practice will be retired on May 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
In this video, we'll explain the solution to the challenge.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Welcome back, how did you do?
0:00
Don't worry if you weren't
able to complete every task.
0:02
It's not unusual to struggle
when learning something new.
0:05
Let's walk through my solution.
0:08
For my first task, we need to declare
a boolean variable named keepGoing and
0:10
assign it the value of true.
0:15
So, bool for
the data type then the name of
0:16
the variable keepGoing=true.
0:21
Next, we need you to define the while
loop, to do this we just type the word
0:27
while followed by a set of
parenthesis then a code block.
0:32
Inside of the parentheses we need to
supply the condition that will be used to
0:39
evaluate if the loop should be executed or
not.
0:43
For our situation we just need to
provide the boolean variable keepGoing.
0:46
And because we're initializing
this variable to true,
0:51
our wire loop will execute at least once.
0:54
Now let's move these to-do
comments inside of our loop.
0:57
Be sure not to move the last one.
1:01
That one needs to stay
outside of the wire loop.
1:03
I'll press Cmd + X or Ctrl +X to cut.
1:05
Then I'll move my cursor up and
then Ctrl+V or Cmd+V to paste
1:10
Just inside of our while loop,
we need to prompt the user with the text,
1:20
Enter a number.
1:23
To write text to the console,
we can use the Console.Write method.
1:25
We pass into the Write method the text
we want to write to the console.
1:31
So, Enter a number.
1:35
Now we need to read the users value and
store it in a string variable named entry.
1:39
First the variable, so string entry =,
1:44
then to read the value
we use Console.ReadLine.
1:49
Now we need to check to see if
the user entered the text quit.
1:55
Sounds like an if statement,
so if (entry ==
2:00
Then the string literal quit), then a set
of curly braces to define a code block.
2:07
I'll move this TODO Exit the program
inside of that code block,
2:16
because we only want to do that if
this if statement evaluates to true.
2:20
To exit the program, we need to set
the keepGoing variable to false.
2:24
Setting the keepGoing variable to
false will cause the loop to exit
2:30
on the next go around.
2:34
For our next TODO,
2:35
we want to run this other code,
if the user didn't enter the text quit.
2:36
To do that,
we can use an else statement, else, and
2:41
then followed by a set of curly braces.
2:45
Then let's move these TODO comments
inside of that code block.
2:49
To review, if the entry variable is equal
to quit, we will keepGoing to false.
2:54
Else, we will do these other tasks, okay.
3:01
So if the user isn't trying
to quite the program,
3:05
we now need to parse the user's
entry to an integer.
3:08
LetΒ΄s start with our variable, so int for
3:12
the data type followed
by the variable name.
3:15
I'll just use number for that.
3:18
To parse a string to an integer, we can
use the int data types parse method.
3:21
We pass into the parse method
the string that we want to parse,
3:28
in our case that's the entry variable.
3:32
Now that we have a number we can
multiply that number by itself
3:36
to determine the square.
3:39
We need another variable, so int result =
3:41
number, and then an asterisk
to do multiplication, number.
3:46
So that's number times number.
3:53
And then we store the result
in the result variable.
3:56
Now we need to output
the result to the console.
4:01
Sounds like a call to
the Console.WriteLine method.
4:04
We can use whatever text
we want to use here.
4:11
I'll use the second one, so
2 multiply by itself is equal to 4.
4:14
So first, we need to start we
the users provided number.
4:18
So I'll use number +, to concatenate
number with a string literal.
4:22
Inside of the quotes,
we'll type multiplied by itself
4:28
is equal to, then another plus sign.
4:35
Now we need our result, so result.
4:40
Then I'll add another plus sign
followed by another set of quotes so
4:43
I can add a period at
the end of that sentence.
4:47
For our last task, we just need to output
the text Goodbye after exiting the loop.
4:53
So, Console.WriteLine,
then the text Goodbye!
4:59
All right, now let's save the file.
5:09
You can do that by pressing Ctrl+S or
Cmd+S on the Mac, or
5:11
select the File > Save menu item.
5:15
Now, let's compile and run our program.
5:23
Show the console by selecting
the View > Show Console menu item.
5:25
And run the command
5:31
mcs Program.cs.
5:35
Now we can run the program using
the command mono space program.exe.
5:40
Here's the prompt asking me for
a number ,I'm enter in the number 4.
5:47
And press the Enter, and
here is my number squared.
5:51
4 multiplied by itself is equal to 16.
5:55
Let's try another number, how about 6?
5:57
6 multiplied by itself is equal to 36.
6:01
Now I'll type quit to exit the program.
6:04
And we see our message,
Goodbye!, and that's it.
6:08
Keep practicing, and
we'll see you next time.
6:11
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up