C programming if else if else statements

C programming  if else if else statements

In this tutorial, we will discuss the subject of C programming if else-if else statements
In this post, we are going to learn how to use if else-if else statements in C programming

first, The if statement evaluates the Boolean Expression inside the parenthesis. when the expression is evaluated to true(1), enter the body part and statements inside the body part of if are executed.

if the test expression is evaluated to false(0), The control is skipped the inside the body of statements from execution and exit from the if part

 

already we are learned the concept of the relational operator in the operator in C language to about understand boolean expression (test expression)

Syntex for if…..  else

if(boolean_expression) {
   /* statement(s) will execute if the boolean expression is true */
}
else {
   /* statement(s) will execute if the boolean expression is false */
}

Flow diagram of if statement

If -else statements

/*

if(expression)
    {
    statement
    }else{
    statement;
    }

*/


program1

#include <stdio.h>
#include <stdlib.h>

int main()

    int age=20;
    if(age>=18)
    {
        printf("you are a minor");
    }
    else{

        printf("you are a adultn");
    }

    return 0;
}

When the code is executed, it produces the following result

Program 1
program 2
#include <stdio.h>
#include <stdlib.h>


int main()
{
    /*local veriable definition*/    int a=17;

    /*check the boolean condition*/if(a<20)
{

    /*if condition is true then print the following*/    printf("a is less than 20!n");

    }
    else{
         /*if condition is false then print the following*/         printf("a is not less than 20!n");
    }

     printf("value of a is:%dn",a);
    return 0;
}

 

When the code is executed, it produces the following result
program 2

Syntex for if…..  else if ……else

 

if(boolean_expression 1) {
   /* Executes when the boolean expression 1 is true */
}
else if( boolean_expression 2) {
   /* Executes when the boolean expression 2 is true */
}
else if( boolean_expression 3) {
   /* Executes when the boolean expression 3 is true */
}
else  {
   /* executes when the none of the above condition is true */
}
Program 3
#include <stdio.h>
#include <stdlib.h>

int main()
{
    /*local variable definition*/    int a=25;

    /*local variable definition*/    if(a==10){

        /*if condition is true then printing following*/        printf("valeu of a is 10!n");
    }
    else if(a==20)
    {
        /*if condition is true then printing following*/        printf("valeu of a is 20!n");
    }
    else if(a==30)
    {
        /*if condition is true then printing following*/        printf("valeu of a is 30!n");
    }
    else
    {
        /*if condition is false then printing following*/        printf("value is not matching!n");

    }
    printf("Exact value of a is:%d!n",a);
    return 0;
}

When the code is executed, it produces the following result

program 3
Program 3
In the following program, the program allows the user to enter an integer number and checks the number for finding even or odd using the modular operator.
finally, it will be displayed given number is even or odd
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int number;
    printf("Enter an integer:");
    scanf("%d",&number);

    if(number%2==0)
    {
        printf("%d is an even integer.",number);
    }
        else
        {
            printf("%d is an odd integer.",number);
        }
return 0;
    }

When the code is executed, it produces the following result

program 4
Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

3 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

3 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

4 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

4 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

4 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

4 months ago

This website uses cookies.