Saturday, June 8, 2013

Learning Java Part 0

This is going to be part 0 of a three part series. I hope everyone learns why I chose to start with 0 rather than 1 by the end of the series.

Java. It's everywhere. In phones, robots, and toasters. Most of the challenge with learning a language when you've never programmed before is the underlying logic and the strange subtleties of the syntax. That's why I'm not going to teach general syntax first. Instead, I'm just going to go over a few language constructs that are commonly seen in nearly any language.

Variable - something that stores a piece of data. Variables often come with types that tell the computer what kind of data that you are storing. Some of the types include

int - for most intents and purposes, treat this just like you would if it were a regular integer
double - it can store values with a decimal point. 123 or 12.3
boolean - only has two values: true or false
Object - stores a object reference. We will look at objects more in depth on another day

These are the four data types that you will work with the most when programming. There are a lot more that I have not mentioned but they are not hard to learn. Generally speaking, you can do math on these operators such as + - * /. You can also find the remainder using %, which you will hear some people call mod. The order of these operations follows PEMDAS, so use parenthesis if you are unsure that your ordering is correct

Here are a few examples of variables...

int a; // The '//' tells the computer to ignore what goes after it on that particular line. This way you can comment your code.
/* both of these are commenting styles
This one can span multiple lines */
double b = 1.7; // we can also set values to these variables
boolean c;
Object d;// Notice the semi-colon at the end of each line, this tells the computer where one instruction ends and where the next one begins

Great, now that we have figured out variables, lets go on to two basic constructs: ifs and loops

Before we can start talking about ifs and loops, there are a few things we need to clear away first. Remember how we talked about boolean? Well, it turns out that boolean is actually pretty important. There are times when we want to test whether something is true or false, such as testing if int a is equal to 5.
The most important boolean operators you will need to know are these:

Equals ==, it has two equal signs because the single equal sign = is used for setting a variable.
Not equals !=
Less than a < b This returns true if a is less than b
Greater than a > b This returns true if a is greater than b
Greater than or equal to >= same idea
Less than or equal to <= you get the point.

AND &&, this returns true if both conditions are true
OR || this returns true as long as either condition is true
NOT ! changes from true to false and vice versa

Here's how if's work.
if (something here is true) {run this code}

Sometimes there might be else attached. This is optional and run when the condition is false
if (something here is true) {run this code} else {run this code instead}

Here are some examples:

 int j = 2;
 if (j == 3) {
 // this code will not get run because j is not equal to 3
 } else {
  // this code will run!
 }
 // carry on with rest of program
 ------------------------------
 int d = 5;
 if (d == 5) {
 // d is equal to 5, so this code runs!
 }
 // carry on with rest of program

Hopefully you can now see how easy this is. Lets move on to loops..

There are two kinds of loops that I want you to be familar with: for and while use them when you have code that repeats itself

No comments:

Post a Comment