Data types
Loops

ifandelse condition

if and else conditions in C

In if and else coditions, decision making is used to specify the order in which statements are executed. In this tutorial, you will learn to create conditional programs using if…else conditions.

if and else codition-Codingknack

If statement in C

Let us take an example

#include <stdio.h>
int main()
{
    int a = 5 ;
    int b = 5 ;
    if ( a == b )
    {
        printf ( "a and b are equal\n" );
    }
    return 0;
}

a and b are equal

{} after if represents the body of if. Whatever is written inside ‘{}‘ is part of if.

So, the flow is that the first condition of if will be checked and if it is true then statement(s) inside if will be executed.

There is another example.

#include <stdio.h>
int main()
{
    int number;

    printf("Enter an integer: ");
    scanf("%d", &number);

    // Test expression is true if number is less than 0
    if (number < 0)
    {
        printf("You entered %d.\n", number);
    }

    printf("The if statement is easy.");

    return 0;

Enter an integer: -2
You entered -2.
The if statement is easy.

When user enters -2, the test expression (number<0) becomes true. Hence, You entered -2 is displayed on the screen.

Enter an integer: 5
The if statement in C programming is easy.

Syntax of if and else condition

if (testExpression) {
    // codes inside the body of if
}
else {
    // codes inside the body of else
}

if and else statement

Now, let’s see the same examples as above but with the if…else statement.

#include<stdio.h> 
int main()
{
    int a = 5;
    int b = 8;
    if ( a == b )
    {
        printf ( "a and b are equal\n" );
    }
    else
    {
        printf ( "a and b are not equal\n" );
    }
    return 0;
}

a and b are not equal

Another example

#include<stdio.h> 
int main()
{
    int number;
    printf("Enter an integer: ");
    scanf("%d",&number);

    // True if remainder is 0
    if( number%2 == 0 )
        printf("%d is an even integer.",number);
    else
        printf("%d is an odd integer.",number);
    return 0;
}

Enter an integer: 7
7 is an odd integer.

Syntax of nested if and else condition

The nested if…else statement allows you to check for multiple test expressions and execute different codes for more than two conditions.

if (testExpression1) 
{
   // statements to be executed if testExpression1 is true
}
else if(testExpression2) 
{
   // statements to be executed if testExpression1 is false and testExpression2 is true
}
else if (testExpression 3) 
{
   // statements to be executed if testExpression1 and testExpression2 is false and testExpression3 is true
}
.
.
else 
{
   // statements to be executed if all test expressions are false
}
 

take a look at this example


#include 
int main()
{
    int number1, number2;
    printf("Enter two integers: ");
    scanf("%d %d", &number1, &number2);

    //checks if two integers are equal.
    if(number1 == number2)
    {
        printf("Result: %d = %d",number1,number2);
    }

    //checks if number1 is greater than number2.
    else if (number1 > number2)
    {
        printf("Result: %d > %d", number1, number2);
    }

    // if both test expression is false
    else
    {
        printf("Result: %d < %d",number1, number2);
    }

    return 0;
}

Enter two integers: 12
23
Result: 12 < 23

Comments are closed.