Using if-else statement in C: A Beginner's Guide


The condition is any expression that returns a boolean value. An if statement executes code conditionally depending on the result of the condition in parentheses. When condition in parentheses is true it will enter to the block of if statement which is defined by curly braces like open braces and close braces. Opening bracket till the closing bracket is the scope of the if statement. The else block is optional and can be omitted.It runs if the if statement is false and does not run if the if statement is true because in that case if statement executes.

  Syntax:
     if( condition )
     {
        // body of the statements will execute if the condition is true ;
     }
     else
     {
        // body of the statements will executed if the condition is false;
     }

The code is a C program that demonstrates the use of the if-else statement in C. Here is an explanation of each line of the code:

  • char name[10]; declares a character array variable named name with a maximum size of 10 characters.
  • int age; declares a variable age of type int.
  • printf("\nEnter Your Name : "); prints a message prompting the user to enter their name.
  • scanf("%s",name); reads a string input from the user and stores it in the variable name.
  • printf("\nEnter The Age : "); prints a message prompting the user to enter their age.
  • scanf("%d",&age); reads an integer input from the user and stores it in the variable age.
  • if(age>=18) checks if the value of the variable age is greater than or equal to 18.
  • printf("\n %s age is %d Eligible For Vote",name,age); prints a message stating that the user is eligible to vote if the above if condition is true.
  • else is used to specify an alternative action to take when the if condition is false.
  • printf("\n %s age is %d Not Eligible For Vote",name,age); prints a message stating that the user is not eligible to vote if the if condition is false.
  • return 0; The return 0; statement is used to indicate that the main function has completed successfully. The value 0 is returned as the exit status of the program, which indicates a successful execution.

The above program prompts the user to enter their name and age. Then it checks if the entered age is greater than or equal to 18. If it is true, it prints a message stating that the user is eligible to vote. If it is false, it prints a message stating that the user is not eligible to vote.


Source Code

//if else statement
 
#include<stdio.h>
int main()
{
    char name[10];
    int age;
    printf("\nEnter Your Name : ");
    scanf("%s",name);
    printf("\nEnter The Age : ");
    scanf("%d",&age);
    if(age>=18)
    {
        printf("\n %s  age is %d Eligible For Vote",name,age);
    }
    else
    {
        printf("\n %s  age is %d Not Eligible For Vote",name,age);
    }
    return 0;
}
 
 
To download raw file Click Here

Output

Enter Your Name : Ram

Enter The Age : 17

Ram  age is 17 Not Eligible For Vote

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C