IF ELSE Statement in C

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

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

If Else statement in C Syntax:

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

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

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

When the test condition present in the above structure evaluated to true, the True block of code will execute. When the condition is false, False code block will execute.

The flow chart of the If Else Statement in C

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 lines. If the condition is true, we will print 2 separate lines. And when the condition is false, Program will print another 2.

#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. For example, 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, the expression is TRUE

If Else Statement in C Output 1

Let us enter 30 as marks. The condition evaluated to FALSE

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

Comments are closed.