CONDITIONAL STATEMENTS IN C PROGRAMMING:

CONDITIONAL STATEMENTS IN C PROGRAMMING:

conditional statements :

Conditional statements: In a “C” ,program are executed in sequence. this happens when there is no condition around the statements. If you put some condition for a block of statements the flow of execution might change based on the result evaluated . This process is termed as “Decision making in C”. They are also called controlled statements.

Now, we are going to discuss each conditional statements one-by-one.

1- conditional statements if in c:

The syntax of the “if” statement in C programming is:

if ( write test expression here )
{
// if the test expression is true, type the statement to be executed.
}

if statement evaluates the test expression which is present inside the parenthesis ( ).

  • If the test expression is true, statement present inside the body of if are executed.
  • If the test expression is false, statement present inside the body of if are not executed.

Let’s try to make it more clear by taking an example:

#include <stdio.h>
int main(void){
    
    int a= 55;
    int b= 72;
    
    if(a>b)     // test statement
{
        printf("a is smaller than b");
    }
    printf("test condition is not satisfied");
}

OUTPUT:

test condition is not satisfied

EXPLANATION:

In the above example we take two integers a and b, and apply the test condition by using “if conditional statements”.

  • integer a=55 and b=72, then compiler checks the test condition which states ( a>b ).
  • Hence, our test condition is false, that’s why the statement present inside the body of “if” was not executed and the statement present outside the body of ‘if” is executed.

hope you got it.

IF-ELSE STATEMENT IN C:

The if-else statement is the extended version of if statement.The syntax of the “if-else” statement in C programming is:

if ( test condition )
{
//if the test expression is true, type the statement to be executed.
} else
{
//if the test expression is false, type the statement to be executed.
}
statements;

if-else statement evaluates the test expression which is present inside the parenthesis ( ).

  • If the value of test condition is true, then the body of “if ” is executed.
  • If the value of test condition is false, then the boy of “else” is executed.
  • In any case, after the execution, the control will be automatically transferred to the statements present outside of the if-else block.

Let’s try to make it more clear by taking an example:

#include <stdio.h>
int main(void){
    
    int a= 55;
    int b= 72;
    
    if(a<b)
    {
        printf("a is smaller than b");
    }else
    {
        printf("b is smaller than a");
    }
    
}

OUTPUT:

a is smaller than b

EXPLANATION:

In the above example we take two integers a and b, and apply the test condition by using “if-else conditional statements”.

  • integer a=55 and b=72, then compiler checks the test condition which states ( a<b ).
  • Hence, our test condition is true, that’s why the statement present inside the “if” body is executed.

IF-ELSE LADDER IN C:

It executes two different codes depending upon whether the text condition is true or false. Sometimes, a choise has to be made from more than two possibilities. this if-else ladder allows you to check between multiple test condition and execute different statements. Syntax for multiple test condition is:

if ( test condition 1){
// statement to be execute
}
else if( test condition 2){
//statement to be execute
}
else if ( test condition ){
//statement to be execute
}
.
.
.
else{
//statement to be execute
}

Let’s take an example to make it more clear.

#include <stdio.h>
int main(void){
    
   int marks=68;
   
   if(marks>75){
       printf("First class");
   }
   else if(marks>65){
       printf("Second class");

   }
   else if(marks>55){
       printf("Third class");
   }
   else{
       printf("Fourth class");
   }
   
}

output:

Second class

I hope now you get it.

nested if-else

It is also possible to include an if..else statement inside the body of another if…else statement. It means that when an if else statement is present inside the body of another “if” or “else” then this is called nested if-else.

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

output:

Input the value of a:12
 Input the value of b:21
 a is not equal to b
b is greater than a

if…else…if statement:

Let me make you understand by taking an example of daily life:

If I have atleast 50,000, then I will purchase an A.C.
else if I have 30,000, then I will purchase refrigerator.
else if I have 20,000, then I will purchase cooler.
Else I will purchase ceiling fan.

In the above example, each condition is dependent on money availability and must be checked sequentially.

Also if a condition is true then all other conditions must be ignored. These situations in C programming are handled using a combination of if...else...if statement.

if...else...if is an extension of if...else statement. It specifies “If some condition is true then execute some task; otherwise if some other condition is true, then execute some different task; if none conditions are true then execute some default task.”

now, let’s see the syntax of if-else-if statement.

if (boolean_expression_1) 
{ 
// If expression 1 is true then execute 
// this and skip other if 
} 
else if (boolean_expression_2) 
{ 
// If expression 1 is false and 
// expression 2 is true then execute 
// this and skip other if 
} 
else if (boolean_expression_n)
 {
 // If expression 1 is false,
 // expression 2 is also false,
 // expression n-1 is also false,
 // and expression n is true then execute 
// this and skip else. 
}
 else 
{ 
// If no expressions are true then
 // execute this skipping all other.
 }

example:

#include <stdio.h>

int main()
{
    /* Declare integer variable */
    int num;

    /* Input an integer from user */
    printf("Enter any number: ");
    scanf("%d", &num);

    if(num < 0)
    {
        /* If number is less than zero, then it is negative */
        printf("NUMBER IS NEGATIVE.");
    }
    else if(num == 0)
    {
        /* If number equal to 0, then it is zero */
        printf("NUMBER IS ZERO.");
    }
    else
    {
        /* If number is greater then zero, then it is positive */
        printf("NUMBER IS POSITIVE.");
    }
    return 0;
}

output:

Enter any number: -22 
NUMBER IS NEGATIVE.

hope you got it.

In our next article we will discuss about loop. 
I will recommend you if you don't visit our previous articles, so firstly go through it for better understanding.
I am mentioning my all previous articles below:
Scroll to Top