Tuesday, 5 March 2013

IF- Statement: and IF-ELSE Statement:



IF- Statement:

It is the basic form where the if statement evaluate a test condition and direct program execution depending on the result of that evaluation.

Syntax:

                If (Expression)
                {
                Statement 1;
                Statement 2;
                }
Where a statement may consist of a single statement, a compound statement or nothing as an empty statement. The Expression also referred so as test condition must be enclosed in parenthesis, which cause the expression to be evaluated first, if it evaluate to true (a non zero value), then the statement associated with it will be executed otherwise ignored and the control will pass to the next statement.
 
Example:
                if (a>b)
                {
                printf (“a is larger than b”);
                }








IF-ELSE Statement:

An if statement may also optionally contain a second statement, the ``else clause,'' which is to be executed if the condition is not met. Here is an example:
                    if(n > 0)
                               average = sum / n;
               else         {
                               printf("can't compute average\n");
                               average = 0;
                                         }

No comments:

Post a Comment