c
C Programming Tutorial : Introduction

C Conditional Statements - If, If-Else

In C programs, statements are executed sequentially in the order in which they appear in the program. But sometimes we may want to use a condition for executing only a part of program. Conditional statements enable us to specify the order in which the various instructions in the program are to be executed. Conditional statements help us to take a decision based on any conditions. Conditional statements define how the control is transferred to other parts of the program. An if statement consists of a Boolean expression followed by one or more statements.

C language supports following types of Conditional- statements, which are as

  1. If statement
  2. If-Else statement
  3. Nested If-else statement
  4. If-Else If ladder
  5. Switch statement

If statement: Simple If

The statements inside of the if body will be executed only when the condition defined by if statement is true. If the condition is false, then compiler skips the statement enclosed with body if statement. We can have any number of if statements in a C program.

In C we represent true with a non-zero integer and false with zero. Hence, in C if an expression evaluates to integer is considered as Boolean expression.

Syntax

if(expression)
{
 //code to be executed
}
 
flow chart Simple If

How "if" statement works..

  • If the expression is evaluated to nonzero (true) then if block statement(s) are executed.
  • If the expression is evaluated to zero (false) then Control passes to the next statement following it.

Example-1

/*Write a program and check if the Number is Even.*/

#include<stdio.h>
#include<conio.h>
void main()
{
    int num=0;
    printf("enter the number");
    scanf("%d",&num);
    if(n%2==0)
    {
        printf("%d number in even",num);
    }
    getch();
}
 

Example-2


/*Write a program to input user age and check if he is eligible to vote in India or not.
A person in India is eligible to vote if he is 18+.*/
#include <stdio.h>
#include<conio.h>
void main()
{
    int age;
    printf("Enter your age: ");
    scanf("%d", &age);
    if(age >= 18)
    {
        printf("You are eligible to vote in India.");
    }
getch();
}
 

If-else statement

This is a bi-directional conditional control statement. This statement is used to test a condition and take one of the two possible actions. If the condition is true, then a single statement or a block of statements is executed (one part of the program), otherwise another single statement or a block of statements is executed (other part of the program). Recall that in C, any nonzero value is regarded as true while zero is regarded as false.

Syntax

if(expression)
{
 //Statements
}
else
{
 //Statements
}
 
Flowchart If-Else-Statement

How "if..else" statement works..

  • If the expression is evaluated to nonzero (true) then if block statement(s) are executed.
  • If the expression is evaluated to zero (false) then else block statement(s) are executed.

Example -1

/*Write a program to check whether a number entered by the user is Even or Odd*/
#include<stdio.h>
#include<conio.h>
void main()
{
    int num=0;
    printf("enter the number");
    scanf("%d",&num);
    if(n%2==0)
    {
        printf("%d number in even", num);
    }
    else
    {
        printf("%d number in odd",num);
    }
    getch();
}
 

Example-2

/*Write a program to check the Largest no */

#include <stdio.h>
#include<conio.h>
void main()
{
    int num1, num2;
    printf("Enter two numbers: ");
    scanf("%d%d", &num1, &num2);
    if(num1 > num2)
    {
        printf("First number is the Largest.");
    }
    else
    {
        printf("Second number is the Largest.");
    }
getch();
}