Advertise here.

Saturday 25 August 2012

Java programming lesson #3 - Decisions

A decision is a way of allowing a program to choose which code to run. If a condition is met then one section of code will be run and if it is not then another section will be run.

The if decision structure

The if statement evaluates a condition and if the result is true then it runs the line of code that follows it. Here is an example of how to test if the value of a variable called i is equal to 5:

public class Decisions
{
   public static void main(String[] args)
   {
      int i = 5;
      if (i == 5)
         System.out.println("i is equal to 5");
   }
}

If you change i to 4 and run this program again you will see that no message is printed.

Relational operators

You will see that a == has been used to test if the values are equal. It is important to know that a = is for setting a value and a == is for testing a value. The == is called a relational operator.

Here is a table of the other relational operators that can be used:

==Equal to
!=Not equal to
>Greater than
>=Greater than or equal to
<Less than
<=Less than or equal to

else

We know that if the condition is true then the code directly afer the if statement is run. You can run code for when the condition is false by using the else statement.

public class Decisions
{
   public static void main(String[] args)
   {
      int i = 4;
      if (i == 5)
         System.out.println("i is equal to 5");
      else
         System.out.println("i is not equal to 5");
   }
}

Grouping statements

If you want to run more than 1 line of code in an if statement then you must group them together with curly brackets.

public class Decisions
{
   public static void main(String[] args)
   {
      int i = 4;
      if (i != 5)
      {
         System.out.println("i not is equal to 5");
         System.out.println("i is equal to " + i);
      }
   }
}

Nested if structures

You can put if statements inside other if statments which will make them nested if statements. It is sometimes more efficient to use nested if statements such as in the following example which finds out if a number is positive, negative or zero:

public class Decisions
{
   public static void main(String[] args)
   {
      int i = 1;
      if (i > 0)
         System.out.println("Positive");
      else
         if (i < 0)
            System.out.println("Negative");
         else
            System.out.println("Zero");
   }
}

AND, OR and NOT

You can test if more than 1 condition is true using the AND(&&) operator. To test if only 1 of many conditions is true use the OR(||) operator. The NOT(!) operator will change a true to a false and a false to a true.

public class Decisions
{
   public static void main(String[] args)
   {
      int i = 1;
      int j = 1;
      if (i == 1 && j == 1)
         System.out.println("i is 1 and j is 1");
      if (i == 1 || j == 2)
         System.out.println("Either i is 1 or j is 1");
      if (!(i == 1))
         System.out.println("i is not 1");
   }
}

The switch decision structure

The switch structure is like having many if statements in one. It takes a variable and then has a list of actions to perform for each value that the variable could be. It also has an optional part for when none of the values match. The break statement is used to break out of the switch structure so that no more conditions are tested.

public class Decisions
{
   public static void main(String[] args)
   {
      int i = 3;
      switch(i)
      {
         case 1:
            System.out.println("i is 1");
            break;
         case 2:
            System.out.println("i is2");
            break;
         case 3:
            System.out.println("i is 3");
            break;
         default:
            System.out.println("Invalid value");
      }
   }
}

0 comments:

Post a Comment