The if-else class of statements should have the following form:
if (condition) {
statements;
}
if (condition) {
statements;
} else {
statements;
}
if (condition) {
statements;
} else if (condition) {
statements;
} else {
statements;
}
All programming languages have some form of an if
statement that allows you to test conditions. All arrays have lengths and we can access that length by referencing the variable arrayname.length
. We test the length of the args
array as follows:
Source Code
// This is the Hello program in Java
class Hello {
public static void main (String args[]) {
/* Now let's say hello */
System.out.print("Hello ");
if (args.length > 0) {
System.out.println(args[0]);
}
}
}
What we did was wrap the System.out.println(args[0])
statement in a conditional test, if (args.length > 0) { }
. The code inside the braces, System.out.println(args[0])
, now gets executed if and only if the length of the args array is greater than zero. In Java numerical greater than and lesser than tests are done with the > and <>= respectively.
Testing for equality is a little trickier. We would expect to test if two numbers were equal by using the = sign. However we've already used the = sign to set the value of a variable. Therefore we need a new symbol to test for equality. Java borrows C's double equals sign, ==, to test for equality. Lets look at an example when there are more then 1 statement in a branch and how braces are used indefinitely.
Source Code
import java.io.*;
class NumberTest
{
public static void main (String[] args) throws IOException
{
BufferedReader stdin = new BufferedReader ( new InputStreamReader( System.in ) );
String inS;
int num;
System.out.println("Enter an integer number");
inS = stdin.readLine();
num = Integer.parseInt( inS ); // convert inS to int using wrapper classes
if ( num < 0 ) // true-branch
{
System.out.println("The number " + num + " is negative");
System.out.println("negative number are less than zero");
}
else // false-branch
{
System.out.println("The number " + num + " is positive");
System.out.print ("positive numbers are greater ");
System.out.println("or equal to zero ");
}
System.out.println("End of program"); // always executed
}
}
All conditional statements in Java require boolean values, and that's what the ==, <, >, <=, and >= operators all return. A boolean is a value that is either true or false. Unlike in C booleans are not the same as ints, and ints and booleans cannot be cast back and forth. If you need to set a boolean variable in a Java program, you have to use the constants true
and false
. false
is not 0 and true
is not non-zero as in C. Boolean values are no more integers than are strings.
Lets look at some examples of if-else:
//Example 1
if(a == b) {
c++;
}
if(a != b) {
c--;
}
//Example 2
if(a == b) {
c++;
}
else {
c--;
}
We could add an else statement like so:
Source Code
// This is the Hello program in Java
class Hello {
public static void main (String args[]) {
/* Now let's say hello */
System.out.print("Hello ");
if (args.length > 0) {
System.out.println(args[0]);
}
else {
System.out.println("whoever you are");
}
}
}
Source Code
public class divisor
{
public static void main(String[] args)
int a = 10;
int b = 2;
if ( a % b == 0 )
{
System.out.println(a + " is divisible by "+ b);
}
else
{
System.out.println(a + " is not divisible by " + b);
}
}
We're not just limited to two cases though. We can combine an else
and an if
to make an else if
and use this to test a whole range of mutually exclusive possibilities.
Lets look at some examples of if-else-if:
//Example 1
if(color == BLUE)) {
System.out.println("The color is blue.");
}
else if(color == GREEN) {
System.out.println("The color is green.");
}
//Example 2
if(employee.isManager()) {
System.out.println("Is a Manager");
}
else if(employee.isVicePresident()) {
System.out.println("Is a Vice-President");
}
else {
System.out.println("Is a Worker");
}
Source Code
// This is the Hello program in Java
class Hello {
public static void main (String args[]) {
/* Now let's say hello */
System.out.print("Hello ");
if (args.length == 0) {
System.out.print("whoever you are");
}
else if (args.length == 1) {
System.out.println(args[0]);
}
else if (args.length == 2) {
System.out.print(args[0]);
System.out.print(" ");
System.out.print(args[1]);
}
else if (args.length == 3) {
System.out.print(args[0]);
System.out.print(" ");
System.out.print(args[1]);
System.out.print(" ");
System.out.print(args[2]);
}
System.out.println();
}
}
No comments:
Post a Comment