Conditional If-Else Statement with Example in c language

February 16, 2015 , 0 Comments

To overcome the disadvantage of if statement, in computer programming the if-else statement is introduced. It is a two way decision statement which executes either first statement (or block of statements) or second statement (or block of statements) based on the condition.

For the true part if takes care and for the false part else takes care. The syntax of if-else statement along with equivalent flow chart is shown below:

if (condition)
 statement 1 or (block of statement 1)
 statement 2 or (block of statement 2)
Conditional If-Else Statement with Example: Turbo C

According to Flowchart, compiler will first check the condition. If the condition is true, the right side of statement(s) will execute otherwise left side of statement(s) will execute. For example 

Program to check whether the number is even or odd: Let num is the given number. After dividing num by 2, if the remainder is zero, then the given number num is even otherwise, given number num is odd. The equivalent statement can be written as:

if (num%2==0)
    Write: “Number is Even”
    Write: “Number is odd”
[End of if]


Step 1: [Read the number to check]
              Read: NUM
Step 2: [Check the number]
                    Write: ‘Number is Even’
           Write: ‘Number is Odd;
    End of if
Step 3:  Exit

The C program to perform the algorithm:

   int num;
   printf(“Enter an integer number here:”);
      printf(“The number num %d is EVEN”,num);
      printf(“The number num %d is ODD”,num);

Nested-If statement

Bill Smithwick

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google