📱

Get Our Mobile App

Take your business learning on the go!

Download on the App StoreGet it on Google Play

Coding a Sequence

SplashKit6:22

Transcription

Hi, I'm Andrew. In this video, we're going to look at building our first program and looking at the syntax in order to do that.

All right, let's have a go at building our own first program and really going through the code and how that works and the syntax rules associated with it. The program that we're going to create is the hello world program. It's very simple and it's pretty much the code that you use to test out the compiler. But we want to really step through this and look at the the syntax and the rules that went behind the creation of this so you understand why all of the bits are where they are.

These are the syntax rules. So let's have a look at the ones that we need for creating a program. All right, these diagrams give us an idea about how to create the code for a program. So this is what we're creating. On the left hand side are the things that we're trying to create. And each of these railroad diagrams or arrow diagrams let us show let us know what we need to type in our code in order to do that. So for a program we can follow it along here and provide a main function or we can go down this one have a header include or a number of includes and then a main function. That's the way we read this. where these boxes, these colored boxes indicate there's another rule. So the way you write a header include is here. Anything in these gray rounded rectangles are things we directly type within the code.

[snorts] So for a header include, we start with hash include. Rather than talking about it here, let's actually switch over to Visual Studio and have a look.

All right, here is my project. Here's the the folder that I was working on and the the old code that I was working with. I've now got a picture of the syntax up here just to remind us how to work through this. So I want to create a program and I know that I need to use the splashkit library to get access to the library. I will use a header include. So I can choose to take this path and include that library. In order to do that the first thing that I need to type is hashincclude. And so quite literally we put hashinclude. And then in quotes we can provide the name of the library. And you'll see Visual Studio to give us some assistance as we go. So that is the name of the library in the quotes and then an end quote to finish the header include that has to go on its own line.

After that we can have the main main function. So we can follow the same rule. What do we need to do? We need to look up that rule. Here is the main function rule. The way we type this is int main and then brackets. So here I can put int main and then par the parenthesis. Oh, I'm getting ahead of myself. Int main and the parenthesis which is followed by a block. And that's what I was just about to type. So we have the open brace here and the close brace. And then inside these we can have have statements. Now notice there's the option to have no statements. So this is a valid program. If I save that, notice it's not saved. I save that with command s. And I can press up. So I've compiled this before. So we get our hello world file being compiled, outputting my program and linking to the splashkit library with the warnings on. We see that that outputs no errors. So I can run my program and the program starts and it stops, but it doesn't do much. So it doesn't do anything. We haven't got any instructions.

[snorts] All right. So, how do we get these instructions? Well, the one thing that we want to do is output a message. So, how do we do that? Well, there's a lot of steps that you need to do to actually achieve that. But the great thing is these are coded up inside procedures that we can use. A procedure you can picture as an entity, a thing that exists inside your program, a programming construct that contains a number of instructions inside it. and it's something that we can call to perform an action. So the splashkit library provides us with a right line procedure where the right line procedure outputs a message to the terminal. So it includes the code to output that message. The syntax for calling a procedure is outlined here. But let's again switch into the code.

All right. So I've got the syntax here. We can see what we want. So inside our program we have a block of instructions and that block includes a number of statements. These are the actions that the computer is going to perform. The statements can be procedure calls at the moment and we'll look at all of the other statements we can do later. Each procedure call has the name of the procedure and then arguments which are values that we pass to it. So here I can call right line and then in the parenthesis we can provide the arguments the values that we want to provide. So hello world can be the value that we pass in there and at the end of that we have a semicolon to end that statement.

[snorts] So the right line include is the name of the procedure. We then have within the brackets the values that pass to it. Now the right line procedure we can see here can take a string but there are also a number of other things that it can take. You can have a look in the references for some more details on that. Now that we have that notice I've saved that we can come down here compile and run that program and then that works.

So these are the syntax rules we've used to create this code. If you don't follow these rules, then that's when you get the syntax errors and you have to check back with the syntax to find out what you were missing. So, we can see here that's where the semicolon came from for this statement.

All right, I've saved that. Let's flick back and wrap up. So, that's the code for our very first program. It's a program that contains a sequence of actions. And for the moment, we've executed or called one procedure, the right line procedure, and got it to output a message. Let's keep on going and see what else we can do.