Java Tutorial
Controll Structures
Introduction
Control structures are statements that are used in programming to allow the programmer to decide when different pats of the program are executed. They also allow checks to be made on variables in the data. There are two main control structures I use when programming, “if” and “while”, these can be used in all most any language though I will focus on teaching you how to use them in Java.
If statement
You can use an if statement to execute a piece of code dependant on whether a given condition is meet.In the below example when x=1 the program will write “hello world” and when it doesn't it will say “byebye world”. Try it out and change the value of x to see the changes
import java.io.*;
public class ifexamp
{
int x; //define x
public static void main(String[] args)
{
x = 1 //give x a value to test you can change this to see the different results
if(x == 1) //not the == is not a mistake and has a different meaning to =
{
System.out.println(“hello world”);
}
else
{
System.out.println(“byebye world”);
}
}
}
When you write an if statement the first thing you need to put is “if” followed by the expression your testing in brackets. In the above code this is the line “if(x==1)”, please note that when trying equals to expressions use a double equals to sign “==” not a single one “=”.
The code that you want to be executed needs to be places in {} brackets. This should then just be written as normal code. It is good practise to indent these lines of code, usually with one tab, so that they can be seen to be within the if statement.
It is then optional to inclued an “else” statement which is code to be executed if the condition is not true. This again goes in {} brackets.
You can nest ifs within each other. However you can often use an “else if” statement to test if a second condition is met when the first one is not.
“while” statements
A while statement is a loop that the program executes until a given condition is found to be true. This can be useful if you are searching for something out of a list or can be used to make a counter.In the below example The program writes the value of x until x = 10. Try changing the code to see what it does so you can see how the loop works.
import java.io.*;
public class whilexamp
{
int x; //define x
public static void main(String[] args)
{
x = 0 //give x a initial value so we can use it in the loop
while(x != 10) //not the != checks whether x doesn't equal 1
{
System.out.println(x);
x++; //adds one to the value of x
}
System.out.println(“x now equals 10 so loop finishes”);
}
}
Like with the if statements the first thing to put is while followed by the expression you want to test in brackets. You then put the code you want to execute in {} brackets like in the above example.
Also the notes that to check for a not equals to situation I used the != symbol. This means that while x doesn't equal 10 the loop is executed but when it does it stops.
Others
Other statements you can use are switch statements (basically a special case of if), do while and for statements. I don't often use these as it is often simpler to use a while or if. There are also special case such as try catch statements that are used with certain advanced features of java.
Comparisons
In almost all statements you need to compare variables. You can this using the following symbols
- == equals 10
- != not equal to
- > greater than
- < less than
- <= less than or equal to
- >= greater than or equal to
- && and
- || or
bravenet.com