Program Structure

The basic structure of a program.

Here are the 5 core concepts used by all programs:

  1. Variables
  2. Control Structures
  3. Data Structures
  4. Syntax
  5. Tools

Variables

In computer programming, a variable is a storage location and an associated symbolic name which contains some known or unknown quantity or information, a value.

A variable is simply a way to store some sort of information for later use, and we can retrieve this information by referring to a “word” that will describe this information.

For example, let’s say you come to my website www.howtoprogramwithjava.com and the first thing I want to do, is ask you what your name is (so that I can greet you in a nice way the next time you visit my website). I would put a little text box on the screen that asks you what your name is… that text box would represent a variable! Let’s say I called that text box ‘yourName’, that would be the symbolic name (or “word”) for your variable (as described from our wiki definition above).

So now, when you type your name into the text box, that information would be stored in a variable called ‘yourName’. I would then be able to come back and say “What value does the variable ‘yourName’ contain?”, and the program would tell me whatever it was your typed into that text box.

This concept is extremely powerful in programming and is used constantly. It is what makes Facebook and Twitter work, it’s what makes paying your bills via your online bank work, it’s what allows you to place a bid on eBay. Variables make the programming world go ’round.

Now, if we want to get more specific, when it comes to the Java programming language, variables have different types. Brace yourself here, as I’m going to try to confuse you by explaining an important concept in three sentences. If I were to be storing your name in a variable, that type would be a String. Or, let’s say I also wanted to store your age, that type would be stored as an Integer. Or let’s say I wanted to store how much money you make in a year, that type would be stored as a Double.

What the heck are String, Integer and Double?

Excellent question! In Java, the programming language wants to know what kind of information you are going to be storing in a variable. This is because Java is a strongly typed language. I could teach you about what the difference is between a strongly typed language and a weakly typed language, but that will likely bore you right now, so let’s just focus on what a type is in Java and why it’s important.

Typing in Java, allows the programming language to know with absolute certainty that the information being stored in a variable will be ‘a certain way’. So like I said, if you’re storing your age, you would use the Integer type… well that’s because in Java, an Integer means you have a number that won’t have any decimal places in it. It will be a whole number, like 5, or 20, or 60, or -60, or 4000, or -16000. All of those numbers would be considered an Integer in Java.

So what would happen if you tried to store something that wasn’t an Integer, into an Integer variable, say for instance the value “$35.38”? Well, quite simply, you would get an error in the program and you would have to fix it! “$35.38” has a dollar sign ($) in it, as well as a decimal place with two digits of accuracy. In Java, when you specify that a variable is of type Integer, you are simply not allowed to store anything except a whole number.

Specifying what kind of data that you are dealing with allows the programming language to use that data in interesting ways. Again, what I say “specifying what kind of data”, I’m just referring to the type of data.

Let’s dive into the power of assigning a type to your data.

What can you do with data types?

Let’s start with a simple example.

Your desire is to add two numbers together, let’s say the number 22 and the number 3. Java will behave differently depending on the type of the variable that’s storing this data.

Let me show you what I mean:

If you have defined your variables to be of type Integer, then adding 22 and 3 together will result in the Integer 25. Makes perfect sense right? Of course, this is simple Math.

But what happens if your variables are not Integers, but are Strings?

A String in Java is a different kind of data type and it behaves differently BECAUSE it is a different type of data.

When we refer to a String in Java (and in many other programming languages) we are treating the data like it’s just a plain old sentence in the English language. A String just represents words (or more specifically letters) all placed in a certain order. That’s all the English language (or any language) is, a series of characters/letters placed in a certain order to give meaning to what you’re writing down.

So now I ask you, what does it mean to add two sentences together? What does it mean to add two Strings together?

I’ll show you.

If you were to have two variables, each defined as Strings and they stored the data “22” and “3” (respectively), what would happen if we added them together?

We would get the String: “223”

This might be confusing at first, but it makes more sense when we use less “misleading” data.

Let’s assume that in our two String variables, we aren’t storing numbers, we’re storing words. So in variable 1 we store the String “Hello”, and in variable 2 we store the String “World”.

Now what happens in your mind if I tell you to add those two words together?

Hopefully your natural instinct is to say that the resulting String would be “Hello World”!

That’s all that’s happening with the Strings “22” and “3”… Java behaves differently because of the type of the variables.

To Java, the String “22” is the same type of data as the String “twenty-two”, they’re both characters arranged in a specific way.

Now I don’t want to go into too much detail about types, as this is better suited to programming basic concept #3 – Data Structures. So that’s all I will touch on for now, but no worries, it will all make sense in time!


We’ve already discussed what a variable is, so now let’s talk about control structures. What on earth is a control structure!? Wiki describes it as follows:

A control structure is a block of programming that analyzes variables and chooses a direction in which to go based on given parameters. The term flow control details the direction the program takes (which way program control “flows”). Hence it is the basic decision-making process in computing; flow control determines how a computer will respond when given certain conditions and parameters.

H’okay, so, that definition is obviously a bunch of technical terms that no beginner to programming would understand. So let me try to describe it in more human terms. When a program is running, the code is being read by the computer line by line (from top to bottom, and for the most part left to right), just like you would read a book. This is known as the “code flow“, now as the code is being read from top to bottom, it may hit a point where it needs to make a decision, this decision could make the code jump to a completely different part of the program, or it could make it re-run a certain piece again, or just plain skip a bunch of code. You could think of this process like if you were to read a choose your own adventure book, you get to page 4 of the book, and it says “if you want to do X, turn to page 14, if you want to do Y, turn to page 5″. That decision that must be made by the reader is the same decision that the computer program must make, only the computer program has a strict set of rules to decide which direction to go (whereas if you were reading a book, it would be a subjective choice based on whomever is reading the book). So, this decision that must be made, that will in turn effect the flow of code, is known as a control structure!

Okay, that doesn’t seem to be such a hard concept… a control structure is just a decision that the computer makes. So then that begs the question, what is it using to base that decision on? Well, it’s simply basing its decision on the variables that you give it! Let me show you a simple example, here’s a piece of Java code:

if (yourAge < 20 && yourAge > 12) { // you are a teenager } else { // you are NOT a teenager } So, you can see above that we have a variable, and its name is yourAge, and we are comparing yourAge to 20 and 12, if you’re less than 20 AND you’re more than 12, then you must be a teenager (because you are between 13 and 19 years of age). What will happen inside of this control structure, is that if the value assigned to the yourAge variable is between 13 and 19, then the code will do whatever is inside of the first segment (between those first two curly braces { } ), and it will skip whatever is inside of the second code segment (the second set of curly braces { } ). And if you are NOT a teenager, then it will skip the first segment of code and it will execute whatever is inside of the second segment of code.

Let’s not worry too much about what the code looks like for the moment, as I’ll touch on how to write the code out properly in section #4 syntax. The only concept you need to try and wrap your head around right now, is that there is a way in programming to ‘choose’ which lines of code to execute, and which lines of code to skip, and that will all depend on the state of the variables inside of your control structure. When I say state of a variable, I just mean what value that variable has at any given moment, so if yourAge = 15, then the state of that variable is currently 15 (and thus, you’re a teenager).

You’ve now seen one control structure and I’ve tried to explain it as best I could. This control structure is known as an if...else structure. This is a very common control structure in programming, let me hit you with some other examples. Here’s a while loop control structure:

while (yourAge < 18) { // you are not an adult // so keep on growing up! } This while loop control structure is also very handy, its purpose is to execute code between those curly braces { } over and over and over until the condition becomes false. Okay, so what’s the condition? Well, the condition is between the round brackets ( ), and in this example it checks yourAge to see if you are less than 18. So if you are less than 18, it will continuously execute the code inside the curly braces { }. Now, if you were 18 or older before the while loop control structure is reached by the code flow, then it won’t execute ANY of the code inside of the curly braces { }. It will just skip that code and continue on executing code below the while loop control structure.

Want Free Access to my Best Java Courses?

Alright, so if you’re read this far down the article, then you’re clearly interested in learning how to code. You can currently get access to my 2 best courses on Java for free for 30 days (then it’s just $10/month afterwards, or $97/year).

When you sign up you’ll receive login information to start learning about Java. If you decide you don’t like the courses, you can cancel at any time. If you decide you love the courses then you don’t have to do anything and you can stay subscribed for as long as you like for just $10/month or $97/year.

It’s like Netflix for coders!

Go ahead an click the button below to get started!

Get Access to my 2 Best Java Courses FREE for 30 Days!

There are a few other examples of control structures in Java, but I don’t want to overwhelm you with them right now. So instead I’ll sum up what we’ve learned today.

We’ve learned that code flows from top to bottom and for the most part left to right, just like a book. We’ve learned that we can skip over certain code or execute certain parts of code over and over again, and this is all achieved by using different control structures. These control structures are immensely important to programming, as they make the programs function properly. For example, you wouldn’t want people to be able to login to your Facebook account if they enter the wrong password right? Well, that’s why we use the if...else control structure, if the passwords match, then login, else show a “password is incorrect” screen. So, without control structures, your program’s code would only flow in one way, and it would essentially only do one thing over and over again, which wouldn’t be very helpful. Changing what the code does based on a variable is what makes programs useful to us all!

I hope I’ve cleared the mystery behind the term control structure, and I look forward to talking about our next subject, data structures!


In computer programming, a subroutine is a sequence of program instructions that perform a specific task, packaged as a unit.

Once a unit is identified, it can be called on wherever that particular task should be performed.

Math requires the definition of a unit.

The unit is the basics

Subprograms may be defined within programs, or separately in libraries that can be used by multiple programs. In different programming languages, a subroutine may be called a procedure, a function, a routine, a method, or a subprogram. The generic term callable unit is sometimes used.[1] The name subprogram suggests a subroutine behaves in much the same way as a computer program that is used as one step in a larger program or another subprogram. A subroutine is often coded so that it can be started (called) several times and from several places during one execution of the program, including from other subroutines, and then branch back (return) to the next instruction after the call, once the subroutine's task is done. Maurice Wilkes, David Wheeler, and Stanley Gill are credited with the invention of this concept, which they termed a closed subroutine,[2][3] contrasted with an open subroutine or macro.[4] Subroutines are a powerful programming tool,[5] and the syntax of many programming languages includes support for writing and using them. Judicious use of subroutines (for example, through the structured programming approach) will often substantially reduce the cost of developing and maintaining a large program, while increasing its quality and reliability.[6] Subroutines, often collected into libraries, are an important mechanism for sharing and trading software. The discipline of object-oriented programming is based on objects and methods (which are subroutines attached to these objects or object classes). In the compiling method called threaded code, the executable program is basically a sequence of subroutine calls.

Bibliography

https://en.wikipedia.org/wiki/Analytical_Engine