Copyright © tutorialspoint.com

Latest C Tutorial

if...else if...else examples

previous

Try following example to understand if statement. You can put the following code into a test.c file and then compile it and then run it.

#include <stdio.h>

main()
{
  int cows = 6;

  if (cows > 1)
    printf("We have cows\n");

  if (cows > 10)
    printf("loads of them!\n");
  else
    printf("Executing else part...!\n");

  if (cows == 5 )
  {
    printf("We have 5 cows\n");
  }
  else if( (cows == 6 )
  {
    printf("We have 6 cows\n");
  }
}

This will produce following result:

We have cows
Executing else part...!
We have 6 cows

previous

Copyright © tutorialspoint.com