Search This Blog

If you like any posts in this blog then click this icon which is present under the post

Friday, 22 July 2011

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

Followers