The if-else Statement in C Programming Language

Let us understand if-else statement in C programming language.

Here, if and else are the keywords in C.

Syntax:

The keyword if tells the compiler that what follows is decision control statement. The block of statements to be executed must be enclosed in opening and closing braces. If only one statement needs to be executed then braces need not be used. Let us understand if-else statement using an example.

Example:

In the above example we have assigned value 10 to a. In the next line, there is an if statement. It is read as “If a is equal to 10 then perform the block of statements enclosed within the braces of if part else perform the block of statements enclosed within the braces of else part”. After the execution of if-else statement normal sequence flow of the program is followed.

The output of the above example is:
You are in if block
You are in 1st block
The value of a is 10

The else-if statement

The else-if statement is nothing but the rearrangement of else with the if that follows.

Consider the below program.

The above program can be re-written as,

Here, the else-if statement is used. Else if reduces the complexity of the program making it easier to understand.

You may also like...

35 Responses

  1. if we get result using if-else,then why we use else if statement. can you tell me properly?

  2. LearnCOnline says:

    Hello,

    The else-if statement is is used when ever we have multiple if conditions.
    e.g.
    if (condition 1)
    {
    do this;
    }
    else if (condition 2)
    {
    do this;
    }
    else if(condition 3)
    {
    do this;
    }
    else
    {
    do this;
    }

    Hope its clear now… Any queries do reply. I will get back to you.

    Many Thanks.

  3. SARAN says:

    @learnconline dude…you know what you are doing now ? you are changing my life…:) thanks for the site…i will share it in every social media i am in…

  4. LearnCOnline says:

    Thanks everyone for your support… We would need your continuous support to keep this going…
    Thanks once again.

    Regards,
    LearnConline

  5. Anonymous says:

    super thanx buddy you are doing a great job here

  6. Imran says:

    Cool site to learn C online.

  7. Anonymous says:

    heres my problem.
    create a program that will accept 3 numbers. if the numbers are unique, press 1 for ascending order and press 2 for descending order. if two numbers have the same value it will automatically print the 3 numbers.

  8. ankit gupta says:

    great work man….it really people learn c easily….Thank you so much

  9. you have to work with three temperature types: Celsius, Fahrenheit and Kelvin. What we are going to do here, people would come to you to ask to convert temperature. They come with three inputs, the temperature which they want to convert, the type of the temperature and the intended type to convert it. As we are dealing with three types of temperature, we represent them 0, 1 and 2 (Celsius = 0, Fahrenheit = 1 and Kelvin = 2). The temperature is a number (floating point). The output is the temperature in the intended type….

    what the ans is ….

  10. Anonymous says:

    cool 🙂

  11. #include
    #include
    #include
    #include

    int main()
    {
    char ch;
    cout< <"Enter any character :";
    ch=getchar();
    if(isalpha(ch))
    cout< <"Alphabet";
    else if(isdigit(ch))
    cout< <"Number";
    else
    cout< <"Special Character";
    getch();
    return 0;
    }

  12. Shouldn’t the output of the example program in IF-ELSE section be:
    you are in 1st block
    The value of a is 10
    Correct me if I’m wrong.
    Cheers 🙂

  13. Anonymous says:

    @Mustafa

    i think its just a typo

    @learnconline

    kudos to you..dude..this stuff is really cool and easy to understand.

    • Babla says:

      #include
      #include

      main()
      {
      char sex, Qualification;
      int experiance;

      printf(“Enter sex Qualification and Experiance: “);
      scanf(“%c %c %d”, &sex, &Qualification, &experiance);

      if(sex == ‘M’ && Qualification == ‘PG’ && experiance >= 10)
      {
      printf(“Salary = 15000”);
      }
      if((sex == ‘M’ && Qualification == ‘UG’ && experiance >= 10) ||
      (sex == ‘M’ && Qualification == ‘PG’ && experiance < 10))
      {
      printf("Salary = 10000");
      }
      if(sex == 'M' && Qualification == 'UG' && experiance = 10)
      {
      printf(“Salary = 15000:”);
      }
      if((sex == ‘F’ && Qualification == ‘UG’ && experiance >= 10) ||
      (sex == ‘F’ && Qualification == ‘PG’ && experiance < 10))
      {
      printf("Salary = 10000");
      }
      else
      {
      printf("Salary = 7000");
      }

      getch();
      return 0;
      }
      it output is 7000 every time. what should be done??

  14. Anonymous says:

    YOU ROCK!!!!! I WAS THINKING HW WILL I LEARN C WITHOUT A TEACHER….HERE I GOT THE BEST SOuRCE U R BETTER THAN ANY TEACHER. I WILL LEARN C IN A WEEK LIKE THIS…..ONE Q. PLZ TELL ME ANY FREE C COMPILER FOR WINDOWS 7 (64 bit) THANKS A LOT MAN

  15. 2) #include
    #include

    int main(void) {
    int n;
    printf(“Please enter a number: “);
    scanf(“%d”, &n);
    if (n == 1) {
    printf(“n is equal to 1!\n”);
    }
    else if (n == 2) {
    printf(“n is equal to 2!\n”);
    }
    else if (n == 3) {
    printf(“n is equal to 3!\n”);
    }
    else {
    printf(“n isn’t equal to 1, 2, or 3.\n”);
    }
    system(“PAUSE”);
    return 0;

  16. Naveed says:

    Aoa…
    i am doing program which a program to input percentage of the student print out its grade according to the percentage criteria.
    when i hve compiled this program its give error of else is misplaced…… can u tell me please whts problem here???

    #include
    #include
    void main()
    {
    clrscr();
    float per;
    printf(“please enter the percentage of the student”);
    scanf(“%f”,&per);
    if(per>=80 && per< =100)
    printf(“the grade A+”);
    else
    if(per>=70 && per<80)
    printf(“the grade is A”);
    else
    if(per>=60 && per<70)
    printf(“the grade is B”);
    else
    if(per>=45 && per<60)
    printf(“the grade is c”);
    else
    if(per>=33 && per<45)
    printf(“the grade is D”);
    else
    printf(“fail”);
    hetch();
    }

  17. Naveen,why did u use double &&, instead of one & in above program. why can’t we use one &? Explain

  18. Anonymous says:

    what will be the output for
    {
    int a=10;
    if(a)

    printf(“%d”,a);

    else

    printf(“\n nothing”);
    }

  19. Anonymous says:

    #include
    #include
    main()
    {
    char password[8];
    int a,b, sum, prod, diff,order,exit;
    float quo,peso, usd,change;
    printf(“PASSWORD:\t”);scanf(“%s”,&password);

    if (password!=”dakota”)
    {
    goto reenter;
    }
    else
    {
    goto menu;
    }

    reenter:
    clrscr();
    printf(“Reenter Password:\t”);
    scanf(“%s”,&password);

    if(password==”dakota”)
    {

    menu:
    printf(“MENU”);
    printf(“CALCULATOR\n”);
    printf(“CONVERSION\n”);
    printf(“GRADE\n”);
    printf(“EXIT\n”);
    }

    printf(“choose a number from the menu: “);
    scanf(“%d”,&a);

    else if (a==1)
    {
    printf(“\nYou chose calculator”);

    printf(“\nenter a number: \t”);scanf(“%d”, &a);
    printf(“\nenter a another number: \t”);scanf(“%d”, &b);
    sum=a+b;
    prod=a*b;
    diff=a-b;
    quo=a/b;
    printf(” the sum is= %d \n”, sum);
    printf(” the product is= %d \n”, prod);
    printf(” the diffence is= %d \n”, diff);
    printf(” the quotient is= %.2f \n”,quo);
    }

    else if (a==2)
    {
    printf(“welcome to conversion!”);

    printf(“\nenter Philippine peso:\t”);scanf(“%f”, &peso);
    usd=peso/43;
    printf(“\n%.2f”,peso);printf(” is equivalent to:\t%.2f”,usd);
    }

    else if (a==3)
    {
    printf(“you chose ordering system”);
    clrscr();
    printf(“\nwhat’s your order?\t”);scanf(“%d”,&order);
    printf(“\ncost:\t”);scanf(“%d”,&a);
    printf(“\nmoney received:\t”);scanf(“%d”,&b);
    change=b-a;
    printf(“\nYour change is:\t%.2f”,&change);
    }

    else if (a==4)
    {goto end;
    }

    end:getch();
    }

    -i don’t know anymore how to correct this. i am a beginner. can you help me?

  20. hi , i’d like to ask

    my program is running now . but he’s giving a wrong answer

    main()
    {
    int grade;
    clrscr();
    gotoxy(33,10);printf(“Enter grade: “);
    scanf (“%d”,&grade);
    if ((grade >=91)&&(grade< =100));{
    gotoxy(33,11);printf(“Excellent”);}
    if ((grade >=75)&&(grade< =90));{
    gotoxy(33,11);printf(“passed”);}
    if (grade < =74);{
    gotoxy(33,11);printf(“failed”);}
    if (grade >=101);{
    gotoxy(33,11);printf(“Invalid grade”);}
    getch();
    }

    if i input a 91 grade he shoud be answering “excellent” but what ever the number is the answer is always invalid ..
    i asked my prof what’s wrong he replied that I missed a curly braces.. before the statement ? where should I put it I tried so many times putting curly braces in different places but it’s error ..
    mind helping me :((
    I’m just a first year college so I’m sorry for if I’m so dumb askin this to you guys :S

  21. Anonymous says:

    this is my program but, as it runs, the output has a problem… can u fix it? #include
    main()
    {
    float at, wc;
    char nc[25],cn[25];

    clrscr();
    printf(“\n Enter name of Costumer:”);
    scanf(“%s”, &nc);
    printf(“\n Enter client number: “);
    scanf(“%s”, &cn);
    printf(“\n Enter Account type: “);
    scanf(“%f”, &at);
    printf(“\nEnter Water Consumption:”);
    scanf(“%f”, &wc);

    if (at=1)
    {
    printf(“\nRESIDENTIAL”);
    printf(“\n1st 10 cu.m=P185.00”);
    printf(“\nExeed 10 cu,m= additional P10.00”);
    if(wc< =10)
    {
    printf(“\nTO PAY FOR RESIDENTIAL:P185.00”);
    }
    else if(wc>10)
    {
    printf(“\nTO PAY FOR RESIDENTIAL:%f”,(wc-10)*10+185);
    }

    else if(at=2)
    {
    printf(“\nCOMMERCIAL”);
    printf(“\n1st 10 cu.m=P220.00”);
    printf(“\nExeed 10 cu.m=additional P15.00”);
    }
    if(wc< =10)
    {
    printf(“\nTO PAY FOR COMMERCIAL:P220.00”);
    }
    else if(wc>10)
    {
    printf(“\nTO PAY FOR COMMERCIAL:%f”,(wc-10)*15+220);
    }
    getch();
    return 0;
    }
    }

  22. Heather says:

    Can anyone help with this program?? I will be much appreciative. I am stuck I have never done programming before and I need to write C program that will do the following:

    Write a C program which prompts the user to enter some numbers and prints

  23. Helllo… i have made a program but its giving some errors can u make it corret for me plzzz??
    #include
    #include
    #include
    main()
    {
    char str=”a”;
    printf(“Enter the name of actor or actoress\t”);
    scanf(“%s”,&a);
    if(a==”shahrukh khan”)
    {
    printf(“Latest movies of Shahrukh Khan in 2012 and 2013\n”);
    getch();
    printf(“1:Jab Tak Ha Jaan\n2:Chennai Express\n3:My Name Is Khan\n4:Rab Ne Bna Di Jodi\b”);
    }
    else if(a==”salman khan”)
    {
    printf(“Latest movies of Salman Khan in 2012 and 2013\n”);
    getch();
    printf(“1:Ek Tha Tiger\n2:Veer\n3:Body Guard\n4:Ready\n5:Dabbang\n6:Dabbang 2\b”);
    }
    else if(a==”amir khan”)
    {
    printf(“Latest movies of Amir Khan in 2012 and 2013\n”);
    getch();
    printf(“1:Taare Zameen Par\n2:3 Idiots\n3:Ghajni\b”);
    }
    else if(a==”saif ali khan”)
    {
    printf(“Latest movies of Saif Ali Khan in 2012 and 2013\n”);
    getch();
    printf(“1:Tashan\n2:Race\n3:Agent Vinod\n4:Qurbaan\n5:Race 2\b”);
    }
    else if(a==”ayushman khurrana”)
    {
    printf(“Latest movies of Ayushman Khurrana in 2012 and 2013\n”);
    getch();
    printf(“1:Vicky Donor\n2:Nautanki Saala\b”);
    }
    else if(a==”katrina kaif”)
    {
    printf(“Latest movies of Katrina Kaif in 2012 and 2013\n”);
    getch();
    printf(“1:Ek Tha Tiger\n2:Jab Tak Ha Jaan\n3:Rajneeti\n4:Race\b”);
    }
    else if(a==”anushka sharma”)
    {
    printf(“Latest moies of Anushka Sharma in 2012 and 2013\n”);
    getch();
    printf(“1:Matru Ki Bijli Ka Mandola\n2:Baand Baaja Baarat\n3:Jab Tak Ha Jaan\b”);
    }
    else if(a==”shruti hassan”)
    {
    printf(“Latest movies of Shruti Hassan in 2013\n”);
    getch();
    printf(“1:Ramaiya Vasta Maiya\b”);
    }
    else if(a==”madhuri dixi”)
    printf(“\nNo movie of Madhuri Dixit in released 2012 and 2013\b”);
    else if(a==”sanjay dutt”)
    {
    printf(“Latest movies of Sanjay Dutt in 2012 and 2013\n”);
    getch();
    printf(“1:Police Giri\n2:Agneepath\b”);
    }
    getch();
    }

  24. Anonymous says:

    question..how can i use if else statement to make a password?
    answer please..thnks..(asap..)

  25. Anonymous says:

    I need bike and spare sale program in c

  26. Anonymous says:

    I need
    They given spare rate .we select only quantity of sapre
    bike rate standard, it will calculate

    output
    base mirror scratch box total
    2500 1 0 1 2800

  27. shair zaman says:

    char vowel;
    printf(“enter vowel=”);
    scanf(“%c”,&vowel);
    if(vowel==’a’ ||vowel==’A’ ||vowel==’i’ ||vowel==’I’ ||vowel==’O’ ||vowel==’o’ ||vowel==’e’ ||vowel==’E’ ||vowel==’u’ ||vowel==’U’)
    {
    printf(“alphabet is vowel=%c”,vowel);
    }
    else
    {
    printf(“alphabet is constant=%c”,vowel);

  28. Anonymous says:

    really awesome dude

  29. sulisulo says:

    hai,,im confused whether to solve it using if statement or switch statement?any help?

    1. The National Earthquake Information Center has asked you to write a program implementing the following decision table to charaterize an earthquake based on it

  30. mhei says:

    hi. can someone help me with this .
    req:
    if item code is A description is CHAIR witha a prize of 200 else if itemcode is B description is TABLE with a prize of 500 else item code is C description is PC with a prize of 4000.

    output display should be like this.
    a.

    Itemcode:_______
    description :________
    prize:_________

    output B.

    Itemcode:_______
    description :________
    prize:_________
    quantity:______
    total amount:__________

    totalamount=quantity x prize

    thanks for the reply 🙂 godbless

  31. poornima says:

    i need to print the name as well as the age using if else but name is not displaying
    my code is this
    #include
    #include
    void main()
    {
    char a[20];
    int i;
    clrscr();
    printf(“enter the name\n”);
    scanf(“%c”,&a);
    printf(“enter the age”);
    scanf(“%d”,&i);
    if(i>=18)
    {
    printf(“eligible for vote”);
    }
    else
    {
    printf(“not eligible for vote”);
    }
    getch();

  32. Darshil Patel says:

    Write a program to calculate bill of a job work done as follows.
    Use if else statement.
    a) Rate of typing 3 Rs/page
    b) Printing of 1st copy 5Rs/pages & later every copy 3Rs/page.
    The user should enter the number of pages and print out copies he/she wants.
    Can You please Give me the answer of this question…..

  1. September 20, 2015

    […] if-else Statement in C […]

Leave a Reply

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