Thursday 6 November 2014

Java Basics Tutorial 3: Switch Statement



The switch statement

 The switch statement is a wonderful way to reduce code and make code simpler to read and follow.  Let us begin by explaining when a Switch statement would be useful. For example if you had a set of numbers which you needed different results for each. Lets say numbers 1 through 5. Okay there are a couple of ways. An IF statement would do the trick.  Ahhh but a SWITCH statement would do the same with less code and be more readable in the future if extending was required to maybe 1 through 10.

This is how they work.

SWITCH (The condition your testing)
{
   CASE 1: condition 1 is met
   break

   CASE 2: condition 2 is met
   break;

   etc etc....
 
   DEFAULT (a default is set that if no condition is met a result can still be set.)

}


Okay so how does this look in real code? For this example I will do a switch statement on a fictitious game. an imaginary dice throw which certain numbers will let your character move.


switch (dice_roll)
{
   case 1:
   character = "Move left";
   break;

   case 2:
   character = "Move right";
   break;

   case 3:
   character = "Move forward";
   break;

   case 4:
   character = "Move backwards;

   case 5:
   character = "Move down";
   break;

   case 6:
   character = "Move up";
   break;

   default = "Stay still";  

}


Well thats all there is to it. Looks clean and simple doesn't it.  Really handy thing to know and will serve you well in the future. Just remember a switch statement needs to test a condition. Each case returns a result and you should always have a default just in case no condition is met. Simple!




Java Basics Lesson 2: Operators



OPERATORS

An operator is just a way of testing a conditional statement.  To break this down even further an example would be like:

5 < 6.... (the integer 5 is less than the integer 6). The less than sign is the operator!

Okay, pretty simple. We have more operators in Java which make coding a conditional statements very simple.

Other conditionals in Java can test other things. Here are a few:

< less than
> greater than
= equal to
<= less than and equal to
>= greater than and equal to
!= not equal to


++ increment integer by one
-- decrement integer by one
&& AND
|| OR

We generally use operators in programming to test a condition. If a person is a girl or a boy, if your pet at home is a cat or a dog... all of these things can be tested in programming through conditional statements.

How we do this is: (example show how to test what stage of a boys life he is in (String stageofperson))

if (boy_age > 12 && < 18)  //if the boy's age is greater than 12 AND less than 18 he is a teenager
{
   stageofperson = "Teenager";
}
elseif (boy_age >= 18 && < 70) //if the boy's age is greater than or equal to 18 but less than 70 = adult.
{
  stageofperson = "Adult"
}
elseif (boy_age < 12)
{
  stateofperson = "Child"
}
else
{
  stateofperson = "Pensioner"
}


WOW okay thats looks long but actually its not.  We are checking through an if statement to see what stage of the person of life is in. Child if he is less than 12. 13 and above to 18 makes him a teeenager and 18 and over but less than 70 makes him an adult. If none of those match he must be older than 70 which makes him a pensioner.  Seems like a long convoluted way but it actually makes for simple and easy programming and simple to explain here. In real life coding practice we would use a SWITCH statement for the above code. This I will blog next but for the purpose of understanding conditional operators this serves it purpose.

Another example would be this in a WHILE loop. We will to WHILE loops soon...

while (name != "Jenny")
{
   //increment the student number
  student++;

}

Well  hope this has helped. Operators are used in every single program you will make. You will know them soon enough. Do not be daunted by anything here.... it will all come together quickly and soon.




Saturday 1 November 2014

Java Basics Lesson 1 - Variables and Data Types


All major programming languages use this simple concept, so what you learn here can be taken to almost any modern programming language.

Variables and Literals

Variables are dynamic and can be changed. Variables basically hold data or information. Think of them as  slots in a memory bank.  Variables can be assigned values easily and they hold that data until they are changed or discarded as redundant.

A literal is a fixed type of data (the types of data that can be stored in a variable).  There are four types of literals. These are:


  1. Booleans (True or false)
  2. Characters and Strings (character is a single letter, Strings consist of usually more than one character)
  3. Numbers (Java supports integer, floating point, byte literals)
  4. Nulls (The Null literal is literally what it means...nothing)

Examples of all above:

boolean hairywoman = true;
char initial = p;
String surname = "rabbids";

int integer = 3;
float floating = 3.1467788557757785464434 ; (floats can hold 32 bits of data)
double mega_floating = 3.4e+038; (doubles can contain up to 64 bits of data)
byte byte_literal = 2; (nobody really uses bytes anymore as integers do the same job)


When we are coding we will often need to hold information that a method or part of the program will require to use later. Variables do this efficiently. You can use them in multiple ways to achieve a goal for example.....

int a = 5; 
int b = 3;
int result = a + b;
the result variable now holds an integer 8.... clever stuff!

you could then use a boolean in a loop (we will get to loops later)

if (result == 8 )
{
    boolean answer = true;
}
else
{
   boolean answer = false;

}


Thats all there is to it folks!