Basic programming question

I just started following along a free programming class for absolute beginners http://see.stanford.edu/see/lecturelist.aspx?coll=824a47e1-135f-4508-a5aa-866adcae1111 and have a couple simple questions.

The teacher says he likes to start in the middle and work his way up and then fill in the blanks at the end. I’m sure he would but I need the blanks filled in to keep my interest in trying to program. I’m not doubting his way of teaching in any way as I would guess it’s harder to pick up the beginning than to throw the student right into the commands. Anyway I’m on lecture 2 of his class.

He jumps right into how to navigate this robot using commands such as “move, turnLeft, pickUp” and he also uses conditions such as move only if no object blocks path. The programing program is called Eclipse.

What I want to understand is what created the commands and conditions, and how the robot can react to them. I guess to not give me a full lecture on the question, just tell me if it is part of the programmers job to create commands and conditions for the program they are building or is that the magic of the each different programming software? Any extra details are welcome. Programming won’t be hard for me as long as I can make any to some sense about it.

A computer at its heart is an example of what engineers call a finite state machine. This machine has a few relatively simple commands that it executes as it runs through its states. These commands are fairly simple though. For example, a computer cannot execute D=A+B+C. Instead, the computer can add A and B together and store that in a register. Then it can add C to that result and store it in a register. Then it can write that register out to D. The individual things that the computer can do, its commands and conditions, are built into the hardware, and they are very limited.

No one wants to write out 3 steps to do one math function like the above. So, we’ve invented higher level languages, which have their own commands and conditions. The compiler breaks these commands up into the smaller commands that the computer can use. That way, you don’t have to write several lines of code just to do a simple math function, you can just write out D=A+B+C and the compiler figures out how to break it down into steps that the computer can understand.

Each programming language has its own “magic” as you called it. In many cases, the programmer can take advantage of these to do what he or she wants. In other cases though, the commands that you want to use or the conditions that you want to apply don’t exist in the language, and then you have to make them yourself. So, the answer to your question is that it is the programmer’s job to do both. The programmer uses the magic of the program he is using to program with (the compiler), and the programmer makes up his or her own magic to fill in the gaps of what the compiler doesn’t do.

For example, I don’t know the programming features of that robot, but let’s say you want to create a function to turn the robot around 180 degrees. But, let’s assume for this example that the robot language doesn’t have a “turn around” function. However, it does have a “turn left” function, so to create your own “turn around” function you just call the “turn left” function twice.

Now if you only need the robot to turn around once in your code, then it’s kinda silly to go to all of the extra work to make your own turn around function. Instead, you just call the turn left function twice. But if you need the robot to turn around a lot, it is better to create your own turn around function and call that so that you don’t have to make 2 function calls each time.

You can add your own commands and such to a certain extent, but at some point you need to do a task like make the robot go from one side of the room to the other, and there is no “go over there” command. Then you’ll have to string together a much more complex series of commands like go forward, and have conditions like if blocked then go around whatever is blocking the robot, but there is no “go around” command so you’ll have to do things like turn left, go straight, turn right, go straight turn right, go straight, turn left to go around an object. Programming a lot of times is taking the tiny steps that you can do and stringing them together to do the more complex task that you want to do.

A program that contains steps like turn left, go straight, and that sort of thing is a “procedural” program, and those aren’t all that common these days. Most folks these days use “object oriented” programming, which is an entirely different mentality. It also may be a bit over your head at this point so I’ll just stop there and you can ask questions if you want.

I would also like to say that starting in the middle and filling in the blanks at the end seems absolutely moronic to me, but then I haven’t watched the video so I don’t know if the teacher does it in a way that actually works.

I can kinda see the teacher’s point. It’s pretty hard to keep a students’ attention when you’re teaching the nitpicky formats and structures and syntax of a programming language, when all people *really *want to do with a program (or a robot!) is to make it go whizzbang!

So, you teach people how to type in the commands that make the robot go whizzbang! And pretty soon, people want to be able to make the robot go whizzzyfizzzoopBANG! and they’ll have the incentive to learn nitpicky formats and structures and syntax and how to swear at the compiler when you miss a semi-colon and it throws an error at you twenty lines of code down.

And if you get too bogged down, you can go back to the whizzbang program and make small changes to get the robot to do what you want it to. Maybe you don’t really understand the syntax or whatever, but given enough time, you will. And you’ll put in the time because making the robot got whizzbangPOP! is really fun!

Ok, that aside, although what all of engineer_comp_geek said is true, you also need to know that programming fundamentally “telling the computer what to do”. While you might be curious as to how the “move, turnLeft, pickUp” commands and conditions work, you might be going too low level. If you keep going down, pretty soon all you’ll have are “put the bit in box A, put that other bit in box B, if box C is empty add box A to box B and store the result in box C”. Which is not very interesting but somebody has to write assembly.

Instead, I would urge you to think in terms of “what problem do you want to solve?” Reading the course syllabus, this seems to be what the course is about. I’m giving you these building blocks “move, turnLeft, pickUp”. Using these blocks, can you make the robot pick up this block and move to that corner? That’s the kind of work programmers do.

Some programming languages have functions like “return the yearly compound interest given the principal, interest rate, time”, and some have functions like “put bit A into register B”. That’s the tools you have. The question is, using those tools, can you make the computer do what you want it to do?

Generally you always have some building blocks in the form of commands to work with, and they came from somebody before you. The magic of each programming software - better, the magic of each language - includes the commands and also includes how they must fit together in a framework.

It isn’t turtles all the way down. At some point there is hardware, a microprocessor generally, and somebody made the physical object so that it effectively has statements implicit in its design. But everybody that programs does so at various tiers in the software constructed on that foundation.

You could be interested in real estate, or in home construction, or in lumber manufacturing, or in forestry, and you’d be at different levels in the chain. Similarly, you could be interested in Intel’s opcodes and microcode, or in Assembly, or in the .NET framework, or in C++, or in Visual Basic for Applications (I had to bend things a little to pretend that it is this distinct, but the point still stands). This is what they mean by “low level” and “high level” languages.

It is practical for a new programmer to put blinders on and pretend the whole universe of programming starts and ends within the language he’s studying, as a means of starting somewhere. But if your taste leans the other way, look up the examples I gave just above to get some sense of it all.

I should have mentioned that you should buy “Code: The Hidden Language of Computer Hardware and Software” by Charles Petzold if you would appreciate a description of the whole shebang from the bottom up. He even explains the electronic part. If Aristotle had ruled Greece and found this book at the age of 20, we would have had the Internet by the end of the Roman empire.

Those seem like complicated answers. The simple one is that all the Karel stuff, like turnLeft, pickUP, etc are functions that are pre-written by someone else.

Don’t worry too much about how they’re written just now; after one or two more lectures you dump the Karel bit and go onto straight Java where it’s all explained.

if you’re finding this bit too simple for you, just treat it as a way to get used to how Eclipse works. BTW, Eclipse is just a programming environment. You could do exactly the same stuff with a command prompt and a text editor, Eclipse just ties it all together for you to make it easier.

There is a top-level (or bottom-level, as you might prefer to view it) language called op-codes which the computer’s CPU can read and understand at the hardware level. This language does have commands, variables, and conditions. While the CPU is of course electronic (effectively just a really fancy and miniaturized circuit board), you can view it as nothing more than a bunch of gears and levers. You set some levers – like to choose some input numbers and an operation like “add” – turn a crank, and it spits out a value.

Unlike a machine where you need human operators to manually choose and pull levers, the CPU can pull in instructions, set its own levers, and then read in more instructions for where to stick the output values. You can view it as being similar to taking a long sheet of metal and cutting long slots into it that each correspond to a particular lever. As you push the metal sheet sideways, it raises and lowers the levers automatically with the rise and fall of the slots: That’s op-codes.

One can program directly for the CPU, writing the human legible version of op-codes. This is called Assembly Language. It’s not actually “a” language since different types of CPU have entirely different mechanics. You couldn’t take the metal sheet for one machine and apply it to the levers of another, unless the two machines were configured to have the same interface. As it happens, nearly all CPUs in modern machines are made to be compatible with the Intel 80386 CPU, so you can write Assembler for that CPU and run it on most any desktop.

But if you know that all computers can at a certain level add, subtract, multiply, and send data to or receive binary data from some reasonably standardized interfaces (like a command prompt, keyboard, mouse, monitor, etc.) then you can invent a language which has a generic way of representing math and interacting with peripheral devices. If people then code in that language, you can run what they wrote through a second program (a compiler) which knows how to translate what they wrote into CPU-specific op-codes. For conversation’s sake, we’ll call this language Z.

Of course, once you’ve got a standardized interface for dealing with the monitor – like to choose a pixel and set it’s color – you might build up a bunch of code that draws windows, toolbars, a mouse cursor, etc. That’s not very useful in and of itself. Making a program which has some windows and toolbars in it is pointless, unless those elements actually do something (like word processing or browsing the internet). So you don’t want to make a full application. This you want a compiler to also allow you to build out a “library”.

A library is a package of code that’s been converted to op-codes by a compiler, but that can’t be run like a program. It’s just new commands that people can call in their application, as if the Z language now has more commands than it used to.

And then eventually people determine that there’s no particular point in compiling an application at all. Instead of making your compiler translate Z into op-codes and save it, so you have to go and run your program separately, you can just tell the “compiler” to read in the source code, and as it translates commands, to reroute those op-codes to the CPU right then. Now it’s not a compiler, it’s an “interpreter”.

You also have the option of writing a Z compiler in Z itself. You just have to write the first compiler in assembler.

But so long story short: The CPU understands SOME commands and conditions. Computer programming languages actually hide some of the commands that the CPU can handle, but then they may add more via libraries or by implicitly knowing how to expand your one line of code into hundreds of lines of assembler (effectively, the “library” is built right into the compiler). As you program, when you create objects and methods, you’ll be building up new commands that you’re adding to the bunch. For instance, right now you have been given commands like “turn left” and “walk forward”. But you could create a new command called “wander like a drunken bastard” where the robot uses the “turn left”, “turn right”, and “walk forward” commands randomly for a little bit. Then you can call your new command “wander like a drunken bastard” and because you’ve stringed together the old commands randomly inside of your new command, you get the achieved effect. If you wanted to be able to do the drunken walk, you can just call your one method and it will, internally, call the others. That way you don’t have to rewrite the same code over again, and you can package your command up in a library and (potentially) sell it to others to use.

Most people don’t know what a command prompt is.

I suspect that most people who are doing Stanford University’s “Introduction to Computer Science: Programming Methodology” online, do in fact know what a command prompt is! :smiley:

I forgot to mention: op-codes, libraries, compilers, etc are also explained later on in the course.

If you really want to dig into it, download, install, and examine the source code for any one of the open source robotic packages out there. (Note that using linux might be beneficial.) IIRC, Player/Stage is probably has the largest install base, but there are lots of others – for instance, Teambots is a (relatively) old, (relatively) simple, Java-based package for multi-agent robotics research.

Programmer here (enterprise n-tier). The materials that you are taking about sounds like “Karel The Robot”, which I used in the 1990’s. Karel is a good system for studying certain basics, and I recommend it. It won’t make you an expert guru senior level programmer in itself, though.

A vital part of programming is building in layers. Knowing how the low levels (machine and assembly language) work is important, but in a practical sense enterprise application developers don’t work at that level on a daily basis because someone else (e.g. framework developers, operating system developers, device driver developers) have already handled that for you. You just have to know the interface/API.

Those who write device drivers at a low level don’t need to worry a whole lot about writing lots of useful high level applications that use their driver. That isn’t their responsibility and all that has to be established is the interface.

If all programmers worked at all levels, then applications would take a thousand times more to develop than they currently take because each application would practically have to implement some of what your operating system and drivers provide, and if your OS is minimal, then you are restricted on what else you can do with the computer and what computers your application will run. Ever think about why your favorite applications can run on so many different computers? Without the operating system, frameworks, and drivers, it ain’t gonna happen. In the elder days of computers, some applications were implemented from the base hardware up, but is totally unpractical for enterprise development today. Development from the hardware up is now only good for very tiny embedded work, like perhaps a pocket watch or something. Many cell phones run a bona-fide operating system today on top of which applications are installed (iOS, Symbian, Android, etc.).

And, it isn’t necessary to master each level before you do any programming. If you get a Computer Science degree, you’ll be expected to demonstrate understanding at various levels, but you won’t study them all at the same time or be expected to master every one the first year.

Not if its first step is “download an IDE”.

Great answers from all. I’m definitely going to get the book that was recommended. It definitely makes a lot more sense now. Before I get into C++ and java and all that I’m gonna start at the hardware end and work my way up. Thanks again

Oh my friend, Karel.
Your beepers could not foretell
C’s programming hell.

(haiku author was a student at Harvard Extension intro programming course using C, don’t remember his name)

Incidentally, AFAIK both Stanford and AP computer science switched to teaching Java instead of C/C++ to intro students for precisely the above-mentioned reason. C builds character, though :slight_smile:

It should also be said that even folks who understand all the different levels of abstraction in computing still don’t think in more than one or two of them at a time. Even if you know what’s going on with the electrons going through the circuit when two numbers are added, that doesn’t mean you have to be thinking about them when you write your addition statement. In fact, it’s best not to, since your code might end up being run on a computer that has a different low-level architecture, and one of the reasons for the higher levels of abstraction is to make it possible to write code that will work regardless of what’s going on at the other levels.

A book that might be useful is “Programming from the group up”, which you can download for free as a PDF.

It covers programming in Assembly (which is much easier than it sounds) and explains what’s happening at a lower level.

Simplest way to explainprogramming, that I used to use with beginner students:

A computer is like a very fancy caluclator, with a huge amount of Memory Store locations. Programming is like a series of calculator keystrokes. You can write these down. Let’s say your calculator has a way to store these automatically.

The most powerful addition that a computer makes over a calculator is the IF - if you reach a point where “if the number displayed is X” - or “is greater than X” or “is less than X” execute a different set of instructions depending on TRUE or FALSE as the answer.

The second great powerful construct is the “loop” construct, which means “jump to a certain spot in the instructions if the condition is satisfied”. It’s easy to see a loop is simply a combination of the JUMP TO and IF commands.

At a higher level, the languages used by various computer programming interfaces are written to simplify and speed up the process of producing really useful output. From those you can add your own higher level “libraries” of additional useful tools.

So You may get a language that contains “turn left, turn right go forward”, and use that to produce a “go in a square” that contains
"Gimme a number A;
repeat 4 times;(
Go forward A steps.
Turn right 90 degrees; )

Similarly, given a regular programming language with standard math functions, you might write let’s say, a “function” that is given a taxable income amount and returns the tax payable. This function would then be used several times by, let’s say, a payroll program to calculate each employees deductions.

A spreadsheet might even be a good example of a “programming language”. You get a tool which will do certain things, and by adding to it, produce something that does even more and so is useful to you (or someone else).

While I can’t vouch for the book [which is titled “Programming from the ground up”], I can say that assembly is interesting, useful and indeed not as hard as it may sound. It’s just very cumbersome and non-portable compared to pretty much any other programming language. It will give you a pretty good grasp on whatever other language you might encounter later though, and I really recommend any programmer at least try to write a few interesting programs in it.