Site icon TestingDocs.com

C if-else Statement

Overview

In this tutorial, we will learn about C if-else Statement with examples. The simple if statement only executes the if block when the conditional expression evaluates to true.

C if-else Statement

The general syntax for the C if-else statement is as follows:

if ( condition )
{
     // if block
    // executed when the condition is true
}
else
{
    // else block
   // executed when the condition is false
}

Example

Let’s write a simple C program to illustrate the if-else statements.

/**
**********************************
* Program Description:
*   C if-else Statement Demo
* Filename  : ifelseDemo.c
* Author    : TestingDocs
* Date      : 02/02/2017
* C Tutorials - www.TestingDocs.com
*************************************
*/
#include<stdio.h>

int main()
{
    int number = 0;
    printf("Enter the number =");
    scanf("%d",&number);
    // C if-else statement
    if(number % 2 == 0)
    {
        printf("The given number (%d) is Even.\n",number);
    }
    else
    {
        printf("The given number (%d) is Odd.\n",number);
    }

    return 0;
} // end main

Exit mobile version