Simple if Statement

Simple if Statement


     Simple if statement is used to execute or skip one statement or group of statements for a particular condition. The general form is

     If(test condition)
     {
         Statement block;
     }
     Next statement;

     When this statement is executed, the computer first evaluates the value of the test condition. If the value is true, statement block and next state are executed sequentially. If the value is false, statement block is skipped and execution starts from next statement.

Rules
1.      The brackets around the condition are must.
2.      The test condition must be relational or logical expression
3.      Statement block is called body of the if statement and it contains one or more statements.
4.      The opening and closing brackets {} are must if the statement block contains more than one statement. Else optional.

Flow chart


Example

Class Student
{
            public static void main(String args[])
            {
                        int mark=70;
                        char grade=’A’
                        if (grade==’A’)
                        {
                                    mark=mark+10;
                        }
                        System.out.println(mark);
            }
}

            This program tests the grade of the student. If the student grade is A, then 10 marks will be added to his marks. For other grades no additional marks are added.

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...