Download and watch the episode here:
Coding 101 2
Shannon Morse: Today on Coding 101 you’re going to learn all about variables, While Loops, and we’re going to convert integers to binary.
Intro: Netcasts you love, from people you trust. This is TWiT.
Ads: Bandwidth for Coding 101 is provided by CacheFly at c-a-c-h-e-f-l-y-dot-com. This episode is brought to you by lynda.com, learn what you want, when you want, with access to over 2000 high-quality online courses and training videos, all for one low monthly price. To try it free for 7 days, visit lyda.com/c101, that’s l-y-n-d-a-dot-com slash c-101.
Fr. Robert Ballecer: Welcome to Coding 101, it’s the TWiT show that gives you the knowledge to live in the wonderful world of the programmer. I’m Fr. Robert Ballecer.
Shannon: And I’m Shannon Morse, and for the next 30 minutes we’re going to get you learned up on everything having to do with the Code Monkeys.
Fr. Robert: That’s right Shannon. Now we know that the key to becoming an expert is repetition. Now, so is there anything that you think our Code Monkeys need to know to move up a level?
Shannon: Quite a bit, actually. So we did get a whole bunch of feedback from last week’s show about basically “how do we get started?”, you know, we really want to enjoy this stuff, but I really need to know how to download the program and everything else that has to do with getting started in C Sharp (C#). So today, I wanted to go ahead and start the show by giving you a little bit of background about how to actually get started downloading and then copy and pasting the code from GitHub.
Fr. Robert: I love it. Let’s get to it.
Shannon: OK. I went ahead and pulled up everything on my computer, if you look over here. First off, I’m going to show you the website. This is where you can download Visual Studio Express for the Windows Desktop — that’s the specific one that we’re going to use. It’s totally free; there’s no 60-day thing or anything like that. You just download it right here — you can sign in. If you do sign in on here with your Windows password and username, then it’ll actually sync everything that you have customized in the program from one computer to another. So every time you open Visual Studio on any computer, it’ll all be the same.
Fr. Robert: Visual Express is free for everyone, but if you’re a student — if you can show a student ID — you can actually get the Professional edition for free. If you’re in highschool; if you’re in university, go ahead and choose the Visual Studio Professional for students — it’ll ask you a little questionnaire and ask for, I think they want a scan of your ID, but then you’re good.
Shannon: I wonder if I still have mine from university back in the day. I still look about the same.
Fr. Robert: I know, I was thinking about that. I’m kind of a student of life. Right?
Shannon: Yeah, I’m a student of life, exactly. So once you have this downloaded it’s a very easy process to get everything downloaded. You just go through the online steps, and then this is what the program looks like when you first open it. So I just opened this from my Start Menu — it’s right over here. So I clicked on that, and that’s what it looks like. Now, we are talking about copying the code from our first episode, Episode 1, from GitHub.
Fr. Robert: I’m sorry about that, folks, I know that there was much frustration, very angry, wow, from people who were saying “Oh my God. I copying and pasted the code--” Yeah, that was my fault. I didn’t realize that our Drupal backend on the TWiT website wasn’t going to play nice.
Shannon: Not the best place to put actual code.
Fr. Robert: Right. So because of some very brilliant people that we’ve got in the chat room, in my Twitter account and Google+, we moved everything over to GitHub — that’s where we’re going to be putting every project from here on in. You’ll be able to find each and every single episode, the homework, the code, the whole deal. And the nice thing is, if you go in and click that, it will show you all the code in the right format. It doesn’t want to play with you right now. Alright, so now that they’ve got a place where they can get the code, like really really slowly-- there we go! What are they going to do with it, Shannon? You said that people were having issues?
Shannon: Yes, we want to make sure that we’re opening the correct type of project, so we’re going to go up here to “New Project”, and this is C Sharp (C#), so we’re choosing “Visual C#”, and this is a console application. So a console application — see how it looks kind of like a terminal or a command prompt? That’s basically what it looks like whenever you’re using our binary project from last week, whenever it actually runs. So, obviously, we’re going to choose “Console Application”. So you choose that and you click “OK”. And then it’ll open for you. It has a little bit of information up here and this is where the actual code starts. So I’m going to take the code from GitHub and I’m going to start with “Class Program” — going to copy and paste all this information and then bring it back over here. I’m going to just cut that out and re-paste the new code right into my line. So now I have pretty much the same exact information that we got from the code last week. Now if I hit “Start”, it should run just fine. OK, so now I can run the code from last week. And I get my decibel number — 19 — hit “Enter” to exit. Yay! It worked! Yes!
Fr. Robert: The nice thing about this, remember, Snubs and I are both tinkerers — we like to hack around — and that’s why we’re giving you-- There were a lot of people, I think who were saying “How come you’re not just giving us, like a ‘Hello World’ program?” The reason why we’re not doing that is because we only get half an hour with you every week and we got to move a little bit quicker. So what we’re doing, is we’re teaching you the basic elements, but we’re also giving you a lot more, so that if you feel so inclined, you can poke around. And remember, since that code is always on GitHub, tear it apart. Break it, change lines, try to put different functions in there because there’s nothing you can do that cannot be undone by simply copying and pasting.
Shannon: Now, say I want to write my own console command. And I’m just going to write it on top of what we had last week, with the “enter your own binary number” and then it converts it for you. So this week, I just wanted to put in a simple “Hello World” and then just kind of mess around with it a little bit, you know, kind of break it a bit. Because people do want to see “Hello World”, right? Right.
Fr. Robert: Yeah.
Shannon: So, this is what I did. If we go back to my computer, I’ve highlighted all the code that has to do with writing input, and then reading output. So I just erased all that, and then I’m going to put in my own thing. I’m going to delete that comment because nobody needs that comment there.
Fr. Robert: We don’t like you. No one likes you, comment.
Shannon: So the first line that I’m going to write is “console”. And if you press “Enter”, it’s just going to automatically fill in whatever you have highlighted. Console > Write. And then type in whatever I want it to say: “Hello World”. OK. And then at the end, of course, you have to add a semicolon (;) because that’s what you do with every single line.
Fr. Robert: Let me point out one thing. And the reason why we wanted to start you with visual C Sharp (C#) — and I know there’s a lot of people that are like “Whoa. Why didn’t you start with an easier language?” — because this ID is free, we wanted you to get used to using high-end tools. The nice thing about this environment, is it will actually tell you the different options like that autocomplete — it’s awesome. It will say, OK if you started writing Console, “Here are the different things that you could put behind Console” and it will tell you when you make a mistake — you’ll get little colorings.
Shannon: Oh it’s so great. Let me show you an example for an error if you wanted to make an error. Say I forgot to put a semicolon at the end and I hit “Start”, it’s going to tell me that there is a build error, so I’m going to say “No, I don’t want to go ahead and run it from the successful build.” It gives me a little red squiggly line. It says “semicolon is expected”, so if I put that semicolon and then I run it — now it says “‘Hello World. Hit enter to exit.” because I left in the “Hit enter to exit.” down here. Now if I want to put in a second line, this one’s going to be a little bit different. So this one’s going to be “Console”, and I’ll press enter again, “write,” and I’m going to add “line” on here because that’s going to tell it after you’re done writing “Hello World,” it’s going to put in an automatic “Enter” to the next line below it. So this time I’m going to write “[space] ‘What’s Up World?!” and then put a semicolon. So If I run this, it says “Hello World [space] What’s Up World?!” and then enter, and then it says “Hit Enter to exit.”
Fr. Robert: And that’s the difference between the “Write” and the “WriteLine” command. “Write” just let’s it write, and then basically the cursor stays right there. “WriteLine” says write this and go ahead and drop down.
Shannon: Oh it’s so cool. I got so excited when I was able to make this work. I know it’s super simple, but I was like “Yes! I did it all by myself. I’m so excited!”
Fr. Robert: That’s what we want. We want you to have that feeling. We want you to have that tinkering feeling where you’re like “I wonder if this wor-- It totally works, yes!” Folks, this is how we learn.
Shannon: I’m sitting over here during Know How and I’m just like “Yes! It worked!” OK, so the next part gets a little bit more complicated, but it’s still along this line of “beginner speech”. So this one, I’m going to make a string that’s called “My variables string” and you can call it whatever you want. So if we look at my computer when I do this, I’m going to start with “String” — this is where we’re going to get into variables for our second section of this episode — And then the next line, I’m going to call it “Boondocks” because I can. “Equals” and then I’m going to put “Console” again, “ReadLine”, so I’m just going to press “Enter” right there, ReadLine and then empty parenthesis and a semicolon. What does this mean?
Fr. Robert: What this is doing is it’s initializing a variable called “Boondocks”, which is a string variable, so it’s going to hold a string of characters. Then it’s going to do the “Console ReadLine” command. The Console ReadLine command waits for the user to enter something and hit Enter, and then when they do that, it’s going to take whatever they enter in that line and put it into the variable called “Boondocks”.
Shannon: Yay! So now on my next line I’m going to type in “Console [Period]”, “WriteLine” and then put in “Boondocks”.
Fr. Robert: There you go. And what it should do now, is it should just output the screen whatever it was that you put in that previous line.
Shannon: OK, so, it says “Hello World [space] What’s Up World?!” because I just left that in here. And if I just type in “Yo dawg, got some boondocks?” So it’s going to repeat what I said because that is my string variable called “boondocks” so it repeats “boondocks”. And then it spaces down and says “Hit Enter to exit” because I just left that in there. So I hit “Enter” and it exits for me. So cool!
Fr. Robert: Now folks, I know that it’s a little bit daunting. There’s people who are freaking out because of all that extra code that we say “Oh just don’t worry about it”, no seriously, we mean don’t worry about it. Leave it alone. We’ll get there. Eventually we’ll show you what that does, but for now just assume it has to be there and tinker around with the parts that we want you to tinker with. Last week we asked you to play with the Console command, the read and the write command.
Shannon: Yeah, break it.
Fr. Robert: Do that!
Shannon: There’s the undo button--
Fr. Robert: It’s an undo, right exactly. And then we’re going to move on to more advanced topics. What do you think? Should we do that?
Shannon: I think we should. I mentioned “string variables”, so why don’t we get into variables.
Fr. Robert: Let’s do that. That’s actually a really, really good point. Now, Snubs, you remember high school math, right?
Shannon: Padre, unfortunately. That’s why we skipped school.
Fr. Robert: Do you remember “X” and “Y” and “A” and “B”.
Fr. Robert: Those were called variables, right. Do you remember what they did?
Shannon: They just stood for a number.
Fr. Robert: Right. It’s like a placeholder, right? It’s a way to say a value is going to go into this placeholder, be it X, Y, A, B, C, whatever it was going to be. We knew that’s how you did mathematical proofs. You had something that could represent any value that I put into it. The same way goes for variables in computers. When I play with a variable in a programming language, what I’m telling my compiler is “Hey, here’s a space, a piece of memory, that I want you to preserve. I’m going to fill it with a value, I might do it now, I might do it later, but please hold on to that.
Shannon: Oh, OK. Cool.
Fr. Robert: Now when we talk about variables, there are many different types of variables. I can have an integer variable.
Shannon: An integer is just a number?
Fr. Robert: A whole number, right. 1, 2, 5, 1000. I could have a decimal number.
Shannon: That could be like 3.14 or 2.5.
Fr. Robert: Exactly, so Pi variable, whatever it’s going to be. I can also have a string of characters.
Shannon: And that can be like my boondocks thing.
Fr. Robert: Exactly, which means it’s just going to store a piece of memory that has all of the characters — there could be letters, numbers, alpha numeric — that you’ve put out. There are several different types. You could have floats, you could have integers, you could have long integers, but the thing to remember about all variables is that as big or small as the name might be, they never get more complicated than your high school math class.
Shannon: Oh good. Because I kind of did really bad in college.
Fr. Robert: Oh no. We’re going to fix that. We can fix that.
Fr. Robert: Now, one thing I will say about variables — and we’ll get into this more later on — is when we initialize a variable, what we’re doing is we’re taking out a chunk of system memory and saying that’s what this variable is going to take up. It differs based on what kind of variable it might be.
Shannon: So is this why we kind of talked about binaries last week?
Fr. Robert: This is why we talked about binaries, right, exactly. Because, remember, all memory is binary, right? The memory in your computer can only be on or off, each individual cell. So that tells you that how many possible combinations you can have for any given variable. So, for example, integer is a 32-bit variable.
Shannon: 32 bits, OK.
Fr. Robert: 2 to the power of 32, which gives you something like 4 billion, 300 something--
Shannon: A lot!
Fr. Robert: It gives you a lot of possible combinations. But those 32 bits of memory have been reserved for that integer value.
Shannon: For that one value.
Fr. Robert: Exactly. And actually, on a side, this is one of the reasons why you always want to sterilize your command input because if you don’t do a really good job--
Shannon: What do you mean by “sterilize”?
Fr. Robert: Making sure that, like when you do the Read command — the Console Read, if you don’t check the input to make sure it was what was expected-- For example, if you put “Enter number between 1 and 5” and someone writes in “Execute, execute, execute, execute” — if you don’t make sure that your code checks to see if they’re giving you the expected value, they could do really bad things. They could actually overwrite some of those variables.
Shannon: Oh I’ve heard about hacks like this. Haha that’s funny.
Fr. Robert: OK so that’s a variable, we’re going to show people how to initialize variables, we’re going to show them how they work more, but as long as they know that that’s what a variable does, and that they know that what you did in your code was to initialize a variable, we’re fine. I think we should probably talk a little bit about loops.
Shannon: Oh loops, what are those?
Fr. Robert: Yeah, OK. We’re specifically talking about a while loop. Now, when we talk about loops, what we’re talking about is a command — something that allows you to repeat a piece of code over and over. Now, for example, let me write this. What if I were to put “Console. write(“Hello world”)”.
Shannon: So then it’ll just output “Hello world”.
Fr. Robert: Right, exactly right. So if I print this it’s going to write “Hello world”, correct? Now what if I wanted to write “Hello World” 10 times. What is the first thing I could do?
Shannon: Well, you could copy and paste “Console. write” over and over again. That would make sense.
Fr. Robert: I could just do this 10 times.
Shannon: But that would be a pain in the butt. Like, if you’re an actual coder, you’re doing this for your day job, nobody wants to do that.
Fr. Robert: And imagine if the function was more complicated than just saying “Hello world”, let’s say the function computed the interest off of a bank account. I don’t have to hardcode it every single time the program does that. I want to have a way to reuse the code that I’ve used.
Shannon: OK, so this is why we have loops.
Fr. Robert: This is why we have loops. So, now let’s say I have something like this. I encapsulate this. And let’s pretend I have a function that allows me to repeat this as many times as I want.
Shannon: Do it.
Fr. Robert: That would be a loop.
Shannon: I want to see it.
Fr. Robert: Let’s do this. I’m going to show you what’s called a while loop. A while loop allows me to continue doing a piece of code, to reiterate it over and over, as long as a boolean condition is met. Now, let me explain what that means.
Shannon: Boolean. Is that like a boondock?
Fr. Robert: It could be. Absolutely. Let’s say I have a while loop and in parenthesis is my boolean operator. In other words, it’s going to say as long as this thing is true, continue to do whatever I find in there.
Shannon: Oh OK.
Fr. Robert: So that’s my code. Now, let’s skip the boolean operator for now. Let’s put “Hello world” in here. So we’re going to put “Console write” — I’m just going to abbreviate this — “Console. write (“Hello world”).
Shannon: I like your writing. It’s very pretty.
Fr. Robert: I don’t write, I type. It’s kind of my thing. Alright, so what I’ve just told this to do, is as long as this is true, continue to do this piece of code: Console. write (“Hello World”), Console. write (“Hello World”), Console. write (“Hello World”). The problem is I have to tell it what kind of condition needs to be met. In this case, I’m going to say “x”, and I know people get ticked off when I use “x”, but I’m going to use “x”. I’m going to say as long as “x” is greater than “0”.
Shannon: Oh. So you can use, well, I call them carrots or big alligators, so you can use those?
Fr. Robert: Yeah, these are just boolean operators. So I’m saying as long as the value of “x” is greater than “0”, run this code. Now, a while loop is what we call a pretest loop; in other words, it’s going to look at the condition before the loop ever runs to make sure that it’s true or false.
Shannon: Yeah, that makes sense.
Fr. Robert: There are loops that are guaranteed to run at least once before it checks. Because it’s a pretest loop, it means that it’s possible, if the condition starts off as false, that this will never ever run. So, for example, let’s say I had integer “x” equals “0”.
Shannon: I can’t see it.
Fr. Robert: Oh.
Shannon: Integer “x” equals “0”. OK got it.
Fr. Robert: Let’s say I have integer “x” is equal to “0”. So if I start out my value at “0”, when it gets to the while loop, “x” is “0”, so is “0” greater than “0”? It’s not, which means this will never ever run.
Shannon: OK. So if you put in a different number, like “9”.
Fr. Robert: Let’s put “9”. OK, good idea. “x” equals “9”, now it’s going to get to the while loop. Is “9” greater than “0”?
Shannon: Yes. So it’s going to write “Hello world”.
Fr. Robert: It’s going to go down to the bottom. It’s going to go back up to the past, and what is the value of “x” now?
Shannon: Is it still “9”?
Fr. Robert: Still “9”. So it’s going to write it, it’s going to go to the bottom and come back up to the top.
Shannon: So it’s going to keep on going.
Fr. Robert: It’s going to keep on going. See this is a perpetual loop. There is no way at this present time for this ever to not be true. “x” will always be greater than “0”. So what I’m going to do is below here, I’m going to say “x” is equal to “x” minus “1”. So the first time it runs, “x” is equal to “9”, it’s going to print “Hello World” and then “x” is going to be equal to “x” minus “1”. So that becomes--
Shannon: So 9 minus 1 equals 8.
Fr. Robert: 8. So now this is “8”.
Shannon: It’s going to say, “is ‘8’ greater than ‘0’?” and write it.
Fr. Robert: And that’s still true.
Shannon: And then it’s going to subtract another one.
Fr. Robert: It’ll go all the way down to when it’s “1” — is “1” greater than “0”, yes, “1” minus “1” is “0”, and then the next time up, is “0” greater than “0”? No. It stops. I’m done. So I’m going to run this loop 9 times.
Shannon: That’s cool!
Fr. Robert: Now, this is a very useful function because anytime I need to reuse a piece of code, I’m going to be using a loop — a while loop is actually a really good way to do it.
Shannon: OK. I like it. I get it.
Fr. Robert: I’m thinking maybe now before we get over the CodeWarrior, you want to say thanks to our sponsors?
Shannon: I think we should. Who’s our sponsor today?
Fr. Robert: I think we should because our sponsor is Lynda and what I like about Lynda is that they’re all about knowledge. That’s what Lynda is — they want you to find an easy way to learn the things that you want to learn. We’re about knowledge on Know How — it’s in our name, actually we’re in Coding 101 right now. We’re all about teaching you the knowledge that you want to learn and that’s why we’re happy to have Lynda as our partner. Now Lynda is an online learning company that can help anyone learn creative, software and business skills to achieve both personal and professional goals. With a Lynda.com subscription, members receive unlimited access to a vast library of current and engaging video tutorials across a wide variety of subjects. Everything from creative and software to business negotiation and programming. At Lynda.com you’ll learn how to code, create and build applications from the foundations of object-oriented programming and C and C++ to desktop and mobile apps for today’s popular operating systems. You’ll explore the fundamentals of programming, building web applications with DotNet, PHP and MySQL, managing data with SQL databases, connecting to cloud services and much much more. Now Snubs, I know that you’ve been brushing up on your code. I’ve been doing the same thing.
Shannon: Yes I have.
Fr. Robert: You could technically jump around the web and find a bunch of websites on how to do this and how to do that.
Shannon: Yeah, but then you’re going to end up in the middle of some crazy lesson by somebody who doesn’t even know what they’re talking about.
Shannon: And they’re interesting too. And fun to watch.
Fr. Robert: How fun is it to watch when someone tries to teach you like-- No that’s not great, right? You want someone who both knows how to do the skill and someone who can actually speak in front of a camera. They have high quality video productions from state-of-the-art studios — these aren’t the homemade videos that you’ll find on YouTube. And they have curated course content — this is what I was just talking about — each Lynda.com course is carefully structured so that the users can learn from start to finish or they can jump to a specific chapter. So if you’re learning C++ and you start looking at the lessons and you realize “This is a bit too basic for me”, go ahead and jump forward and find the place where you should be. They have easy to follow videos that help you find the answers you need, and, my favorite part: searchable transcripts. They’ll let you search within a video to save you time and find exactly what you’re looking for. Courses are for all experience levels, from beginner, intermediate and advanced, covering a wide range of technical skills, creative techniques, business strategies, and more. You can watch from your computer, tablet or mobile device, and you can switch and pick up on the chapter where you left off. In other words, Shannon, learn at your pace, when you want, what you want, on your schedule.
Shannon: Love it.
Fr. Robert: So here’s what we want you to do. We want you to try Lynda.com. It’s only $25 per month for access to Lynda’s entire course library. Or for $37.50 per month, you can subscribe to the premium plan, which includes exercise files that let you follow along with the instructor’s projects using the exact same assets. And you can try Lynda.com right now with a free 7-day trial to access the entire library. That’s over 2,000 courses free for 7 days. That’s L-Y-N-D-A dot com slash “c101) and we thank Lynda for their support of Coding 101.
Shannon: Thank you!
Fr. Robert: Hey, you know we got this guy, Lou--
Fr. Robert: Yeah, he’s our Code Warrior. Think maybe we should have him back on the show?
Shannon: He’s the best thing ever, except for Starbucks.
Fr. Robert: Hey, Brian, you want to bring in Lou? Oh my gosh is that Lou Maresca?
Shannon: Yay! It’s Lou!
Lou Maresca: Hey guys, how you doing?
Fr. Robert: Pretty good, now, I’d say that our Coding 101 folk had a lot of fun with your code last week on converting binary into decimal. We now want to convert integers into binary. Now, before we get to that, Lou, do you mind if I teach people really quickly how that might actually work before you give them the code?
Lou: Sure. No problem.
Fr. Robert: OK. I got this from your code. This is how Lou does it. In fact, this is the way that we encourage people to do it. Now, when we were doing binary to integers, it was pretty simple, right? You remember that, we would just look at the value, if it’s “1” we add it up, if it’s “0” we pass on by it. It’s a bit more complicated when we’re converting an integer into binary because when we have something like,say, 41, how does 41 become a binary number? Here’s the easiest way to think of it. I’m converting from a base-10 system — integers, decimals — to a base-2 system. Here’s how you’re going to do it, Shannon. What you want is you take your original quotient and divide it by 2. So how many times does “2” go into “41”?
Shannon: 20 point-
Fr. Robert: Forget the point.
Shannon: Oh, sorry. 20.
Fr. Robert: 20 times right. Is there a remainder?
Shannon: Asking me to do math.
Fr. Robert: I know, I know, I’m sorry. Is there a remainder?
Fr. Robert: And so the remainder would be “1”. Now do the same thing — 2 into 20 goes how many times?
Fr. Robert: And was there a remainder?
Shannon: Yes... no!
Fr. Robert: Exactly. Now, 2 into 10 again, I get what?
Shannon: Now it looks like 100. 5.
Fr. Robert: 5. Is there a remainder?
Fr. Robert: No. Now 2 into 5 how many times?
Shannon: That would be 2.
Fr. Robert: 2 times. Is there a remainder?
Fr. Robert: Yes. 2 goes into 1 how many times?
Shannon: 2 goes into 1?
Fr. Robert: Mhmm
Shannon: 0... 1?
Fr. Robert: One time. Is there a remainder? No.
Shannon: 2 goes into 2 one time.
Fr. Robert: Right. And then at the end we leave it off as this. Now this, if I read this from bottom to top, right to left, this actually gives me my binary.
Shannon: Oh my god, that’s your binary!
Fr. Robert: 101001 that gives me the value of 41.
Shannon: So you start at the bottom.
Fr. Robert: Start at the bottom.
Fr. Robert: So bottom to top. Right to left.
Shannon: I see what you did there.
Fr. Robert: That’s actually very simple. It seems difficult, but that’s how you convert an integer into a binary number. Now, remember last week you helped me to break this down into machine language.
Fr. Robert: Because everything in programming is about taking a real-world problem and turning it into something a machine can actually understand. If you were going to break this down, what would you say the process would be. So we start with a quotient of 41 — we need to tell the computer to do what?
Shannon: Get back to 41?
Fr. Robert: If we start with 41, we have to tell it to divide by 2, right?
Shannon: Oh. Yeah.
Fr. Robert: We divide by 2 and then we say check, right? Is there a remainder? If 2 goes into 41 twenty times, I multiply this by 2 to get 40, and I say is 41 and 40 the same thing? What’s the difference? 1. And I just keep doing that. I keep doing that. I keep dividing my 2 and checking to see if there’s a remainder. Divide by 2, is there a remainder? Divide by 2, is there a remainder? All the way down to the very last one. When 1 becomes the quotient, I just move that over to the end and that gives me my binary number.
Shannon: That’s cool.
Fr. Robert: It’s a really cool process right?
Shannon: That’s really cool. I didn’t know you could do that at all.
Fr. Robert: So Lou, you hardcoded this into a program.
Lou: That’s right. That’s right. What we’re going to do is we’re going to show you how we basically take a number from a user, which we’ve seen before in the last program, and then we’re going to do a loop and like you said exactly what you did is convert that from an integer into a binary.
Fr. Robert: Alright. Do you want to show us your code?
Lou: Sure. So just as Snubs created a really fast console application here. And then we have this guy here — zoom in so everyone can see. So the first thing we want to do is, like you said, ask the user to enter in their numbers. So we ask them “Hey please enter some kind of number or whole number” and then just like Snubs did, I’m going to ask them to grab that number. So I’m going to store it in this fancy-schmancy string variable that we have — just a bunch of text that they enter. And since we’re working with text, we have to do some kind of fancy conversion from text into a number. So I’m just going to put this code in here. You don’t have to worry about that, we can talk about that later. Basically that’s just going to convert the text into a number and then it gives an error if you enter in something else.
Fr. Robert: Now let me stop you really quickly because some people are going to get thrown off by that. An integer is the number. Right? So, 1, 2, 3, 4, 5, 6, 7, 8, 9.
Lou: That’s right.
Fr. Robert: A float is a decimal point, like 3.14.
Shannon: We’re just dealing with integers.
Fr. Robert: We’re just dealing with numbers, right, but when we deal with strings, strings can be characters too — a, b, c, d.
Shannon: Can they include decimals?
Fr. Robert: They could. Because it’s just a string of characters, so any characters. So what Lou is saying is you need something in there to convert that string of characters into numbers. And, Lou, I’m assuming that you’re sanitizing your input?
Lou: That’s right, sanitizing. Making sure they can’t enter something that can crash the program.
Fr. Robert: Right, so if you ask them for a number and they type “the quick brown fox jumped over the lazy padre” it will say “hey, that ain’t a number. What you trying to do? Get out of here.”
Lou: And another thing too, is if they enter too long of a number — because it could be too long and not fit in that size of a variable — it will also check that as well.
Fr. Robert: Got it.
Shannon: So would a code like this be why if I enter a birthdate of 2015 it would yell at me and be like “No, you must have been born before that because it’s not 2015 yet.” if I filled that out in a form online somewhere.
Lou: That’s right.
Fr. Robert: Exactly. That’s all it means to sanitize. It’s making sure that people can’t enter a value that they shouldn’t be able to enter.
Shannon: That’s cool! OK. Moving on.
Fr. Robert: Show it to us.
Lou: So now what we want to do is we want to give them the ability-- we’re actually going to do a conversion now so we’re going to show them how to convert. What we’re going to do is create this little function — we can talk about functions later — but basically has the heart of the code in here to actually convert it. And what we’re going to pay attention to is we get an input of a number, so this is a number we got converted to. And then this is that while loop that Padre was talking about. So in this case, we set a variable for how many times we want a loop, in this case it’s potentially 32 times because we have a 32-bit number.
Fr. Robert: And a 32-bit number could be four billion, three hundred and twenty-four million-something different possibilities.
Lou: That’s right, exactly. So, basically we’re going to loop “while” — the bits are at greater than “0”, so right now we’re starting with “32” and so we’re greater than “0”. The first thing we’re going to do is I’m going to decrement that, so we’re down to “31”. And just like you did on the board, I’m going to divide that number and what this fancy-schmancy little character here is, it’s called an operator, and it’s just like if I were to put a division symbol here. I’m using what they call a mod or a modulo, and what that means is it’s going to give me the remainder. Rather than giving me what I devised, my dividend, it’s just going to give me the remainder, in this case your “0” or your “1”.
Shannon: Oh that’s cool.
Fr. Robert: In our examples, if you were to run that on the number “41”, it would give you a remainder of “1”.
Lou: And then what I’m going to do, is I’m going to do a check, and that’s a special check — you don’t have to worry about it. It’s just saying “Hey, if it’s a “1”, then I’m just going to store a “1” for later so that I can output it to the user.” And then down below finally, I’m going to do what you just did, I’m going to divide the number by 2 again and just store the rest of the number. So in your case, you did “41” and you divided by “2” and you have what was left over. And then again the loop ends and it goes back up to the top and it keeps doing it over and over again. Basically once this hits “0” or it’s greater than “0” — if it hits “0” it’s going to stop — and then we hit the end of the program and then it will return your number.
Shannon: So can I see this actually running?
Lou: Yeah, sure. Let me just add in one extra line here and that’s just going to call this program. So we’ll go up to the top and right here I’m going to add it in here to say make sure to call my little function that I created. And so I’m going to hit run. So it says “Hey, enter a number.” In this case you did “41’, so I’ll do “41” as well. Oops I guess I can’t do that that way. 41. Wups and there’s my error; I didn’t enter a number at all.
Fr. Robert: You just got owned by your own program.
Lou: Number Lock wasn’t on. There you go. OK, so “41” and there you go. So it’s converted your number to binary.
Shannon: That’s cool.
Fr. Robert: 101001
Shannon: So everything is running in the background; you’re not going to see a bunch of output with this code, it’s just going to give you the answer — the binary number.
Lou: That’s right.
Fr. Robert: Actually, Snubs, be with me here on this one. I think you just came up with a challenge that we could give to the chat room and that is go ahead and print every line of the process. Find a way to put something into that while loop that will show you what’s actually happening inside the calculations.
Shannon: Oh I’m not going to say anything. I’m going to let them figure it out.
Fr. Robert: Don’t say anything. We gave them everything they need in just this episode.
Shannon: That’s great! Thank you Lou.
Lou: Yeah, no problem.
Fr. Robert: Now, Lou, let me ask about this. When we deal with code like this I find that a lot of programmers have their favorite loop and they’ll actually bend over backward to make sure they keep using their favorite loop.
Shannon: They’re favorite Lou — haha
Fr. Robert: Yes, I use my favorite Lou all the time. What’s your favorite loop?
Lou: It depends, like while loops like you showed them and there’s also for loops, and that’s very similar to a while loop, but you can initialize and update a variable every time you do the loop. I’m partial to for loops, but there’s also do while’s, which are similar to while’s and then there’s for eaches in C sharp (C#) and that iterates over things, but I’m partial to for loops.
Fr. Robert: Got it. I think next time when we have Lou on, I think we need to let him do a deep dive on all the other kinds of loops.
Shannon: We should!
Fr. Robert: Lou, again, let me get your input on this. I find that because people favor, all programmers tend to favor a piece of code, just like they have their favorite language, often they’ll write less efficient code in order to use their favorite operator.
Lou: It’s true. That’s very true.
Fr. Robert: So what about this, what if we have you back next week to give us the rundown on the different types of loops so that we can see like the while loop is a pretest, but then also posttest loops. And we could also look at “if then else” statements because it’s a good way to look at how you convert that real-world process into code. Can we do that, Lou?
Lou: Yeah, totally. Thanks for having me on this time.
Fr. Robert: Lou, if you could tell the folks at home where can they find you? We know that you’re @LouMM on Twitter, we know that you work for Microsoft, we know that you’re a regular contributor to This Week in Enterprise Tech, but if our Code Monkeys want to go out and talk to you about something they’re finding in their code, where do they find you?
Lou: If you want to talk to me, you can talk to me on Twitter, for sure — send me a message on there. Of actually, you can send me-- I’m going to put my email out there: LouMM@outlook.com.
Fr. Robert: Whoh. That’s brave.
Fr. Robert: You want to do that?
Lou: I’m going to do it. I’m doing it.
Shannon: at outlook dot com.
Fr. Robert: So Code Monkeys, I’m not saying that we’re going to be trolls or anything, but if we were trolls we would all sign Lou up for every single Internet offer we possibly could.
Shannon: Don’t do that.
Fr. Robert: But we wouldn’t do that. No, we love Lou. Lou, thank you very much for being our Code Warrior. We’re going to have you on next week. We’re going to show off your code and, by the way, all of his code is going to be available in our GitHub repository right after the show. Lou, you are a monster.
Lou: Thanks guys. I appreciate it.
Shannon: Thank you.
Fr. Robert: Now Snubs, do you think maybe we’ve given them too much-- like brains might start exploding?
Shannon: Yeah, I think this is a good place to end.
Fr. Robert: It’s a good place to end, yeah. So next week we’re going to be talking all about those other types of loops now that you’ve gotten your feet dirty. We’re also going to be talking about “if then else” statements. We would really really, like how do we promote this? How do they tinker? What are they supposed to do?
Shannon: All sorts of places. You can subscribe to our show. We’re available over at TWiT.tv/code, that’s where you can watch all the long versions of the episodes, well just like the live one that you just watched and you can find all the subscribtion notes for audio and video as well.
Fr. Robert: Alright we’re going to have the links to the compiler, we’re going to have links to our GitHub repository, also don’t forget to subscribe to us on iTunes, if you are an iTunes user. I think we were like number three in video podcasts last week.
Shannon: We are. Yeah, overall, surprisingly.
Fr. Robert: Wow. Go figure. So please jump over there, if you are an iTunes user. If you have an iTunes device, jump in there, subscribe to the show, we’ll make sure you get all the goodness, each and every single week.
Shannon: And we’re on YouTube, right?
Fr. Robert: Yeah, we are on YouTube.
Fr. Robert: And how about this. I’m thinking that maybe once in a while we do a Hangout — invite the Code Monkeys in.
Shannon: I would love to do that! Yeah, that would be so much fun.
Fr. Robert: Exactly, but you can’t do that unless you’re a part of our YouTube group so go ahead and jump in there. Also, we learn from our Know How brethren that the best way to foster a community is to give them a place to hangout, so we created a G+ community. Where is that, Shannon?
Shannon: Yes. So I just created it. It’s gplus.to/twitcoding101 and there’s not even an image up yet, it’s brand new but you can find it at gplus.to/twitcoding101.
Fr. Robert: We have to code it. If you’re not into the G+ group, you can also find us on Twitter. Actually, that’s probably the best way to get ahold of us. I’m @PadreSJ.
Shannon: I’m @Snubs.
Fr. Robert: And as long as you’re watching, you know, why not watch us live?
Shannon: Yeah, absolutely.
Fr. Robert: We go every Thursday usually at about 1:30-ish.
Shannon: Today was about 1:45
Fr. Robert: PM. Pacific Standard Time and as long as you’re there, why not jump into the chat room at irc.twit.tv. We actually do read your comments and believe it or not we incorporate it into the show as we run it’s part of the fun. I want to say goodbye. I’m Father Robert Ballecer.
Shannon: And I’m Shannon Morse.