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