If else and if…else…if statement in C

In earlier submit, I defined to regulate applications circulate of execution utilizing simple if statement.

Easy if works as “if some situation is true then do some duties. It does not specify what to do if the situation is false. A very good program should assume each method. For instance – if a person inputs the right account quantity and pin, then enable cash withdrawal in any other case present error message to the person.

if...else assertion is an extension of straightforward if. It really works as “If some situation is true then, do some process in any other case do another process.

To learn about Flow Control Statements — Click Here

Syntax of if...else assertion


    // Physique of if
    // If expression is true then execute this


In the above syntax, if the given Boolean expression is true then, execute physique of if half in any other case execute the physique of else half. In any case, both physique if or physique of else is executed. Not, in any case, each of the blocks will execute.

Flowchart of if...else assertion

flow chart if else assertion -- Teche Programmer

To learn about flow charts basics — Click Here

Instance of if...else assertion

Allow us to write program based mostly on if...else assertion. Write a program to enter two numbers from a person. Print most between each the given numbers.

 * C program to seek out most between two numbers

#embody <stdio.h>

int principal()
    /* Declare two integer variables */
    int num1, num2;

    /* Enter two quantity from person */
    printf("Enter two numbers: ");
    scanf("%dpercentd", &num1, &num2);

    /* Evaluate each quantity utilizing relational operator */
    if(num1 > num2)
        /* If first quantity is larger than second */
        printf("First quantity is most.");
        /* If first quantity just isn't better than second */
        printf("Second quantity is most.");

    return 0;

Output of the above program

Enter two numbers: 10
Second quantity is most.

Ladder if...else...if assertion

Easy if assertion offers skills to execute duties based mostly on some situations. Its extension if…else takes either side of the situation and execute some statements if situations are true or if the situation is false then, execute another assertion.

Nevertheless, issues in actual life just isn’t easy. Allow us to contemplate an actual life conditional downside

If I've at the very least $1500, then I'll buy Microsoft Floor Professional 4.
Else if I've at the very least $1200, then I'll buy Apple Mac ebook Air.
Else if I've at the very least $1000, then I'll buy HP ultra-book.
Else I'll buy some mid-level developer laptop computer.

Within the above state of affairs, every situation relies on mum or dad situation and has to be checked sequentially. Additionally, if a situation is true then all different situations have to be ignored. These conditions in C programming are dealt with utilizing a mixture of if...else...if assertion.

if...else...if is an extension of if...else assertion. It specifies “If some situation is true then execute some process; in any other case if another situation is true, then execute some completely different process; if none situations are true then execute some default process.”

Ladder if...else...if assertion syntax

if (boolean_expression_1)

    // If expression 1 is true then execute 
    // this and skip different if 

else if (boolean_expression_2)

    // If expression 1 is fake and 
    // expression 2 is true then execute 
    // this and skip different if

else if (boolean_expression_n)

    // If expression 1 is fake, 
    // expression 2 can be false, 
    // expression n-1 can be false,
    // and expression n is true then execute 
    // this and skip else.


    // If no expressions are true then
    // execute this skipping all different.

Ladder if...else...if assertion flowchart

if else ladder f;ow chart -- Teche Programmer

Instance of ladder if...else...if assertion

Allow us to write an easy C program to enter an integer from a person. Examine if the given integer is negative, zero, or positive?

To learn about basic C Program — Click Here

 * C program to examine detrimental, zero or optimistic.

#embody <stdio.h>

int principal()
    /* Declare integer variable */
    int num;

    /* Enter an integer from person */
    printf("Enter any quantity: ");
    scanf("%d", &num);

    if(num < 0)
        /* If quantity is lower than zero, then it's detrimental */
        printf("NUMBER IS NEGATIVE.");
    else if(num == 0)
        /* If quantity is larger then zero, then it's optimistic */
        printf("NUMBER IS POSITIVE.");
    return 0;

Output of the above program.

Enter any quantity: -22

Leave a Reply

Your email address will not be published. Required fields are marked *