C Programming Language

if statements

                  If statement is a conditional branching statement. In conditional branching statement a condition is evaluated, if it is evaluate true a group of statement is executed. The simple format of an if statement is as follows:

if (expression)

                 If the expression is evaluated and found to be true, the single statement following the "if" is executed. If false, the following statement is skipped. Here a compound statement composed of several statements bounded by braces can replace the single statement.

Here's an example program using simple if statement:

if statement

if else statement:

                    This feature permits the programmer to write a single comparison, and then execute one of the two statements depending upon whether the test expression is true or false. The general form of the if-else statement is


                    Here also expression in parentheses must evaluate to (a boolean) true or false. Typically you're testing something to see if it's true, and then running a code block(one or more statements) if it is true, and another block of code if it isn't. The statement1 or statement2 can be either simple or compound statement.

The following program demonstrates a legal if else statement:

if else

                     You can set up an if-else statement to test for multiple conditions. The following example uses two conditions so that if the first test fails, we want to perform a second test before deciding what to do:

if (x%2==0)
       printf(“x is an even number”);
      if (x>10)
                printf(“x is an odd number and greater than 10”);
                printf(“x is an odd number and less than 10”);

                   This brings up the other if-else construct, the  if, else if, else. This construct is useful where two or more alternatives are available for selection.

The syntax is

          statement 1;
else if (condition)
          statement 2;
else if(condition)
         statement n-1;
          statemens n ;

                The various conditions are evaluated one by one starting from top to bottom, on reaching a condition evaluating to true the statement group associated with it are executed and skip other statements. If none of expression is evaluate to true, then the statement or group of statement associated with the final else is executed.

The following program demonstrates a legal if-elseif-else statement:

if elseif else
puts Home switch

c program stats