Khurram's World - JAVA Conditional Statements
 
 
..Islam and Religion
 .Computer & IT
  Web Designing TIPS
..HTML & DHTML
..Active Server Pages
..JAVA & JAVA Applets
..JAVA & VB Script
..CGI & Perl
..Electronic Commerce
..Web Sites & Emails
 
Pakistan News Service
Bay Banner
 
 
 
Conditional Statement  

Objective:

Learn about:
    • The role of decision making statements in programming
    • Use of if and switch statements
    • Nested if statements
Concept
Condition statements allow the selective execution of portions of a program according to the value of some expression. The java programming language supports the if and switch statements as conditional statements.
 
if statement
Within an “if” statement, first of all the given condition is checked. If it is true, the underlying statement is executed once. If it is false, program control is switched to the next statement.
Syntax
if(Boolean expression) {
        statement / block;
}
Example
        int i = 10;
if (i < 10 ) {
        System.out.println(“i is less than 10”);
if / else statement
In the case of if/else statements, when the if statement becomes false, control of the program shifts to the else block.
Syntax
if (Boolean expression) {
        statement / block;
}
else {
        statement / block;
}
Example 1
        int i  = 11;
if ( i < 10 ) {
        System.out.println(“i is less than 10”);
} else {
        System.out.println(“i is greater than 10”);
}
Example 2
        int i = 11;
if (i < 10) {
        System.out.println(“i is less than 10”);
}
else if (i > 11) {
        System.out.println(“i is less than 11”);
}
else if (i == 11) {
        System.out.println(“i is equal to 11”);
}
else {
        System.out.println(“Number is not found”);
}
Nested if
In nested if statements, we declare an if statement within the declaration of another if statement. This forms a block of if statements.
Syntax
If (Boolean expression) {
        If (Boolean expression) {
                Statement block;
        }
                statement;
        }
        else
                statement;  
Mode of Execution
With nested if statements, first of all the outer if statement is executed. If the outer if statement is true then control will be shifted to the second if. If the second if is true the statements within its scope will be executed. When all the statements are executed, then program will control will shifted back to the outer if and any remaining statements there will be executed.
Example
class NestedIf {
        public static void main(String args[]) {
                String name = “Pakistan”;            
               
if (name == “Pakistan”) {
                if (name.length() < 5 ) {
                        System.out.println(name);
                }
                        System.out.println(“This is the nested if “);
                }
                else
                System.out.println(“Your string is more than five letters”);
        }
}
The Switch Statement
The switch statement is similar to the if statement, except that it allows many values of any expression to be checked in a single statement. A switch statement can be implemented as a series of if-else statement.
First the case variable is checked. Then program execution transfers to the first statement identified by the case value that matches the result of the expression. The expression must be an integer or a character. It cannot evaluate to a Boolean or floating-point value.
Syntax
Switch (variable) {
        case value 1;
                statement-list1;
                break;
        case value 2;
                statement-list2;
                break;
        case value 3;    
       
        statement-list3;
                break;
        default;
                statement-list3;
                break;
}
 
Example
class SwitchEx {
        public static void main (String args[]) {
                int a = 15;
                switch (a) {
       
        case 1;
                        System.out.println(“My color is green”);
                        break;
                case 5;
                        System.out.println(“My color is blue”);
                        break;
                case 10;
                        System.out.println(“My color is yellow”);
                        break;
                default;
                        System.out.println(“My color is red”);
                        break;
                }
        }
}