Decision/if Statement in C | Types | Examples

A statement in a general sense is a part of your program that can be executed i.e., a statement specifies an action.

Statement in C is categorized into the following groups:

  • Selection or Conditional statement
  • Iteration or Loop statement
  • Jump
  • Expression
  • Block

In this article, we will discuss the Conditional Statement or Selection statement and Jump statement in depth.

So let’s discuss the above topics in brief here and then we will continue with the if statement in C.

Block Statments

  • Block statments are simply blocks of code. A block begins with { and ends with a }.
  • Block statments are also referred to as compound statements.

Expression Statements

  • Expression statements are composed of a valid expression or can say that this is a combination of operators and operands which reduces to a single value followed by semicolon (;).
  • Eg:
i = 0;
i = i + 1;

Iteration or Loop statement

  • The iteration statements are for, while, and do-while. These are also commonly called loop statements.
  • Iteration statements cause statements to be executed zero or more times depending upon the conditions except when either the break statement or the continue statement is encountered.
  • We will discuss about loop in details in upcoming articles.

Selection or Conditional statement

if statement in C and switch statement are the two selection statements. In addition, the conditional operator ( ? : ) is an alternative to the if statement in C under certain circumstances.

if statement in C

The general form of the if statement is

if( expression)
{
   statement;
   statement1;
   statement2;
   .
   .
   .
}
else
{
  statement;
  statement1;
}
  • if expression evaluates to true (anything other than 0), the statement or block that forms the target of if is executed; otherwise, the statement or block that is the target of else will be executed, if it exists.
  • Either the code associated with if or the code associated with else executes, never both.

Let’s have a very simple C program to see the working of if

WAP to enter a number check if it is positive or negative.

#include <stdio.h>
 
void main()
{
    int n;
 
    printf("Enter a number \n");
    scanf("%d", &n);
    if (n >= 0)
        printf("%d is a positive number \n", n);
    else
        printf("%d is a negative number \n", n);
}

Code Explanation

  • I have taken an integer which I want to check as a input from the user and store it in a variable n.
  • Using if,else statements I have checked whether the integer is greater or lesser than zero( as we all know that if a number is greater than 0 then it is positive else negative)
  • If it is greater than or equal to zero, then print the ouput as “Number is positive number”.
  • If it is lesser than zero, then print the ouput as “Number is negative number”.
  • Exit.
if statement in c for checking the positive and negative number

Nested if-else statement in C

  • Nesting means if inside if block or else inside else block.
  • Repetation can take place n number of times.
if(condition/expression)
{
    //Nesting of if else inside the body of "if"
    
    if(condition/expression)
    {
       //Statements inside the body of nested "if"
    }
    else
    {
       //Statements inside the body of nested "if"
    }
}
else
{
    //Nesting of if else inside the body of "else"
    
    if(condition/expression)
    {
       //Statements inside the body of nested "else"
    }
    else
    {
       //Statements inside the body of nested "else"
    }
}

Example of nested if..else statement

#include <stdio.h>
int main()
{
   int n, m;
   printf("Input the value of n:");
   scanf("%d", &n);
   printf("Input the value of m:");
   scanf("%d",&m);
   if (n != m)
   {
	printf("%d is not equal to %d\n", n, m);
	//Nested if else
	if (n > m)
	{
		printf("%d is greater than %d \n", n, m);
	}
	else
	{
		printf("%d is greater than %d\n", m, n);
	}
   }
   else
   {
	printf("%d is equal to %d\n", n, m);
   }
   return 0;
}

Output:

Input the value of n:15
Input the value of m:12
15 is not equal to 12
15 is greater than 12 

C – else..if statement

This statement is used when you have to check multiple conditions. We can use this and avoid the use of the nesting if-else statement.

Syntax of else..if statement:

if (condition/expression1) 
{
   //This statements will execute if the condition1 is true
}
else if(condition/expression2) 
{
   //This statements will execute if thecondition2 is true
}
else if (condition/expression3) 
{
   //This statements will execute if the condition3 is true
}
.
.
.
.
.
else 
{
   //This statements will execute if the conditions return false.
}

Example of else..if statement

Let’s take the above example which we have seen above and will rewrite the same program using else…if statements.

#include <stdio.h>
int main()
{
    int n, m;
   printf("Input the value of n:");
   scanf("%d", &n);
   printf("Input the value of m:");
   scanf("%d",&m);
   if (n != m)
   {
        printf("%d is not equal to %d\n", n, m);
   }
        else if (n > m)
   {
        printf("%d is greater than %d \n", n, m);
   }
        else if (m > n)
   {
        printf("%d is greater than %d\n", m, n);
   }
   else
   {
        printf("%d is equal to %d\n", n, m);
   }
  return 0;
}

Output:

Input the value of n:15
Input the value of m:12
15 is not equal to 12

Conditional operator (? 🙂

We have covered conditional operator? :  or also known as a ternary operator which can be used to replace if…else statements under the following circumstances. Its general form is −

Exp1 ? Exp2 : Exp3;

Where Exp1, Exp2, and Exp3 are expressions.

Click here for Operators in details

The value of conditional statement/expression is determined like this −

  • Exp1 is evaluated first and if it is true, then Exp2 is evaluated and becomes the value of the entire expression.
  • if Exp1 is false, then Exp3 is evaluated and its value becomes the value of the expression.
Conditional operator pictorial representation

Let’s have an example of the conditional operator

#include <stdio.h>  
int main()  
{  
    int age;  // variable declaration  
    printf("Enter your age");  
    scanf("%d",&age);   // user input for age variable  
    (age>=18)? (printf("eligible for voting")) : (printf("not eligible for voting"));  // conditional operator  
    return 0;  
}  

Output

Enter your age 18
18
eligible for voting

Enter your age 17
17
not eligible for voting

Run your program here

I hope you will find this information useful. If you have any queries in if statement in C, please comment below or you can contact me directly.

Leave a Comment

Ads Blocker Image Powered by Code Help Pro
Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Please Reload After Disabling Ad\'s