If Else condition in C Language

If Else condition in C Language:

(1) If Else statement allows us to write two alternative paths and the control condition determines which path gets executed. In an if-else statement, only the code associated with if (i.e. statement-1) or the code associated with else (i.e. statement-2) executes, never both.

(2) Sample C language code and its output, as shown:

Sample C language code for If Else condition
#include <stdio.h>
#include <conio.h>
intmain()
{
int x;
int y;
x = 5;
  y = 7;
  // If else in C Language
if (x > y) {
printf(“x is greater than y\n”);
  } else {
printf(“x is less than y\n”);
  }
  // If else if in C Language
  x = 10;
  y = 10;
if (x > y) {
printf(“x is greater than y\n”);
  } else if (x = y) {
printf(“x is equal to y\n”);
  } else {
printf(“x is less than y\n”);
  }
getch();
return 0;
}

(3) After that go to the “Build” menu and select the “Build” option for publishing the code as shown.

click on build for logical operator code unit

(4) After building the code, press the “Run” button, as shown.

Run c language code for if else statement

(5) After pressing the “run” button, the output screen is opened, as shown.

if else condition output obtained in C

If Else Syntax:

if (condition) {
  // block of code to be executed if the condition is true
} else {
  // block of code to be executed if the condition is false
}

If Else If Syntax:

if (condition1) {
  // block of code to be executed if condition1 is true
} else if (condition2) {
  // block of code to be executed if the condition1 is false and condition2 is true
} else {
  // block of code to be executed if the condition1 is false and condition2 is false
}

Logical Operators in C Language
Comparison Operators in C Language
Arithmetic Operators in C Language
SQL SELECT DISTINCT Statement
SUBSTRING (Transact-SQL)– Microsoft Docs

Leave a Reply