1 00:00:00,000 --> 00:00:04,553 [MUSIC] 2 00:00:04,553 --> 00:00:06,860 Hi everyone, this is James. 3 00:00:06,860 --> 00:00:11,446 In this C# practice session, you'll get a chance to practice writing loops and 4 00:00:11,446 --> 00:00:13,545 conditional statements using C#. 5 00:00:13,545 --> 00:00:18,820 It reinforces what you'd learned in stage three of the C# basics course. 6 00:00:18,820 --> 00:00:22,080 If you have a difficult time completing this practice session, 7 00:00:22,080 --> 00:00:24,150 you might need to review that course. 8 00:00:24,150 --> 00:00:26,123 See the teacher's notes for a link. 9 00:00:26,123 --> 00:00:28,660 I have attached a workspace to his video. 10 00:00:28,660 --> 00:00:33,370 Go ahead and open the workspace or download the project files 11 00:00:33,370 --> 00:00:38,470 if you want to use an external editor or an IDE like Visual Studio. 12 00:00:38,470 --> 00:00:42,970 The program.cs file contains a single main method. 13 00:00:42,970 --> 00:00:46,680 You'll write all of the code for your program in this method. 14 00:00:46,680 --> 00:00:50,480 To complete this practice session, read through the to-do code comments and 15 00:00:50,480 --> 00:00:52,130 complete each one. 16 00:00:52,130 --> 00:00:54,910 The purpose of this program is to prompt the user for 17 00:00:54,910 --> 00:00:59,950 a number, calculate the square of the provided number, and display the result. 18 00:01:00,980 --> 00:01:03,520 Let's see the completed program in action. 19 00:01:03,520 --> 00:01:05,830 First, we need to view the console. 20 00:01:05,830 --> 00:01:09,570 So, I'll click on View > Show Console. 21 00:01:09,570 --> 00:01:13,491 Remember, to compile a C# file, we use the mcs command. 22 00:01:17,414 --> 00:01:20,990 Then, to run the program, we use the mono command. 23 00:01:24,860 --> 00:01:28,950 See the teacher's notes, if you need a refresher on either of those commands. 24 00:01:28,950 --> 00:01:32,790 Notice that the program prompts me for a number, I'll enter 2. 25 00:01:34,470 --> 00:01:37,465 Then it displays the square of my provided number. 26 00:01:37,465 --> 00:01:43,180 2 multiplied by itself is equal to 4, we can enter another number. 27 00:01:43,180 --> 00:01:46,660 Or type the text quit to terminate execution of the program. 28 00:01:48,330 --> 00:01:52,480 Let's review each of the to-do comments in the program.cs file. 29 00:01:54,241 --> 00:01:58,751 Our first task is to declare a boolean variable named keepGoing and 30 00:01:58,751 --> 00:02:01,370 assign it a value of true. 31 00:02:01,370 --> 00:02:04,860 You use these variable to know when to exit the while loop. 32 00:02:04,860 --> 00:02:09,690 Speaking of the while loop, your next task is to define a while loop. 33 00:02:09,690 --> 00:02:14,378 You want to keep looping as long as the variable keepGoing has a value of true. 34 00:02:14,378 --> 00:02:18,494 Just inside of the while loop, you want to prompt the user with the text, 35 00:02:18,494 --> 00:02:19,448 enter a number. 36 00:02:19,448 --> 00:02:24,130 And assign their value to a string variable named entry. 37 00:02:24,130 --> 00:02:27,930 Then you'll want an if statement that checks to see if the user 38 00:02:27,930 --> 00:02:30,000 entered the text quit. 39 00:02:30,000 --> 00:02:34,430 And if they did enter the text quit, you'll want to exit the program. 40 00:02:34,430 --> 00:02:38,542 And if the user didn't enter the text quit, 41 00:02:38,542 --> 00:02:42,997 you want to parse the users entry to an integer. 42 00:02:42,997 --> 00:02:46,850 Then you want to take that number and square it. 43 00:02:46,850 --> 00:02:50,770 You can do this by multiplying the number by itself. 44 00:02:50,770 --> 00:02:54,230 Once you have the square of the number, you want to output the result. 45 00:02:54,230 --> 00:02:58,820 So for example, output the text, the square of 2 is 4. 46 00:03:01,840 --> 00:03:05,820 Or maybe 2 multiplied by itself is equal to 4. 47 00:03:07,580 --> 00:03:09,660 Whatever you want to do, the choice is yours. 48 00:03:11,070 --> 00:03:15,420 Then after exiting the loop, let's output the text goodbye 49 00:03:15,420 --> 00:03:18,710 just to let the user know that the program has finished running. 50 00:03:18,710 --> 00:03:21,230 Notice that the code comments are indented 51 00:03:21,230 --> 00:03:25,030 to reflect what the overall structure of the program should look like. 52 00:03:25,030 --> 00:03:27,930 You can use this as a guide when you're writing your code. 53 00:03:27,930 --> 00:03:31,090 If reading or writing to the console is unfamiliar to you, 54 00:03:31,090 --> 00:03:35,142 be sure to review the second stage in the C# basics course. 55 00:03:35,142 --> 00:03:36,720 Or see the teacher's notes for 56 00:03:36,720 --> 00:03:41,040 a link to another practice session that will help you practice those skills. 57 00:03:41,040 --> 00:03:46,908 Okay, there is a lot here just take it one step at a time, you've got this. 58 00:03:46,908 --> 00:03:52,116 And don't forget, practice is an important part of the learning process. 59 00:03:52,116 --> 00:03:56,490 The more that you do the better that your retention of the material will be. 60 00:03:56,490 --> 00:04:00,780 And if you get stuck at any point, see the teachers knows for hands and 61 00:04:00,780 --> 00:04:02,170 links to videos. 62 00:04:02,170 --> 00:04:06,220 That cover what you need to know in order to complete the session. 63 00:04:06,220 --> 00:04:10,010 And if you aren't able to fully complete the program don't worry. 64 00:04:10,010 --> 00:04:13,890 I'll show you how I completed the to-dos in the next video. 65 00:04:13,890 --> 00:04:15,630 Good luck and we'll see you in a bit.