If statements in C

05 min read

 

If statements enable you to make decisions, that is, they enable the program to follow a certain course of action depending on whether a particular condition is met or not.

1) Simple 'if' Statement: This has single selection structure. The if statement first checks the condition. If the condition is true, the true-block statements get executed. Otherwise, the control goes to next statement that is following the if block.

Syntax:

if(condition)
{
True-statement block;
}
Statement-x;

Example:

int main( )
{
int n;
printf("Enter a number:");
scanf("%d",&n);
if(n>0)
{
printf("The number %d is a positive number \n", n);
}
getch( );
return 0;
}

Only if the inputted number is greater than '0', the expression (n>0) evaluates to true and the message will be printed. If the expression evaluates to false, nothing will be done and hence no output will be displayed.

 

2) if....else statement:  So, in order to make some action to be performed if the expression evaluates to false, `else' block can be used.

Syntax:

if(condition)
{
True statement block;
}
else
{
False statement block;
}
statement-x;

The if statement first checks the condition. If the condition is true, the true block statements get executed. After executing the true block statements, the execution continues with statement-x. If the condition is false, the false block statements get executed. After executing the false block statements, the execution continues with statement-x.

Example: To check whether the number is odd or even

int main( )
{
int n;
printf("Enter a number");
scanf("%d",&n);
if(n%2 == 0)
{
printf("it is an even number \n");
}
else
{
printf("It is not an even number \n");
}
getch( );
return 0;
}


3) Nested if statement: The if-else statements can be nested. Nesting is where one statement is contained within another one. This can be used when one condition has to be tested before testingthe second one.

Syntax:

if (condition)
{
if (condition)
{
True statement block;
}
else
{
False statement block;
}
}
else
{
False statement block;
}


Example — To find the biggest of 3 numbers.

int main( )
{
int a,b,c;
printf("Enter 3 numbers:\n");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
if(a>c)
{
printf("a is the biggest\n");
}
else
{
printf("c is the biggestNn");
}}
else
{
if(b>c)
{
printf("b is the biggest\n");
}
else
{
printf("c is the biegest\n");
}}
getch( );
return 0;
}


Assume a=10, b=15, and c=20. So, the outer 'if' expression (a>b 4 10>15) will be false. Coming to the 'else' block, the expression (b>c 4 15>20) is false and so the inner 'else' block is executed which prints the message "c is the biggest".

 

4) Multiple else...if statements: When any of the condition is evaluated to true, the remaining if-else statements will be ommited. Thus, control will be transferred out of the entire nest once a true condition is encountered. 

Syntax:

if (condition-1) 
statements-1;
else if(condition-2)
statements-2;
........
else if(condition-n)
statements-n;
else
statements;

 

POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error