IF ELSE Statement in C

The If Else statement in C Programming is an extension to the If statement in C (which we discussed in the earlier post). We already saw the If statement, and it will only execute the statements when the given condition is true. And if the condition is false, it will not execute statements.

In the real-world, it would be nice to execute something when the condition fails. To do so, If else statement used. Here, Else statement will execute the statements when the condition fails. Let us see the syntax of If Else in C:

If Else statement in C Syntax:

The basic syntax of If Else Statement in C language is as follows:

if (Test condition)
{
  //If the condition is TRUE then these statements will be executed
  True statements;
}

else
{
  //If the condition is FALSE then these statements will be executed
  False statements;
}

If the test condition present in the above structure is true, the True statements will execute. When the condition is false, False statements will execute.

The flow chart of the If Else Statement

Flow Chart for If Else Statement in C

If Else statement in C Example

In this program, we are going to place 4 different printf statements. If the condition is true, we will print 2 separate statements. If the condition is false, Program will print another 2 statements.

/* Program for If Else Statement in C language */

#include<stdio.h> 

int main()
{
 int marks;

 printf("Enter you subject Marks:\n");
 scanf("%d",&marks);

 if(marks >= 50)
  {
    printf("Congratulations\n"); //s1 
    printf("You cleared the subject"); //s2
  }

 else
  {
    printf("You Failed\n");//s3
    printf("Better Luck Next Time");//s4
  }
 return 0;
}

Analysis: User enters his marks, and if the marks is greater than or equal to 50 then s1, s2 will print. If the marks is less than 50 then s3 and s4 will print as an output.

OUTPUT 1: Let us enter 60 as marks. It means, C If else statement Condition is TRUE

If Else Statement in C Output 1

Let us enter 30 as marks. Condition is FALSE

Enter you subject Marks:
30
You Failed
Better Luck Next Time

Comments are closed.