Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
In this video, we'll explain the challenge that you'll be working to complete.
Prerequisites
This practice session assumes that you've completed stage 3 of the C# Basics course. If you haven't done that yet, go check it out, then come back to this workshop.
This practice session also builds upon the skills that are covered in the Practice Basic Variables, Input, and Output in C# practice session that covers stage 2 of the C# Basics course. If you haven't completed this practice session yet, you might consider doing that first before attempting this practice session.
Help
If you get stuck on any of the following topics or simply need a refresher, click on a topic in list below to view the associated video in the C# Basics course.
-
0:00
[MUSIC]
-
0:04
Hi everyone, this is James.
-
0:06
In this C# practice session, you'll get a chance to practice writing loops and
-
0:11
conditional statements using C#.
-
0:13
It reinforces what you'd learned in stage three of the C# basics course.
-
0:18
If you have a difficult time completing this practice session,
-
0:22
you might need to review that course.
-
0:24
See the teacher's notes for a link.
-
0:26
I have attached a workspace to his video.
-
0:28
Go ahead and open the workspace or download the project files
-
0:33
if you want to use an external editor or an IDE like Visual Studio.
-
0:38
The program.cs file contains a single main method.
-
0:42
You'll write all of the code for your program in this method.
-
0:46
To complete this practice session, read through the to-do code comments and
-
0:50
complete each one.
-
0:52
The purpose of this program is to prompt the user for
-
0:54
a number, calculate the square of the provided number, and display the result.
-
1:00
Let's see the completed program in action.
-
1:03
First, we need to view the console.
-
1:05
So, I'll click on View > Show Console.
-
1:09
Remember, to compile a C# file, we use the mcs command.
-
1:17
Then, to run the program, we use the mono command.
-
1:24
See the teacher's notes, if you need a refresher on either of those commands.
-
1:28
Notice that the program prompts me for a number, I'll enter 2.
-
1:34
Then it displays the square of my provided number.
-
1:37
2 multiplied by itself is equal to 4, we can enter another number.
-
1:43
Or type the text quit to terminate execution of the program.
-
1:48
Let's review each of the to-do comments in the program.cs file.
-
1:54
Our first task is to declare a boolean variable named keepGoing and
-
1:58
assign it a value of true.
-
2:01
You use these variable to know when to exit the while loop.
-
2:04
Speaking of the while loop, your next task is to define a while loop.
-
2:09
You want to keep looping as long as the variable keepGoing has a value of true.
-
2:14
Just inside of the while loop, you want to prompt the user with the text,
-
2:18
enter a number.
-
2:19
And assign their value to a string variable named entry.
-
2:24
Then you'll want an if statement that checks to see if the user
-
2:27
entered the text quit.
-
2:30
And if they did enter the text quit, you'll want to exit the program.
-
2:34
And if the user didn't enter the text quit,
-
2:38
you want to parse the users entry to an integer.
-
2:42
Then you want to take that number and square it.
-
2:46
You can do this by multiplying the number by itself.
-
2:50
Once you have the square of the number, you want to output the result.
-
2:54
So for example, output the text, the square of 2 is 4.
-
3:01
Or maybe 2 multiplied by itself is equal to 4.
-
3:07
Whatever you want to do, the choice is yours.
-
3:11
Then after exiting the loop, let's output the text goodbye
-
3:15
just to let the user know that the program has finished running.
-
3:18
Notice that the code comments are indented
-
3:21
to reflect what the overall structure of the program should look like.
-
3:25
You can use this as a guide when you're writing your code.
-
3:27
If reading or writing to the console is unfamiliar to you,
-
3:31
be sure to review the second stage in the C# basics course.
-
3:35
Or see the teacher's notes for
-
3:36
a link to another practice session that will help you practice those skills.
-
3:41
Okay, there is a lot here just take it one step at a time, you've got this.
-
3:46
And don't forget, practice is an important part of the learning process.
-
3:52
The more that you do the better that your retention of the material will be.
-
3:56
And if you get stuck at any point, see the teachers knows for hands and
-
4:00
links to videos.
-
4:02
That cover what you need to know in order to complete the session.
-
4:06
And if you aren't able to fully complete the program don't worry.
-
4:10
I'll show you how I completed the to-dos in the next video.
-
4:13
Good luck and we'll see you in a bit.
You need to sign up for Treehouse in order to download course files.
Sign up