Selection Statements
if statment
- use the if statement to selectively execute instructions based
on whether a condition (test) is true
- format
if(boolean condition)
statement or block
else
statement or block
- condition must evaluate to true or false
- you may have a single statement following the if or a block of
statements in {}
- the else part is optional
- conditions for comparing numbers
- > for
greater than
- < for less than
- != for not equal
- == for equal (note
double equal signs)
- >= for greater
than or equal to
- <= for less
than or equal to
- && for AND
- || for OR
- example:
int x = 0,
y = 1;
if(x == y)
System.out.println("x equals y");
else
System.out.println("x is not equal to y");
if(y >= x)
{
y = x;
x = 0;
}
else
x = 1;
if(x > 0 || y > 0)
System.out.println("One or both X or Y is greater than zero);
if(y > 0 && x > 0)
System.out.println("Both X and Y are greater than zero");
- conditions for comparing strings use String funtions equals,
equalsIgnoreCase, compareTo, compareToIgnoreCase. In each you are
asking one string to compare itself to another string. equals and
equalsIgnoreCase return trrue or false, compareTo and
compareToIgnoreCase return a negative number if the string being asked
comes before, 0 if the strings are equal, and positive if the string
being asked comes after the other string.
- example:
String s1 =
"Cat", s2 = "Dog", s3 = "dog";
if(s2.equals(s3))
System.out.println("s2 equals s3");
else
System.out.println("s2 is not equal to s3"); // this will
print
if(s2.equalsIgnoreCase(s3))
System.out.println("s2 equals s3"); // this will print
else
System.out.println("s2 is not equal to s3");
if(s1.compareTo(s2)
> 0)
System.out.println("s1 comes after s2");
else
if(s1.compareTo(s2)
< 0)
System.out.println("s1 comes before s2"); // this will print
else
System.out.println("s1 equals s2");
conditional expression
- this is a short cut for simple if else
statements and has the format:
(boolean expression) ? expression 1 :
expression 2
- if the boolean expression is true, then expression 1 is
evaluated, else expression 2 is evaluated. Note, be sure to use the ?
and : in the right spots
- Example
if(x > 0)
y = 1;
else
y = -1;
y = (x > 0) ? 1 : -1;
switch statement
- use a switch statement to selectively execute one of a number of
groups of statements based on the result of an expression or value of
a variable
- replaces multiple ifs and nested ifs when examining the same
expression for different values
- format
switch (expression)
{
case value1:
statements
break;
case value2:
statements
break;
case value3:
statements
break;
default:
statements;
}
- the expression can evaluate to any number,or single character (
not a String)
- execution will start after the colon following the correct value
and continue until a break statement is encountered.
- the default case is optional
- values must be constants or literals
- example:
int x = 1, y = 2;
switch (x + y)
{
case 1:
System.out.println("x+y = 1");
break;
case 2:
System.out.println("x+y = 2");
break;
case 3:
System.out.println("x+y = 3");
break;
default:
System.out.println("x+y is
not 1, 2 or 3");
}
using if statements would yield
if(x+y == 1)
System.out.println("x+y = 1");
else
if(x+y == 2)
System.out.println("x+y = 2");
else
if(x+y == 3)
System.out.println("x+y = 3");
else
System.out.println("x+y is not 1, 2 or 3");