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 the code.

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

C If Else Statement Syntax:

The basic syntax of the 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 is evaluated to be true, the True block of code will execute. When the condition is false, a 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: The user enters his marks, and if the marks are greater than or equal to 50, then s1 and s2 will print. For example, if the marks are 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 was evaluated as FALSE.

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

Comments are closed.