Java by Example - IF, ELSE and SWITCH: basic control structures
To check for an expression or condition being true or false, you usually use the IF statement:
if(test-expression)
body
That means: if the test expression results to TRUE, the body is executed, otherwise the whole statement is skipped and the sourcecode below it is executed, if any. Test expressions can be a boolean variable, or the result of a function or calculation: if(3*3==9) is also true.
In many cases you want to execute a certain piece of code if a condition or expression is true, and a different piece, if that same expression does not result to true. To accomplish this, you can use the following statement:
if(test-expression)
body1
else
body2
The applet below should make this clear, it tests for mouseclicks being in the left or in the right half and displays the result in a text string.
//Sourcecode
import java.awt.*;
import java.applet.*;
public class Project5A extends Applet
{
int mouseX, mouseY;
boolean clicked=false;
public boolean mouseDown(Event evt,int x,int y)
{
clicked=true;
mouseX=x;
mouseY=y;
repaint();
return true;
}
public void paint(Graphics g)
{
g.setFont(new Font("Helvetica",Font.PLAIN,15));
g.drawString("Click inside the applet!", 80,50);
//we only draw this if the mouse has been clicked
if(clicked)
{
if(mouseX < size().width/2)
g.drawString("You clicked on the left side!",60,100);
else
g.drawString("You clicked on the right side!",60,100);
}
}
}
|
In many cases you will also want to check for other cases, if case 1 is not true. You could write:
if(test-expression1)
body1
if(test-expression2)
body2
if(test-expression3)
body3
, but these statements would be tested independently from each other. Suppose case 3 is correct, but case 2 also. Or case 1 and case 2 are true, but you want to execute body3 only if test-expression1 and test-expression2 are false. You could use the following statement:
if(test-expression1)
body1
if(test-expression2)
body2
if(test-expression1==false && test-expression2==false)
body3
Doing that, it might occur that both body1 and body2 are being executed, if test-expression1 AND test-expression2 are true. If you want only one of the three bodys to be executed, you must use the following:
if(test-expression1)
body1
else if(test-expression2)
body2
else
body3
The following applet does just that:
//Sourcecode
import java.awt.*;
import java.applet.*;
public class Project5B extends Applet
{
int mouseX, mouseY;
boolean clicked=false;
public boolean mouseDown(Event evt,int x,int y)
{
clicked=true;
mouseX=x;
mouseY=y;
repaint();
return true;
}
public void paint(Graphics g)
{
g.setFont(new Font("Helvetica",Font.PLAIN,15));
g.drawString("Click inside the applet!", 80,50);
//we only draw this if the mouse has been clicked
if(clicked)
{
if(mouseX < size().width/3)
g.drawString("You clicked in the left 1/3!",60,100);
else if(mouseX > size().width-size().width/3)
g.drawString("You clicked in the right 1/3!",60,100);
else
g.drawString("You clicked in the middle 1/3!",60,100);
}
}
}
|
In some cases you will want to check for a number of conditions, where a series of if/else statements would be suitable. A more elegant and shorter way is the switch-statement. It tests for a variable (which must be an integer type) to match one of several values. If any of them matches, the body of that statement is executed:
switch(test-expression)
{
case 0: body1
break;
case 1: body2
break;
case 2: body3
break;
case 3: body4
break;
default: body5
}
So, if the test-expression equals 0, body1 is executed, if it equals 3, body 4 is executed and if neither of the first four match, the default statement is executed, if any.
The break after each statement takes care of the whole switch-statement being left, then. If none of the single statements matches the value of the test variable, the body of the default statement is executed. This is mandatory though, you don't have to provide a default branch.
The example below demonstrates this. If the number of clicks inside the applet matches zero, one, two or ten, you get an appropriate message, also for any other case:
//Sourcecode
import java.awt.*;
import java.applet.*;
public class Project5C extends Applet
{
//we can initialize this variable with 0, which is not neccessary though,
//booleans are initialized with false and numbers with 0 by default
int clickNumber=0;
public boolean mouseDown(Event evt,int x,int y)
{
//increase by 1, you could also write
//clickNumber = clickNumber+1, but why should you :)
clickNumber++;
repaint();
return true;
}
public void paint(Graphics g)
{
g.setFont(new Font("Helvetica",Font.PLAIN,15));
g.drawString("Click inside the applet!", 80,30);
g.drawString("Clicks: "+clickNumber, 120,50);
//our switch statement, which tests the clickNumber variable
switch(clickNumber)
{
case 0:
g.drawString("You haven't clicked yet!",70,100);
break;
case 1:
g.drawString("You clicked once!",90,100);
break;
case 2:
g.drawString("You clicked 2 times!",80,100);
break;
case 10:
g.drawString("You clicked 10 times!",90,100);
break;
//the default statement is mandatory, it can be ommitted
default:
{
g.drawString("You clicked more than 2 times,",50,80);
g.drawString("but not 10 times!",90,100);
}
}
}
}
|
For completeness, I show you another way for an if-else statement. It is extremely short and can be put in one line, in many cases inside another expression. Here it comes:
test-expression ? body1 : body2;
Funny, isnt't it? This is a short way of writing:
if(test-expression)
body1
else
body2
An example:
a < b ? c=1 : c=2;
That means: if a is less than b, let c be 1, if not, let c be 2.
Another nice example:
g.setColor(b1?Color.red:Color.black);
b1 is a boolean variable here. If it is true, then the active color is set to red, if it is false, the color is set to black. You see that it is no problem to put this test statement into the brackets of the setColor-method.
|