if ........ else Statement

if ........ else Statement


     if….. else statement is used to  execute one group of statements if the test condition is true or other group if the test condition is false. The general form is

     if(test condition)
     {
           Statement block-1;
     }
     else
     {
          Statement block-2;
     }
     Next statement;

                When this statement is executed, the computer first evaluates the value of the condition. If the value is true, statement block-1 is executed and the control is transferred to next statement. If the value is false, statement block-2 is executed and the control is transferred to next statement.

Rules

1.       The brackets around the test condition are must.
2.       The test condition must be a relational or logical expression.
3.       Statement blocks are called body of the if …. Else statement. It can contain one or more statements.
4.       The opening and closing brackets {} are must if the statement blocks contain more than one statement. Else optional.
5.       No statements other than the statements in the statement blocks are allowed between if….. else.

Flow diagram



Example

Class Student
{
Public static void main(String args[])
{
int mark=50;
if(mark>35)
System.out.println(“Pass”);
else
System.out.println(“Fail”);
}
}

                This program tests the mark of the student. If it is greater than 35 it prints pass. If it is less than 35 it prints fail.

No comments:

Post a Comment

Creating Objects

Creating Objects                  Creating objects means to allocate memory space for all the instance variables of the objects. S...