C if else Statement

In C if-else statement is a selection statement that is used to select statements depending on the value of a controlling expression. The statements in the if-branch are executed only if the condition evaluates to a non-zero value (or true). That means if the controlling expression is not equal to 0, the statement following the if statement gets executed, and the statements following the else get skipped.

But if the controlling expression is 0, the statement following the if selection statement gets skipped, and the statement following the else gets executed.

 

Syntax:

if (expression)
secondary-block1
else
secondary-block2

The secondary-block1 gets executed when the expression is not equal to 0 and the secondary-block2 gets executed when the expression is equal to 0.

 

Flowchart of the if-else statement in C:

 

How if-else statement in C works?

I believe now you have understood that if the given controlling expression is non-zero, then the statements associated with the if statement is executed and the statements associated with else are not executed. Similarly, if the controlling expression is zero, then the statements associated with “if” are ignored and the statements associated with the ‘else’ are executed

For better understanding consider the below program where we are finding the even and odd numbers using the if-else statements.

#include <stdio.h>

int main(void)
{
    int data; //variable to store data

    /*Get input from the user*/
    printf("\n Enter any number: = ");
    scanf("%d", &data);
    
    /* If number is divisible by 2 then it is a even number */
    if((data % 2) == 0)
    {
        printf("\n Number is Even.\n\n");
    }
    else
    {
        printf("\n Number is Odd.\n\n");
    }

    return 0;
}

 

How the above program works:

Case 1: When you entered even number:

We know that the modular division of an even number by 2 is 0. So expression (data % 2) return 0, as we know expression ( 0 == 0) return 1.

So if((data % 2) == 0) become if(1), as per C standard if the controlling expression is non-zero then the body associated with the if statement will be executed and the body associated with else will skpiied. So statement printf(“\n Number is Even.\n\n”) will be executed and skip the statement printf("\n Number is Odd.\n\n").

 

Case 2: When you entered an odd number:

For odd number expression (data % 2) return 1, so expression ( 1== 0) return 0. So if((data % 2) == 0) become if(0), so body of if will be skip and else will be executed. So statement printf("\n Number is Odd.\n\n") will be executed and printf("\n Number is Even.\n\n") the statement will be skipped.

Let’s consider one more example, where the C code find the greatest number from the two given numbers.

#include <stdio.h>
 
int main(void)
{
    int a, b;
 
    printf(" Enter the number1 = ");
    scanf("%d", &a);
    
    printf("\n Enter the number2 = ");
    scanf("%d", &b);
    
    if (a > b)
    {
        printf("\n Greatest number = %d \n",a);
    }
    else
    {
        printf("\n Greatest number = %d \n",b);
    }
       
    return 0;
}

 

 

If…else if…else Statements

If more than one conditions occur in the program, then we should use if…else if…else statements. In if…else if…else statement each condition is checked until not get the true condition.

In if… else if statement, when the condition is found to be true, then statements that attached with the true condition is only executed and all other conditions will be skipped and program control comes out of if… else if statement.

Syntax,

if(condition-1)
{
    statement-1;
}

else if(condition-2)
{
    statement-2;
}

...

else if(condition-n)
{
    statement-n;
}

else
{
    statement;
}

 

 

c else if

 

The basic formula to convert Fahrenheit and Celsius to each other.

Celsius to Fahrenheit: (°C × 9/5) + 32 = °F
Fahrenheit to Celsius: (°F − 32) x 5/9 = °C

#include <stdio.h>

int main()
{

    float fh,cl;
    char ch;

    printf("\n\n Press c to convert temperature from Fahrenheit to Celsius.");

    printf("\n\n Press f to convert temperature from Celsius to Fahrenheit.");

    printf("\n\n Enter your choice (c, f): ");

    scanf("%c",&ch);

    if((ch =='c') ||(ch =='C'))
    {
        printf("\n\nEnter temperature in Fahrenheit: ");
        scanf("%f",&fh);
        cl= (fh - 32) / 1.8;
        printf("\n\nTemperature in Celsius: %.2f\n\n",cl);
    }
    else if((ch =='f') ||(ch =='F'))
    {
        printf("\n\nEnter temperature in Celsius: ");
        scanf("%f",&cl);
        fh= (cl*1.8)+32;
        printf("\n\nTemperature in Fahrenheit: %.2f\n\n",fh);
    }
    else
    {
        printf("\n\nInvalid Choice !!!\n\n");
    }

    return 0;
}

Output:

c if else

Recommended Articles for you: