if statement in C


  • if statement is very powerful decision making statement.
  • C uses if statement to execute a specific set of statements when the given logical condition is true.
  • Here is a syntax of if statement as follows:
  • Syntax

    if(condition)

    {

    Statements ;

    }
  • Statements present inside if block are executed only when the condition is true. In case the condition is false the compiler skips the statements within if block. Here is a diagram that represents the concept of if statement more clearly:
  • decision-making
  • Here is an example of if statement:

Example

#include<iostream.h>

#include<conio.h>

int main()

{

int a,b;

printf("\n Enter the value of a and b:");

scanf("%d%d",&a,&b);

if(a==b)

{

printf("value of a and b are equal");

}

getch();

return 0;

}

Output

The output of the above program is as follows:

Enter the value of a and b: 4

4

Value of a and b are equal